forked from NodeBB/nodebb-plugin-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsockets.js
56 lines (49 loc) · 1.38 KB
/
websockets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var async = require('async');
var privileges = module.parent.parent.require('./privileges');
var SocketPosts = module.parent.parent.require('./socket.io/posts');
var posts = module.parent.parent.require('./posts');
module.exports.checkbox = {
edit: function (socket, data, callback) {
async.waterfall([
async.apply(privileges.posts.canEdit, parseInt(data.pid, 10), socket.uid),
function (canEdit, next) {
if (!canEdit) {
return next(new Error('[[error:no-privileges]]'));
}
posts.getPostField(data.pid, 'content', next);
},
function (content, next) {
// Generate array of checkbox indices in the raw content
var checkboxRegex = /\[[\sx]?\]/g;
var match;
var indices = [];
while ((match = checkboxRegex.exec(content)) !== null) {
indices.push(match.index);
}
next(null, content, indices);
},
function (content, indices, next) {
var checkboxRegex = /\[[\sx]?\]/g;
content = content.replace(checkboxRegex, function (match, idx) {
if (idx !== indices[data.index]) {
return match;
}
return data.state ? '[x]' : '[ ]';
});
next(null, content);
},
function (content, next) {
SocketPosts.edit(socket, {
pid: data.pid,
content: content,
}, next);
},
], function (err) {
if (err) {
return callback(err, false);
}
callback(err, true);
});
},
};