forked from ether/ep_comments_page
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commentManager.js
250 lines (209 loc) · 7.91 KB
/
commentManager.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
var _ = require('ep_etherpad-lite/static/js/underscore');
var db = require('ep_etherpad-lite/node/db/DB').db;
var ERR = require('ep_etherpad-lite/node_modules/async-stacktrace');
var randomString = require('ep_etherpad-lite/static/js/pad_utils').randomString;
var shared = require('./static/js/shared');
var utils = require('./utils');
exports.getComments = async function(padId, callback)
{
// We need to change readOnly PadIds to Normal PadIds
padId = await utils.getReadWritePadId(padId);
// Not sure if we will encouter race conditions here.. Be careful.
//get the globalComments
db.get("comments:" + padId, function(err, comments)
{
if(ERR(err, callback)) return;
//comment does not exists
if(comments == null) comments = {};
callback(null, { comments: comments });
});
};
exports.deleteComments = function(padId, callback)
{
db.remove('comments:' + padId, function(err)
{
if(ERR(err, callback)) return;
callback(null);
});
};
exports.addComment = function(padId, data, callback)
{
exports.bulkAddComments(padId, [data], function(err, commentIds, comments) {
if(ERR(err, callback)) return;
if(commentIds && commentIds.length > 0 && comments && comments.length > 0) {
callback(null, commentIds[0], comments[0]);
}
});
};
exports.bulkAddComments = async function(padId, data, callback)
{
// We need to change readOnly PadIds to Normal PadIds
padId = await utils.getReadWritePadId(padId);
//get the entry
db.get("comments:" + padId, function(err, comments) {
if(ERR(err, callback)) return;
// the entry doesn't exist so far, let's create it
if(comments == null) comments = {};
var newComments = [];
var commentIds = _(data)
.chain()
.map(function(commentData) {
if (commentData.text.length > 0) {
//if the comment was copied it already has a commentID, so we don't need create one
var commentId = commentData.commentId || shared.generateCommentId();
var comment = {
"author": commentData.author || "empty",
"creator": commentData.creator || "empty",
"name": commentData.name,
"text": commentData.text,
"timestamp": parseInt(commentData.timestamp) || new Date().getTime()
};
//add the entry for this pad
comments[commentId] = comment;
newComments.push(comment);
return commentId;
}
return;
})
.compact()
.value();
//save the new element back
db.set("comments:" + padId, comments, function() {
callback(null, commentIds, newComments);
});
});
};
exports.copyComments = function(originalPadId, newPadID, callback)
{
//get the comments of original pad
db.get('comments:' + originalPadId, function(err, originalComments) {
if(ERR(err, callback)) return;
var copiedComments = _.mapObject(originalComments, function(thisComment, thisCommentId) {
// make sure we have different copies of the comment between pads
return _.clone(thisComment);
});
//save the comments on new pad
db.set('comments:' + newPadID, copiedComments, callback);
});
};
exports.getCommentReplies = async function(padId, callback){
// We need to change readOnly PadIds to Normal PadIds
padId = await utils.getReadWritePadId(padId);
//get the globalComments replies
db.get("comment-replies:" + padId, function(err, replies)
{
if(ERR(err, callback)) return;
//replies do not exist
if(replies == null) replies = {};
callback(null, { replies: replies });
});
};
exports.deleteCommentReplies = function(padId, callback){
db.remove('comment-replies:' + padId, function(err)
{
if(ERR(err, callback)) return;
callback(null);
});
};
exports.addCommentReply = function(padId, data, callback){
exports.bulkAddCommentReplies(padId, [data], function(err, replyIds, replies) {
if(ERR(err, callback)) return;
if(replyIds && replyIds.length > 0 && replies && replies.length > 0) {
callback(null, replyIds[0], replies[0]);
}
});
};
exports.bulkAddCommentReplies = async function(padId, data, callback){
// We need to change readOnly PadIds to Normal PadIds
padId = await utils.getReadWritePadId(padId);
//get the entry
db.get("comment-replies:" + padId, function(err, replies){
if(ERR(err, callback)) return;
// the entry doesn't exist so far, let's create it
if(replies == null) replies = {};
var newReplies = [];
var replyIds = _.map(data, function(replyData) {
//create the new reply id if necessary
var replyId = replyData.replyId || "cr-" + randomString(16);
metadata = replyData.comment || {};
var reply = {
"commentId" : replyData.commentId,
"text" : replyData.reply || replyData.text,
"author" : metadata.author || replyData.author || "empty",
"creator" : metadata.creator || replyData.creator || "empty",
"name" : metadata.name || replyData.name,
"timestamp" : parseInt(replyData.timestamp) || new Date().getTime()
};
//add the entry for this pad
replies[replyId] = reply;
newReplies.push(reply);
return replyId;
});
//save the new element back
db.set("comment-replies:" + padId, replies, function() {
callback(null, replyIds, newReplies);
});
});
};
exports.copyCommentReplies = function(originalPadId, newPadID, callback){
//get the replies of original pad
db.get('comment-replies:' + originalPadId, function(err, originalReplies){
if(ERR(err, callback)) return;
var copiedReplies = _.mapObject(originalReplies, function(thisReply, thisReplyId) {
// make sure we have different copies of the reply between pads
return _.clone(thisReply);
});
//save the comment replies on new pad
db.set('comment-replies:' + newPadID, copiedReplies, callback);
});
};
exports.changeCommentText = async function(padId, commentId, commentText, currentUser, callback){
var commentTextIsNotEmpty = (commentText || '').length > 0;
if(commentTextIsNotEmpty && commentId && currentUser){
// Given a comment we update the comment text
// We need to change readOnly PadIds to Normal PadIds
padId = await utils.getReadWritePadId(padId);
// If we're dealing with comment replies we need to a different query
var prefix = "comments:";
if(commentId.startsWith("cr")){
prefix = "comment-replies:";
}
//get the entry
db.get(prefix + padId, function(err, comments){
if(ERR(err, callback)) return;
var targetComment = comments[commentId];
var currentUserIsAuthor = targetComment && (targetComment.author === currentUser);
if (targetComment && currentUserIsAuthor) {
//update the comment text
targetComment.text = commentText;
//save the comment updated back
db.set(prefix + padId, comments, callback);
}else{
var userIsNotAuthorOfComment = targetComment && !currentUserIsAuthor;
var errorMessage = userIsNotAuthorOfComment ? 'User can not update comment' : 'Comment does not exist';
console.log('Error on updating comment with id ' + commentId + '.' + errorMessage);
callback(true)
}
});
}else{// don't save comment text blank
callback(true);
}
}
exports.toggleImportantFlag = function(padId, commentId, callback){
var prefix = "comments:";
//get the entry
db.get(prefix + padId, function(err, comments){
if(ERR(err, callback)) return;
var targetComment = comments[commentId];
if (targetComment) {
//update the comment important flag
targetComment.important = !targetComment.important;
//save the comment updated back
db.set(prefix + padId, comments, callback);
}else{
var errorMessage = 'Comment does not exist';
console.log('Error on updating comment with id ' + commentId + '.' + errorMessage);
callback(true)
}
});
}