Skip to content

Commit

Permalink
Merge pull request #9335 from RocketChat/fix-not-updating-last-message
Browse files Browse the repository at this point in the history
[FIX] Deleting message with store last message not removing
  • Loading branch information
rodrigok authored Jan 5, 2018
2 parents 9dc9db3 + eddd352 commit 987b1e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 5 additions & 2 deletions packages/rocketchat-lib/server/functions/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ 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);
const room = RocketChat.models.Rooms.findOneById(message.rid, { fields: { lastMessage: 1 } });
if (!room.lastMessage || room.lastMessage._id === message._id) {
const lastMessage = RocketChat.models.Messages.getLastVisibleMessageSentWithNoTypeByRoomId(message.rid, message._id);
RocketChat.models.Rooms.setLastMessageById(message.rid, lastMessage);
}
}

if (showDeletedStatus) {
Expand Down
7 changes: 6 additions & 1 deletion packages/rocketchat-lib/server/models/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,17 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base {
return this.findOne(query);
}

getLastMessageSentWithNoTypeByRoomId(rid) {
getLastVisibleMessageSentWithNoTypeByRoomId(rid, messageId) {
const query = {
rid,
_hidden: { $ne: true },
t: { $exists: false }
};

if (messageId) {
query._id = { $ne: messageId };
}

const options = {
sort: {
ts: -1
Expand Down

0 comments on commit 987b1e1

Please sign in to comment.