Skip to content

Commit

Permalink
[FIX] Regression in prune by user, and update lastMessage (#11646)
Browse files Browse the repository at this point in the history
* Fix regression

* Also update lastMessage

* code style fix
  • Loading branch information
vynmera authored and sampaiodiego committed Aug 10, 2018
1 parent 90be09e commit e3df820
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 579 deletions.
8 changes: 2 additions & 6 deletions packages/rocketchat-lib/server/functions/cleanRoomHistory.js
Expand Up @@ -24,14 +24,10 @@ RocketChat.cleanRoomHistory = function({ rid, latest = new Date(), oldest = new
return fileCount;
}

let count = 0;
if (limit) {
count = RocketChat.models.Messages.removeByIdPinnedTimestampLimitAndUsers(rid, excludePinned, ts, limit, fromUsers);
} else {
count = RocketChat.models.Messages.removeByIdPinnedTimestampAndUsers(rid, excludePinned, ts, fromUsers);
}
const count = limit ? RocketChat.models.Messages.removeByIdPinnedTimestampLimitAndUsers(rid, excludePinned, ts, limit, fromUsers) : RocketChat.models.Messages.removeByIdPinnedTimestampAndUsers(rid, excludePinned, ts, fromUsers);

if (count) {
RocketChat.models.Rooms.resetLastMessageById(rid);
RocketChat.Notifications.notifyRoom(rid, 'deleteMessageBulk', {
rid,
excludePinned,
Expand Down
3 changes: 1 addition & 2 deletions packages/rocketchat-lib/server/functions/deleteMessage.js
Expand Up @@ -39,8 +39,7 @@ RocketChat.deleteMessage = function(message, user) {
if (RocketChat.settings.get('Store_Last_Message')) {
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);
RocketChat.models.Rooms.resetLastMessageById(message.rid);
}
}

Expand Down
17 changes: 17 additions & 0 deletions packages/rocketchat-lib/server/models/Rooms.js
Expand Up @@ -439,6 +439,23 @@ class ModelRooms extends RocketChat.models._Base {
return this.update(query, update);
}

resetLastMessageById(_id) {
const query = { _id };
const lastMessage = RocketChat.models.Messages.getLastVisibleMessageSentWithNoTypeByRoomId(_id);

const update = lastMessage ? {
$set: {
lastMessage
}
} : {
$unset: {
lastMessage: 1
}
};

return this.update(query, update);
}

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

Expand Down

0 comments on commit e3df820

Please sign in to comment.