Merge pull request #9227 from RocketChat/fix-update-last-message

Fix: updating last message on message edit or delete
pull/9258/head
Rodrigo Nascimento 8 years ago
parent 0308923a98
commit faf382f006
No known key found for this signature in database
GPG Key ID: CFCE33B7B01AC335
  1. 6
      packages/rocketchat-lib/server/functions/deleteMessage.js
  2. 6
      packages/rocketchat-lib/server/lib/notifyUsersOnMessage.js
  3. 17
      packages/rocketchat-lib/server/models/Messages.js
  4. 12
      packages/rocketchat-lib/server/models/Rooms.js

@ -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 {

@ -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;
}

@ -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);

@ -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};

Loading…
Cancel
Save