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/deleteMessage.js

58 lines
1.9 KiB

import { Meteor } from 'meteor/meteor';
Convert rocketchat-file-upload to main module structure (#13094) * Move rocketchat settings to specific package * WIP: Move models from rocketchat-lib to a specific package (server) * Move function from rocketchat:lib to rocketchat:utils to use it in rocketchat:models * Move client models from rocketchat:lib to rocketchat:models * Fix lint * Move rocketchat.info from lib to utils * Remove directly dependency between lib and migrations * Move statistics Model to rocketchat:models * Create rocketchat:metrics to be able to depacking rocketchat callbacks * Move callbacks to specific package * Remove unused dependency * Move rocketchat-notifications to a specific package * Move rocketchat-promises to a specific package * remove directly dependency from metrics and models * Move CachedCollection from lib to models * Move ui models/collections from ui to models * Move authorization client/ui models to rocketchat:models to be able to remove lib dependency * Creation of rocketchat:ui-utils to help decouple rocketchat:lib and rocketchat:authz * Move some common functions to rocketchat:utils * Change imports to dynamic imports to avoid directly dependency between some packages * Move authz models to rocketchat:models * Remove directly dependency between rocketchat:authz and rocketchat:lib * Move some functions from rocketchat:lib to rocketchat:utils * Add functions to settings package * Convert rocketchat:file-upload to main module structure * Import FileUpload where it is being used * Remove FileUpload and fileUploadHandler from globals eslintrc * Merge branch 'develop' into globals/move-rocketchat-callbacks * Fix missed export * Fix canBeDeleted and canBeCreated function, remove async
7 years ago
import { FileUpload } from 'meteor/rocketchat:file-upload';
RocketChat.deleteMessage = function(message, user) {
const keepHistory = RocketChat.settings.get('Message_KeepHistory');
const showDeletedStatus = RocketChat.settings.get('Message_ShowDeletedStatus');
const deletedMsg = RocketChat.models.Messages.findOneById(message._id);
if (deletedMsg && Apps && Apps.isLoaded()) {
const prevent = Promise.await(Apps.getBridges().getListenerBridge().messageEvent('IPreMessageDeletePrevent', deletedMsg));
if (prevent) {
throw new Meteor.Error('error-app-prevented-deleting', 'A Rocket.Chat App prevented the message deleting.');
}
}
if (keepHistory) {
if (showDeletedStatus) {
RocketChat.models.Messages.cloneAndSaveAsHistoryById(message._id);
} else {
RocketChat.models.Messages.setHiddenById(message._id, true);
}
if (message.file && message.file._id) {
RocketChat.models.Uploads.update(message.file._id, { $set: { _hidden: true } });
}
} else {
if (!showDeletedStatus) {
RocketChat.models.Messages.removeById(message._id);
}
if (message.file && message.file._id) {
FileUpload.getStore('Uploads').deleteById(message.file._id);
}
}
Meteor.defer(function() {
RocketChat.callbacks.run('afterDeleteMessage', deletedMsg);
});
// update last message
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) {
RocketChat.models.Rooms.resetLastMessageById(message.rid, message._id);
}
}
if (showDeletedStatus) {
RocketChat.models.Messages.setAsDeletedByIdAndUser(message._id, user);
} else {
RocketChat.Notifications.notifyRoom(message.rid, 'deleteMessage', { _id: message._id });
}
if (Apps && Apps.isLoaded()) {
Apps.getBridges().getListenerBridge().messageEvent('IPostMessageDeleted', deletedMsg);
}
};