-
Notifications
You must be signed in to change notification settings - Fork 40
/
websockets.js
37 lines (31 loc) · 999 Bytes
/
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
'use strict';
const privileges = require.main.require('./src/privileges');
const SocketPosts = require.main.require('./src/socket.io/posts');
const posts = require.main.require('./src/posts');
module.exports.checkbox = {
edit: async function (socket, data) {
const canEdit = await privileges.posts.canEdit(parseInt(data.pid, 10), socket.uid);
if (!canEdit) {
throw new Error('[[error:no-privileges]]');
}
let content = await posts.getPostField(data.pid, 'content');
// Generate array of checkbox indices in the raw content
const checkboxRegex = /\[[\sx]?\]/g;
let match;
const indices = [];
// eslint-disable-next-line
while ((match = checkboxRegex.exec(content)) !== null) {
indices.push(match.index);
}
content = content.replace(checkboxRegex, function (match, idx) {
if (idx !== indices[data.index]) {
return match;
}
return data.state ? '[x]' : '[ ]';
});
await SocketPosts.edit(socket, {
pid: data.pid,
content: content,
});
},
};