Skip to content

Commit

Permalink
Fix race condition of lastMessage set (#14041)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok authored Apr 8, 2019
1 parent 34e1e0c commit e4b5011
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions app/lib/server/lib/notifyUsersOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ export function updateUsersSubscriptions(message, room, users) {
}
}

// Update all the room activity tracker fields
// This method take so long to execute on gient rooms cuz it will trugger the cache rebuild for the releations of that room
Rooms.incMsgCountAndSetLastMessageById(message.rid, 1, message.ts, settings.get('Store_Last_Message') && message);
// Update all other subscriptions to alert their owners but witout incrementing
// the unread counter, as it is only for mentions and direct messages
// We now set alert and open properties in two separate update commands. This proved to be more efficient on MongoDB - because it uses a more efficient index.
Expand Down Expand Up @@ -122,6 +119,9 @@ function notifyUsersOnMessage(message, room) {
return message;
}

// Update all the room activity tracker fields
Rooms.incMsgCountAndSetLastMessageById(message.rid, 1, message.ts, settings.get('Store_Last_Message') && message);

if (message.tmid) {
return message;
}
Expand Down
2 changes: 1 addition & 1 deletion app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ export class Rooms extends Base {
}

setReactionsInLastMessage(roomId, lastMessage) {
return this.update({ _id: roomId }, { $set: { lastMessage } });
return this.update({ _id: roomId }, { $set: { 'lastMessage.reactions': lastMessage.reactions } });
}

unsetReactionsInLastMessage(roomId) {
Expand Down
4 changes: 2 additions & 2 deletions app/reactions/server/setReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export function setReaction(room, user, message, reaction, shouldReact) {
callbacks.run('unsetReaction', message._id, reaction);
callbacks.run('afterUnsetReaction', message, { user, reaction, shouldReact });
} else {
Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
Rooms.setReactionsInLastMessage(room._id, message);
}
Messages.setReactions(message._id, message.reactions);
callbacks.run('setReaction', message._id, reaction);
callbacks.run('afterSetReaction', message, { user, reaction, shouldReact });
}
Expand All @@ -73,10 +73,10 @@ export function setReaction(room, user, message, reaction, shouldReact) {
};
}
message.reactions[reaction].usernames.push(user.username);
Messages.setReactions(message._id, message.reactions);
if (isTheLastMessage(room, message)) {
Rooms.setReactionsInLastMessage(room._id, message);
}
Messages.setReactions(message._id, message.reactions);
callbacks.run('setReaction', message._id, reaction);
callbacks.run('afterSetReaction', message, { user, reaction, shouldReact });
}
Expand Down
2 changes: 1 addition & 1 deletion mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--compilers js:babel-mocha-es6-compiler
--require babel-mocha-es6-compiler
--reporter spec
--ui bdd

0 comments on commit e4b5011

Please sign in to comment.