Skip to content

Commit 318931e

Browse files
committed
Allow suggestion editing
1 parent c2df5e1 commit 318931e

File tree

6 files changed

+579
-312
lines changed

6 files changed

+579
-312
lines changed

commentManager.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,52 @@ exports.changeCommentText = async (padId, commentId, commentText, authorId) => {
207207
// save the comment updated back
208208
await db.set(prefix + padId, comments);
209209
};
210+
211+
exports.changeComment = async (padId, commentId, commentText, changeFrom, changeTo, authorId, state) => { // eslint-disable max-len
212+
if (commentText.length <= 0) {
213+
logger.debug(`ignoring attempt to change comment ${commentId} to the empty string`);
214+
throw new Error('comment_cannot_be_empty');
215+
}
216+
217+
// Given a comment we update the comment text
218+
219+
// If we're dealing with comment replies we need to a different query
220+
let prefix = 'comments:';
221+
if (commentId.substring(0, 7) === 'c-reply') {
222+
prefix = 'comment-replies:';
223+
}
224+
225+
// get the entry
226+
const comments = await db.get(prefix + padId);
227+
if (comments == null || comments[commentId] == null) {
228+
logger.debug(`ignoring attempt to edit non-existent comment ${commentId}`);
229+
throw new Error('no_such_comment');
230+
}
231+
if (comments[commentId].author !== authorId) {
232+
logger.debug(`author ${authorId} attempted to edit comment ${commentId} ` +
233+
`belonging to author ${comments[commentId].author}`);
234+
throw new Error('unauth');
235+
}
236+
// update the comment text
237+
comments[commentId].text = commentText;
238+
if (changeTo) {
239+
comments[commentId].changeTo = changeTo;
240+
if (!comments[commentId].changeFrom) {
241+
comments[commentId].changeFrom = changeFrom;
242+
}
243+
}
244+
245+
if (comments[commentId].changeTo && !changeTo) {
246+
comments[commentId].changeTo = null;
247+
}
248+
249+
if (state) {
250+
comments[commentId].changeAccepted = true;
251+
comments[commentId].changeReverted = false;
252+
} else {
253+
comments[commentId].changeAccepted = false;
254+
comments[commentId].changeReverted = true;
255+
}
256+
// save the comment updated back
257+
await db.set(prefix + padId, comments);
258+
};

exportHTML.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ exports.getLineHTMLForExport = async (hookName, context) => {
3030
let hasPlugin = false;
3131
// Load the HTML into a throwaway div instead of calling $.load() to avoid
3232
// https://github.com/cheeriojs/cheerio/issues/1031
33-
//const content = $('<div>').html(context.lineContent);
33+
// const content = $('<div>').html(context.lineContent);
3434
// include links for each comment which we will add content later.
3535
content.find('span').each(function () {
3636
const span = $(this);

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ exports.socketio = (hookName, args, cb) => {
137137
socket.broadcast.to(padId).emit('textCommentUpdated', commentId, commentText);
138138
}));
139139

140+
socket.on('updateComment', handler(async (data) => {
141+
const {commentId, commentText, authorId, changeFrom, changeTo, changeAcceptedState} = data;
142+
const {padId} = await readOnlyManager.getIds(data.padId);
143+
await commentManager.changeComment(padId, commentId, commentText, changeFrom, changeTo, authorId, changeAcceptedState); // eslint-disable max-len
144+
socket.broadcast.to(padId).emit('commentUpdated', commentId, commentText, changeFrom, changeTo); // eslint-disable max-len
145+
}));
146+
140147
socket.on('addCommentReply', handler(async (data) => {
141148
const {padId} = await readOnlyManager.getIds(data.padId);
142149
const [replyId, reply] = await commentManager.addCommentReply(padId, data);

0 commit comments

Comments
 (0)