forked from ether/ep_comments_page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
331 lines (281 loc) · 11.3 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
var eejs = require('ep_etherpad-lite/node/eejs/');
var settings = require('ep_etherpad-lite/node/utils/Settings');
var formidable = require('formidable');
var clientIO = require('socket.io-client');
var commentManager = require('./commentManager');
var comments = require('./comments');
var apiUtils = require('./apiUtils');
var _ = require('ep_etherpad-lite/static/js/underscore');
exports.padRemove = function(hook_name, context, callback) {
commentManager.deleteCommentReplies(context.padID, function() {
commentManager.deleteComments(context.padID, callback);
});
}
exports.padCopy = function(hook_name, context, callback) {
commentManager.copyComments(context.originalPad.id, context.destinationID, function() {
commentManager.copyCommentReplies(context.originalPad.id, context.destinationID, callback);
});
}
exports.handleMessageSecurity = function(hook_name, context, callback){
if(context.message && context.message.data && context.message.data.apool){
var apool = context.message.data.apool;
if(apool.numToAttrib && apool.numToAttrib[0] && apool.numToAttrib[0][0]){
if(apool.numToAttrib[0][0] === "comment"){
// Comment change, allow it to override readonly security model!!
callback(true);
}else{
callback();
}
}else{
callback();
}
}else{
callback();
}
};
exports.socketio = function (hook_name, args, cb){
var app = args.app;
var io = args.io;
var pushComment;
var padComment = io;
var commentSocket = io
.of('/comment')
.on('connection', function (socket) {
// Join the rooms
socket.on('getComments', function (data, callback) {
var padId = data.padId;
socket.join(padId);
commentManager.getComments(padId, function (err, comments){
callback(comments);
});
});
socket.on('getCommentReplies', function (data, callback) {
var padId = data.padId;
commentManager.getCommentReplies(padId, function (err, replies){
callback(replies);
});
});
// On add events
socket.on('addComment', function (data, callback) {
var padId = data.padId;
var content = data.comment;
commentManager.addComment(padId, content, function (err, commentId, comment){
socket.broadcast.to(padId).emit('pushAddComment', commentId, comment);
callback(commentId, comment);
});
});
socket.on('deleteComment', function(data, callback) {
// delete the comment on the database
commentManager.deleteComment(data.padId, data.commentId, function (){
// Broadcast to all other users that this comment was deleted
socket.broadcast.to(data.padId).emit('commentDeleted', data.commentId);
});
});
socket.on('revertChange', function(data, callback) {
// Broadcast to all other users that this change was accepted.
// Note that commentId here can either be the commentId or replyId..
var padId = data.padId;
commentManager.changeAcceptedState(padId, data.commentId, false, function(){
socket.broadcast.to(padId).emit('changeReverted', data.commentId);
});
});
socket.on('acceptChange', function(data, callback) {
// Broadcast to all other users that this change was accepted.
// Note that commentId here can either be the commentId or replyId..
var padId = data.padId;
commentManager.changeAcceptedState(padId, data.commentId, true, function(){
socket.broadcast.to(padId).emit('changeAccepted', data.commentId);
});
});
socket.on('bulkAddComment', function (padId, data, callback) {
commentManager.bulkAddComments(padId, data, function(error, commentsId, comments){
socket.broadcast.to(padId).emit('pushAddCommentInBulk');
var commentWithCommentId = _.object(commentsId, comments); // {c-123:data, c-124:data}
callback(commentWithCommentId)
});
});
socket.on('bulkAddCommentReplies', function(padId, data, callback){
commentManager.bulkAddCommentReplies(padId, data, function (err, repliesId, replies){
socket.broadcast.to(padId).emit('pushAddCommentReply', repliesId, replies);
var repliesWithReplyId = _.zip(repliesId, replies);
callback(repliesWithReplyId);
});
});
socket.on('updateCommentText', function(data, callback) {
// Broadcast to all other users that the comment text was changed.
// Note that commentId here can either be the commentId or replyId..
var padId = data.padId;
var commentId = data.commentId;
var commentText = data.commentText;
commentManager.changeCommentText(padId, commentId, commentText, function(err) {
if(!err){
socket.broadcast.to(padId).emit('textCommentUpdated', commentId, commentText);
}
callback(err);
});
});
socket.on('addCommentReply', function (data, callback) {
var padId = data.padId;
var content = data.reply;
var changeTo = data.changeTo || null;
var changeFrom = data.changeFrom || null;
var changeAccepted = data.changeAccepted || null;
var changeReverted = data.changeReverted || null;
var commentId = data.commentId;
commentManager.addCommentReply(padId, data, function (err, replyId, reply, changeTo, changeFrom, changeAccepted, changeReverted){
reply.replyId = replyId;
socket.broadcast.to(padId).emit('pushAddCommentReply', replyId, reply, changeTo, changeFrom, changeAccepted, changeReverted);
callback(replyId, reply);
});
});
// comment added via API
socket.on('apiAddComments', function (data) {
var padId = data.padId;
var commentIds = data.commentIds;
var comments = data.comments;
for (var i = 0, len = commentIds.length; i < len; i++) {
socket.broadcast.to(padId).emit('pushAddComment', commentIds[i], comments[i]);
}
});
// comment reply added via API
socket.on('apiAddCommentReplies', function (data) {
var padId = data.padId;
var replyIds = data.replyIds;
var replies = data.replies;
for (var i = 0, len = replyIds.length; i < len; i++) {
var reply = replies[i];
var replyId = replyIds[i];
reply.replyId = replyId;
socket.broadcast.to(padId).emit('pushAddCommentReply', replyId, reply);
}
});
});
};
exports.eejsBlock_dd_insert = function (hook_name, args, cb) {
args.content = args.content + eejs.require("ep_comments_page/templates/menuButtons.ejs");
return cb();
};
exports.eejsBlock_mySettings = function (hook_name, args, cb) {
args.content = args.content + eejs.require("ep_comments_page/templates/settings.ejs");
return cb();
};
exports.eejsBlock_editbarMenuLeft = function (hook_name, args, cb) {
args.content = args.content + eejs.require("ep_comments_page/templates/commentBarButtons.ejs");
return cb();
};
exports.eejsBlock_scripts = function (hook_name, args, cb) {
args.content = args.content + eejs.require("ep_comments_page/templates/comments.html", {}, module);
args.content = args.content + eejs.require("ep_comments_page/templates/commentIcons.html", {}, module);
return cb();
};
exports.eejsBlock_styles = function (hook_name, args, cb) {
args.content = args.content + eejs.require("ep_comments_page/templates/styles.html", {}, module);
return cb();
};
exports.clientVars = function (hook, context, cb) {
var displayCommentAsIcon = settings.ep_comments_page ? settings.ep_comments_page.displayCommentAsIcon : false;
var highlightSelectedText = settings.ep_comments_page ? settings.ep_comments_page.highlightSelectedText : false;
return cb({
"displayCommentAsIcon": displayCommentAsIcon,
"highlightSelectedText": highlightSelectedText,
});
};
exports.expressCreateServer = function (hook_name, args, callback) {
args.app.get('/p/:pad/:rev?/comments', function(req, res) {
var fields = req.query;
// check the api key
if(!apiUtils.validateApiKey(fields, res)) return;
// sanitize pad id before continuing
var padIdReceived = apiUtils.sanitizePadId(req);
comments.getPadComments(padIdReceived, function(err, data) {
if(err) {
res.json({code: 2, message: "internal error", data: null});
} else {
res.json({code: 0, data: data});
}
});
});
args.app.post('/p/:pad/:rev?/comments', function(req, res) {
new formidable.IncomingForm().parse(req, function (err, fields, files) {
// check the api key
if(!apiUtils.validateApiKey(fields, res)) return;
// check required fields from comment data
if(!apiUtils.validateRequiredFields(fields, ['data'], res)) return;
// sanitize pad id before continuing
var padIdReceived = apiUtils.sanitizePadId(req);
// create data to hold comment information:
try {
var data = JSON.parse(fields.data);
comments.bulkAddPadComments(padIdReceived, data, function(err, commentIds, comments) {
if(err) {
res.json({code: 2, message: "internal error", data: null});
} else {
broadcastCommentsAdded(padIdReceived, commentIds, comments);
res.json({code: 0, commentIds: commentIds});
}
});
} catch(e) {
res.json({code: 1, message: "data must be a JSON", data: null});
}
});
});
args.app.get('/p/:pad/:rev?/commentReplies', function(req, res){
//it's the same thing as the formidable's fields
var fields = req.query;
// check the api key
if(!apiUtils.validateApiKey(fields, res)) return;
//sanitize pad id before continuing
var padIdReceived = apiUtils.sanitizePadId(req);
// call the route with the pad id sanitized
comments.getPadCommentReplies(padIdReceived, function(err, data) {
if(err) {
res.json({code: 2, message: "internal error", data:null})
} else {
res.json({code: 0, data: data});
}
});
});
args.app.post('/p/:pad/:rev?/commentReplies', function(req, res) {
new formidable.IncomingForm().parse(req, function (err, fields, files) {
// check the api key
if(!apiUtils.validateApiKey(fields, res)) return;
// check required fields from comment data
if(!apiUtils.validateRequiredFields(fields, ['data'], res)) return;
// sanitize pad id before continuing
var padIdReceived = apiUtils.sanitizePadId(req);
// create data to hold comment reply information:
try {
var data = JSON.parse(fields.data);
comments.bulkAddPadCommentReplies(padIdReceived, data, function(err, replyIds, replies) {
if(err) {
res.json({code: 2, message: "internal error", data: null});
} else {
broadcastCommentRepliesAdded(padIdReceived, replyIds, replies);
res.json({code: 0, replyIds: replyIds});
}
});
} catch(e) {
res.json({code: 1, message: "data must be a JSON", data: null});
}
});
});
}
var broadcastCommentsAdded = function(padId, commentIds, comments) {
var socket = clientIO.connect(broadcastUrl);
var data = {
padId: padId,
commentIds: commentIds,
comments: comments
};
socket.emit('apiAddComments', data);
}
var broadcastCommentRepliesAdded = function(padId, replyIds, replies) {
var socket = clientIO.connect(broadcastUrl);
var data = {
padId: padId,
replyIds: replyIds,
replies: replies
};
socket.emit('apiAddCommentReplies', data);
}
var broadcastUrl = apiUtils.broadcastUrlFor("/comment");