Skip to content

Commit

Permalink
Merge pull request #9227 from RocketChat/fix-update-last-message
Browse files Browse the repository at this point in the history
Fix: updating last message on message edit or delete
  • Loading branch information
rodrigok committed Dec 26, 2017
1 parent 0308923 commit faf382f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions packages/rocketchat-lib/server/functions/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ RocketChat.deleteMessage = function(message, user) {
});
}

// update last message
if (RocketChat.settings.get('Store_Last_Message')) {
const lastMessage = RocketChat.models.Messages.getLastMessageSentWithNoTypeByRoomId(message.rid);
RocketChat.models.Rooms.setLastMessageById(message.rid, lastMessage);
}

if (showDeletedStatus) {
RocketChat.models.Messages.setAsDeletedByIdAndUser(message._id, user);
} else {
Expand Down
6 changes: 5 additions & 1 deletion packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
RocketChat.models.Rooms.incMsgCountById(message.rid, 1);
return message;
} else if (message.editedAt) {
// skips this callback if the message was edited

// only updates last message if it was edited (skip rest of callback)
if (RocketChat.settings.get('Store_Last_Message') && (!room.lastMessage || room.lastMessage._id === message._id)) {
RocketChat.models.Rooms.setLastMessageById(message.rid, message);
}
return message;
}

Expand Down
17 changes: 16 additions & 1 deletion packages/rocketchat-lib/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,26 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base {
}

findOneBySlackTs(slackTs) {
const query = {slackTs};
const query = {slackTs};

return this.findOne(query);
}

getLastMessageSentWithNoTypeByRoomId(rid) {
const query = {
rid,
t: { $exists: false }
};

const options = {
sort: {
ts: -1
}
};

return this.findOne(query, options);
}

cloneAndSaveAsHistoryById(_id) {
const me = RocketChat.models.Users.findOneById(Meteor.userId());
const record = this.findOneById(_id);
Expand Down
12 changes: 12 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,18 @@ class ModelRooms extends RocketChat.models._Base {
return this.update(query, update);
}

setLastMessageById(_id, lastMessage) {
const query = {_id};

const update = {
$set: {
lastMessage
}
};

return this.update(query, update);
}

replaceUsername(previousUsername, username) {
const query = {usernames: previousUsername};

Expand Down

0 comments on commit faf382f

Please sign in to comment.