From eddd352f679ca4d2979d9df93016b5082b1f6d6c Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 4 Jan 2018 18:14:44 -0200 Subject: [PATCH] Fix updating last message when showing deleted status Closes #9332 --- packages/rocketchat-lib/server/functions/deleteMessage.js | 7 +++++-- packages/rocketchat-lib/server/models/Messages.js | 7 ++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/rocketchat-lib/server/functions/deleteMessage.js b/packages/rocketchat-lib/server/functions/deleteMessage.js index aa7101cbfdf5..9111557397d1 100644 --- a/packages/rocketchat-lib/server/functions/deleteMessage.js +++ b/packages/rocketchat-lib/server/functions/deleteMessage.js @@ -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) { diff --git a/packages/rocketchat-lib/server/models/Messages.js b/packages/rocketchat-lib/server/models/Messages.js index 3adbe4ad4c64..90f8bf068765 100644 --- a/packages/rocketchat-lib/server/models/Messages.js +++ b/packages/rocketchat-lib/server/models/Messages.js @@ -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