-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathupdateMessage.ts
More file actions
130 lines (108 loc) · 3.8 KB
/
updateMessage.ts
File metadata and controls
130 lines (108 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { AppEvents, Apps } from '@rocket.chat/apps';
import { Message } from '@rocket.chat/core-services';
import type { IMessage, IUser, AtLeast } from '@rocket.chat/core-typings';
import type { Root } from '@rocket.chat/message-parser';
import { Messages, Rooms } from '@rocket.chat/models';
import { Meteor } from 'meteor/meteor';
import { settings } from '../../../settings/server';
import { afterSaveMessage } from '../lib/afterSaveMessage';
import { notifyOnRoomChangedById } from '../lib/notifyListener';
import { validateCustomMessageFields } from '../lib/validateCustomMessageFields';
export const updateMessage = async function (
{
parseUrls,
...message
}: (AtLeast<IMessage, '_id' | 'rid' | 'msg' | 'customFields'> | AtLeast<IMessage, '_id' | 'rid' | 'content'>) & {
parseUrls?: boolean;
},
user: IUser,
originalMsg?: IMessage,
previewUrls?: string[],
): Promise<void> {
const originalMessage = originalMsg || (await Messages.findOneById(message._id));
if (!originalMessage) {
throw new Error('Invalid message ID.');
}
let messageData: IMessage = Object.assign({}, originalMessage, message);
// For the Rocket.Chat Apps :)
if (message && Apps.self && Apps.isLoaded()) {
const prevent = await Apps.self?.triggerEvent(AppEvents.IPreMessageUpdatedPrevent, messageData);
if (prevent) {
throw new Meteor.Error('error-app-prevented-updating', 'A Rocket.Chat App prevented the message updating.');
}
let result = await Apps.self?.triggerEvent(AppEvents.IPreMessageUpdatedExtend, messageData);
result = await Apps.self?.triggerEvent(AppEvents.IPreMessageUpdatedModify, result);
if (typeof result === 'object') {
Object.assign(messageData, result);
}
}
// If we keep history of edits, insert a new message to store history information
if (settings.get('Message_KeepHistory')) {
await Messages.cloneAndSaveAsHistoryById(messageData._id, user as Required<Pick<IUser, '_id' | 'username' | 'name'>>);
}
Object.assign(messageData, {
editedAt: new Date(),
editedBy: {
_id: user._id,
username: user.username,
},
});
const room = await Rooms.findOneById(messageData.rid);
if (!room) {
return;
}
messageData = await Message.beforeSave({ message: messageData, room, user, previewUrls, parseUrls });
if (messageData.customFields) {
validateCustomMessageFields({
customFields: messageData.customFields,
messageCustomFieldsEnabled: settings.get<boolean>('Message_CustomFields_Enabled'),
messageCustomFields: settings.get<string>('Message_CustomFields'),
});
}
const { _id, ...editedMessage } = messageData;
if (!editedMessage.msg) {
delete editedMessage.md;
}
if (editedMessage.attachments != null) {
const attachments = Array.isArray(editedMessage.attachments) ? editedMessage.attachments : [editedMessage.attachments];
editedMessage.attachments = attachments.map((attachment) => {
let normalizedMd: Root;
if (Array.isArray(attachment.md)) {
normalizedMd = attachment.md;
} else if (attachment.md != null) {
normalizedMd = [attachment.md];
} else {
normalizedMd = [];
}
return {
...attachment,
md: normalizedMd,
};
});
}
// do not send $unset if not defined. Can cause exceptions in certain mongo versions.
await Messages.updateOne(
{ _id },
{
$set: {
...editedMessage,
},
...(!editedMessage.md && { $unset: { md: 1 } }),
},
);
if (Apps.self?.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
void Apps.self?.triggerEvent(AppEvents.IPostMessageUpdated, messageData);
}
setImmediate(async () => {
const msg = await Messages.findOneById(_id);
if (!msg) {
return;
}
await afterSaveMessage(msg, room, user);
if (room?.lastMessage?._id === msg._id) {
void notifyOnRoomChangedById(message.rid);
}
});
};