The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-lib/server/functions/updateMessage.js

51 lines
1.9 KiB

RocketChat.updateMessage = function(message, user) {
// For the Rocket.Chat Apps :)
if (message && Apps && Apps.isLoaded()) {
const prevent = Promise.await(Apps.getBridges().getListenerBridge().messageEvent('IPreMessageUpdatedPrevent', message));
if (prevent) {
throw new Meteor.Error('error-app-prevented-updating', 'A Rocket.Chat App prevented the message updating.');
}
let result;
result = Promise.await(Apps.getBridges().getListenerBridge().messageEvent('IPreMessageUpdatedExtend', message));
result = Promise.await(Apps.getBridges().getListenerBridge().messageEvent('IPreMessageUpdatedModify', result));
if (typeof result === 'object') {
message = Object.assign(message, result);
}
}
// If we keep history of edits, insert a new message to store history information
if (RocketChat.settings.get('Message_KeepHistory')) {
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id);
}
message.editedAt = new Date();
message.editedBy = {
_id: user._id,
username: user.username,
};
const urls = message.msg.match(/([A-Za-z]{3,9}):\/\/([-;:&=\+\$,\w]+@{1})?([-A-Za-z0-9\.]+)+:?(\d+)?((\/[-\+=!:~%\/\.@\,\w]*)?\??([-\+=&!:;%@\/\.\,\w]+)?(?:#([^\s\)]+))?)?/g) || [];
message.urls = urls.map((url) => ({ url }));
message = RocketChat.callbacks.run('beforeSaveMessage', message);
const tempid = message._id;
delete message._id;
RocketChat.models.Messages.update({ _id: tempid }, { $set: message });
const room = RocketChat.models.Rooms.findOneById(message.rid);
if (Apps && Apps.isLoaded()) {
// This returns a promise, but it won't mutate anything about the message
// so, we don't really care if it is successful or fails
Apps.getBridges().getListenerBridge().messageEvent('IPostMessageUpdated', message);
}
Meteor.defer(function() {
RocketChat.callbacks.run('afterSaveMessage', RocketChat.models.Messages.findOneById(tempid), room, user._id);
});
};