Skip to content

Commit

Permalink
fix missing broadcast to all users
Browse files Browse the repository at this point in the history
  • Loading branch information
pr4xx authored and rhansen committed Oct 14, 2020
1 parent 86a571a commit 463300b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 60 deletions.
80 changes: 36 additions & 44 deletions commentManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ var shared = require('./static/js/shared');

exports.getComments = async function (padId, callback)
{
// We need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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.

Expand All @@ -25,8 +23,12 @@ exports.getComments = async function (padId, callback)
});
};

exports.deleteComment = function (padId, commentId, callback)
exports.deleteComment = async 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 @@ -37,7 +39,7 @@ exports.deleteComment = function (padId, commentId, callback)
delete comments[commentId];
db.set("comments:" + padId, comments);

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

});
};
Expand All @@ -51,24 +53,22 @@ exports.deleteComments = function (padId, callback)
});
};

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

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

exports.bulkAddComments = async function(padId, data, callback)
{
// We need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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) {
Expand Down Expand Up @@ -100,7 +100,7 @@ exports.bulkAddComments = async function(padId, data, callback)
//save the new element back
db.set("comments:" + padId, comments);

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

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

exports.getCommentReplies = async function (padId, callback){
// We need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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 @@ -147,22 +145,20 @@ exports.deleteCommentReplies = function (padId, callback){
});
};

exports.addCommentReply = function(padId, data, callback){
exports.bulkAddCommentReplies(padId, [data], function(err, replyIds, replies) {
exports.addCommentReply = async function(padId, data, callback){
await exports.bulkAddCommentReplies(padId, [data], function(err, padIds, replyIds, replies) {
if(ERR(err, callback)) return;

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

exports.bulkAddCommentReplies = async function(padId, data, callback){
// We need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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){
Expand Down Expand Up @@ -198,7 +194,7 @@ exports.bulkAddCommentReplies = async function(padId, data, callback){
//save the new element back
db.set("comment-replies:" + padId, replies);

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

Expand All @@ -222,11 +218,9 @@ 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 need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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 Down Expand Up @@ -255,19 +249,17 @@ exports.changeAcceptedState = async function(padId, commentId, state, callback){
//save the new element back
db.set(prefix + padId, comments);

callback(null, commentId, comment);
callback(null, padIds, 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 need to change readOnly PadIds to Normal PadIds
var isReadOnly = padId.indexOf("r.") === 0;
if(isReadOnly){
padId = await readOnlyManager.getPadId(padId);
};
// 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 @@ -286,9 +278,9 @@ exports.changeCommentText = async function(padId, commentId, commentText, callba
//save the comment updated back
db.set(prefix + padId, comments);

callback(null);
callback(null, padIds);
});
}else{// don't save comment text blank
callback(true);
callback(true, padIds);
}
}
48 changes: 32 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,21 @@ exports.socketio = function (hook_name, args, cb){
socket.on('addComment', (data, respond) => {
var padId = data.padId;
var content = data.comment;
commentManager.addComment(padId, content, function (err, commentId, comment){
socket.broadcast.to(padId).emit('pushAddComment', commentId, comment);
commentManager.addComment(padId, content, function (err, padIds, commentId, comment){
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('pushAddComment', commentId, comment);
});
respond(commentId, comment);
});
});

socket.on('deleteComment', (data, respond) => {
// delete the comment on the database
commentManager.deleteComment(data.padId, data.commentId, function (){
commentManager.deleteComment(data.padId, data.commentId, function (padIds){
// Broadcast to all other users that this comment was deleted
socket.broadcast.to(data.padId).emit('commentDeleted', data.commentId);
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('commentDeleted', data.commentId);
});
});

});
Expand All @@ -86,31 +90,39 @@ exports.socketio = function (hook_name, args, cb){
// 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);
commentManager.changeAcceptedState(padId, data.commentId, false, function(error, padIds){
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('changeReverted', data.commentId);
});
});
});

socket.on('acceptChange', (data, respond) => {
// 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);
commentManager.changeAcceptedState(padId, data.commentId, true, function(error, padIds){
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('changeAccepted', data.commentId);
});
});
});

socket.on('bulkAddComment', (padId, data, respond) => {
commentManager.bulkAddComments(padId, data, function(error, commentsId, comments){
socket.broadcast.to(padId).emit('pushAddCommentInBulk');
commentManager.bulkAddComments(padId, data, function(error, padIds, commentsId, comments){
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('pushAddCommentInBulk');
});
var commentWithCommentId = _.object(commentsId, comments); // {c-123:data, c-124:data}
respond(commentWithCommentId);
});
});

socket.on('bulkAddCommentReplies', (padId, data, respond) => {
commentManager.bulkAddCommentReplies(padId, data, function (err, repliesId, replies){
socket.broadcast.to(padId).emit('pushAddCommentReply', repliesId, replies);
commentManager.bulkAddCommentReplies(padId, data, function (err, padIds, repliesId, replies){
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('pushAddCommentReply', repliesId, replies);
});
var repliesWithReplyId = _.zip(repliesId, replies);
respond(repliesWithReplyId);
});
Expand All @@ -122,19 +134,23 @@ exports.socketio = function (hook_name, args, cb){
var padId = data.padId;
var commentId = data.commentId;
var commentText = data.commentText;
commentManager.changeCommentText(padId, commentId, commentText, function(err) {
commentManager.changeCommentText(padId, commentId, commentText, function(err, padIds) {
if(!err){
socket.broadcast.to(padId).emit('textCommentUpdated', commentId, commentText);
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('textCommentUpdated', commentId, commentText);
});
}
respond(err);
});
});

socket.on('addCommentReply', (data, respond) => {
const padId = data.padId;
commentManager.addCommentReply(padId, data, (err, replyId, reply) => {
commentManager.addCommentReply(padId, data, (err, padIds, replyId, reply) => {
reply.replyId = replyId;
socket.broadcast.to(padId).emit('pushAddCommentReply', replyId, reply);
[padIds.padId, padIds.readOnlyPadId].forEach(function(padId) {
socket.broadcast.to(padId).emit('pushAddCommentReply', replyId, reply);
});
respond(replyId, reply);
});
});
Expand Down

0 comments on commit 463300b

Please sign in to comment.