Skip to content

Commit

Permalink
Move readOnlyManager.getIds() call to socket.io handlers
Browse files Browse the repository at this point in the history
This reduces the complexity of commentManager. It also makes future
authorization checks easier to implement (because the checks should be
performed where the messages arrive, not deep in the abstracted
internals).

Also: Update the `apiAddComments` and `apiAddCommentReplies` message
events to broadcast to both pad IDs.
  • Loading branch information
rhansen committed Oct 14, 2020
1 parent 463300b commit ed5e163
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 93 deletions.
56 changes: 14 additions & 42 deletions commentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ 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 readOnlyManager = require("ep_etherpad-lite/node/db/ReadOnlyManager.js");
var shared = require('./static/js/shared');

exports.getComments = async function (padId, callback)
{
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

// Not sure if we will encouter race conditions here.. Be careful.

//get the globalComments
Expand All @@ -23,12 +18,8 @@ exports.getComments = async function (padId, callback)
});
};

exports.deleteComment = async function (padId, commentId, callback)
exports.deleteComment = function (padId, commentId, callback)
{
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

db.get('comments:' + padId, function(err, comments)
{
if(ERR(err, callback)) return;
Expand All @@ -39,7 +30,7 @@ exports.deleteComment = async function (padId, commentId, callback)
delete comments[commentId];
db.set("comments:" + padId, comments);

callback(padIds, commentId);
callback(padId, commentId);

});
};
Expand All @@ -53,23 +44,19 @@ exports.deleteComments = function (padId, callback)
});
};

exports.addComment = async function(padId, data, callback)
exports.addComment = function(padId, data, callback)
{
await exports.bulkAddComments(padId, [data], function(err, padIds, commentIds, comments) {
exports.bulkAddComments(padId, [data], function(err, commentIds, comments) {
if(ERR(err, callback)) return;

if(commentIds && commentIds.length > 0 && comments && comments.length > 0) {
callback(null, padIds, commentIds[0], comments[0]);
callback(null, commentIds[0], comments[0]);
}
});
};

exports.bulkAddComments = async function(padId, data, callback)
{
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

//get the entry
db.get("comments:" + padId, function(err, comments) {
if(ERR(err, callback)) return;
Expand Down Expand Up @@ -100,7 +87,7 @@ exports.bulkAddComments = async function(padId, data, callback)
//save the new element back
db.set("comments:" + padId, comments);

callback(null, padIds, commentIds, newComments);
callback(null, commentIds, newComments);
});
};

Expand All @@ -123,17 +110,13 @@ exports.copyComments = function(originalPadId, newPadID, callback)
};

exports.getCommentReplies = async function (padId, callback){
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

//get the globalComments replies
db.get("comment-replies:" + padId, function(err, replies)
{
if(ERR(err, callback)) return;
//comment does not exists
if(replies == null) replies = {};
callback(null, { replies: replies});
callback(null, { replies: replies });
});
};

Expand All @@ -145,21 +128,17 @@ exports.deleteCommentReplies = function (padId, callback){
});
};

exports.addCommentReply = async function(padId, data, callback){
await exports.bulkAddCommentReplies(padId, [data], function(err, padIds, replyIds, replies) {
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, padIds, replyIds[0], replies[0]);
callback(null, replyIds[0], replies[0]);
}
});
};

exports.bulkAddCommentReplies = async function(padId, data, callback){
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

//get the entry
db.get("comment-replies:" + padId, function(err, replies){
if(ERR(err, callback)) return;
Expand Down Expand Up @@ -194,7 +173,7 @@ exports.bulkAddCommentReplies = async function(padId, data, callback){
//save the new element back
db.set("comment-replies:" + padId, replies);

callback(null, padIds, replyIds, newReplies);
callback(null, replyIds, newReplies);
});
};

Expand All @@ -218,10 +197,6 @@ exports.copyCommentReplies = function(originalPadId, newPadID, callback){
exports.changeAcceptedState = async function(padId, commentId, state, callback){
// Given a comment we update that comment to say the change was accepted or reverted

// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

// If we're dealing with comment replies we need to a different query
var prefix = "comments:";
if(commentId.substring(0,7) === "c-reply"){
Expand Down Expand Up @@ -249,17 +224,14 @@ exports.changeAcceptedState = async function(padId, commentId, state, callback){
//save the new element back
db.set(prefix + padId, comments);

callback(null, padIds, commentId, comment);
callback(null, commentId, comment);
});
}

exports.changeCommentText = async function(padId, commentId, commentText, callback){
var commentTextIsNotEmpty = commentText.length > 0;
if(commentTextIsNotEmpty){
// Given a comment we update the comment text
// We might need to change readOnly PadIds to Normal PadIds
var padIds = await readOnlyManager.getIds(padId);
padId = padIds.padId;

// If we're dealing with comment replies we need to a different query
var prefix = "comments:";
Expand All @@ -278,9 +250,9 @@ exports.changeCommentText = async function(padId, commentId, commentText, callba
//save the comment updated back
db.set(prefix + padId, comments);

callback(null, padIds);
callback(null);
});
}else{// don't save comment text blank
callback(true, padIds);
callback(true);
}
}
Loading

0 comments on commit ed5e163

Please sign in to comment.