-
Notifications
You must be signed in to change notification settings - Fork 10k
/
IMessage.ts
338 lines (289 loc) · 8.49 KB
/
IMessage.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import type { UrlWithStringQuery } from 'url';
import type Icons from '@rocket.chat/icons';
import type { MessageSurfaceLayout } from '@rocket.chat/ui-kit';
import type { Root } from '@rocket.chat/message-parser';
import type { IRocketChatRecord } from '../IRocketChatRecord';
import type { IUser } from '../IUser';
import type { IRoom, RoomID } from '../IRoom';
import type { MessageAttachment } from './MessageAttachment/MessageAttachment';
import type { FileProp } from './MessageAttachment/Files/FileProp';
import type { ILivechatVisitor } from '../ILivechatVisitor';
type MentionType = 'user' | 'team';
type MessageUrl = {
url: string;
source?: string;
meta: Record<string, string>;
headers?: { contentLength: string } | { contentType: string } | { contentLength: string; contentType: string };
ignoreParse?: boolean;
parsedUrl?: Pick<UrlWithStringQuery, 'host' | 'hash' | 'pathname' | 'protocol' | 'port' | 'query' | 'search' | 'hostname'>;
};
type VoipMessageTypesValues =
| 'voip-call-started'
| 'voip-call-declined'
| 'voip-call-on-hold'
| 'voip-call-unhold'
| 'voip-call-ended'
| 'voip-call-duration'
| 'voip-call-wrapup'
| 'voip-call-ended-unexpectedly';
type TeamMessageTypes =
| 'removed-user-from-team'
| 'added-user-to-team'
| 'ult'
| 'user-converted-to-team'
| 'user-converted-to-channel'
| 'user-removed-room-from-team'
| 'user-deleted-room-from-team'
| 'user-added-room-to-team'
| 'ujt';
type LivechatMessageTypes =
| 'livechat_navigation_history'
| 'livechat_transfer_history'
| 'livechat_transcript_history'
| 'livechat_video_call'
| 'livechat_transfer_history_fallback'
| 'livechat-close'
| 'livechat_webrtc_video_call'
| 'livechat-started';
type OmnichannelTypesValues = 'omnichannel_placed_chat_on_hold' | 'omnichannel_on_hold_chat_resumed';
type OtrMessageTypeValues = 'otr' | 'otr-ack';
type OtrSystemMessages = 'user_joined_otr' | 'user_requested_otr_key_refresh' | 'user_key_refreshed_successfully';
export type MessageTypesValues =
| 'e2e'
| 'uj'
| 'ul'
| 'ru'
| 'au'
| 'mute_unmute'
| 'r'
| 'ut'
| 'wm'
| 'rm'
| 'subscription-role-added'
| 'subscription-role-removed'
| 'room-archived'
| 'room-unarchived'
| 'room_changed_privacy'
| 'room_changed_description'
| 'room_changed_announcement'
| 'room_changed_avatar'
| 'room_changed_topic'
| 'room_e2e_enabled'
| 'room_e2e_disabled'
| 'user-muted'
| 'user-unmuted'
| 'room-removed-read-only'
| 'room-set-read-only'
| 'room-allowed-reacting'
| 'room-disallowed-reacting'
| 'command'
| 'videoconf'
| LivechatMessageTypes
| TeamMessageTypes
| VoipMessageTypesValues
| OmnichannelTypesValues
| OtrMessageTypeValues
| OtrSystemMessages;
export type TokenType = 'code' | 'inlinecode' | 'bold' | 'italic' | 'strike' | 'link';
export type Token = {
token: string;
text: string;
type?: TokenType;
noHtml?: string;
} & TokenExtra;
export type TokenExtra = {
highlight?: boolean;
noHtml?: string;
};
export interface IMessage extends IRocketChatRecord {
rid: RoomID;
msg: string;
tmid?: string;
tshow?: boolean;
ts: Date;
mentions?: ({
type: MentionType;
} & Pick<IUser, '_id' | 'username' | 'name'>)[];
groupable?: false;
channels?: Pick<IRoom, '_id' | 'name'>[];
u: Required<Pick<IUser, '_id' | 'username' | 'name'>>;
blocks?: MessageSurfaceLayout;
alias?: string;
md?: Root;
_hidden?: boolean;
imported?: boolean;
replies?: IUser['_id'][];
location?: {
type: 'Point';
coordinates: [number, number];
};
starred?: { _id: IUser['_id'] }[];
pinned?: boolean;
unread?: boolean;
temp?: boolean;
drid?: RoomID;
tlm?: Date;
dcount?: number;
tcount?: number;
t?: MessageTypesValues;
e2e?: 'pending' | 'done';
otrAck?: string;
urls?: MessageUrl[];
/** @deprecated Deprecated */
actionLinks?: {
icon: keyof typeof Icons;
i18nLabel: unknown;
label: string;
method_id: string;
params: string;
}[];
/** @deprecated Deprecated in favor of files */
file?: FileProp;
files?: FileProp[];
attachments?: MessageAttachment[];
reactions?: {
[key: string]: { names?: (string | undefined)[]; usernames: string[]; federationReactionEventIds?: Record<string, string> };
};
private?: boolean;
/* @deprecated */
bot?: boolean;
sentByEmail?: boolean;
webRtcCallEndTs?: Date;
role?: string;
avatar?: string;
emoji?: string;
// Tokenization fields
tokens?: Token[];
html?: string;
// Messages sent from visitors have this field
token?: string;
federation?: {
eventId: string;
};
}
export type MessageSystem = {
t: 'system';
};
export interface IEditedMessage extends IMessage {
editedAt: Date;
editedBy: Pick<IUser, '_id' | 'username'>;
}
export const isEditedMessage = (message: IMessage): message is IEditedMessage => 'editedAt' in message && 'editedBy' in message;
export const isDeletedMessage = (message: IMessage): message is IEditedMessage =>
'editedAt' in message && 'editedBy' in message && message.t === 'rm';
export const isMessageFromMatrixFederation = (message: IMessage): boolean =>
'federation' in message && Boolean(message.federation?.eventId);
export interface ITranslatedMessage extends IMessage {
translations: { [key: string]: string } & { original?: string };
translationProvider: string;
autoTranslateShowInverse?: boolean;
autoTranslateFetching?: boolean;
}
export const isTranslatedMessage = (message: IMessage): message is ITranslatedMessage => 'translations' in message;
export interface IThreadMainMessage extends IMessage {
tcount: number;
tlm: Date;
replies: IUser['_id'][];
}
export interface IThreadMessage extends IMessage {
tmid: string;
renderedOnThread?: boolean;
}
export const isThreadMainMessage = (message: IMessage): message is IThreadMainMessage => 'tcount' in message && 'tlm' in message;
export const isThreadMessage = (message: IMessage): message is IThreadMessage => !!message.tmid;
export interface IDiscussionMessage extends IMessage {
drid: string;
dlm?: Date;
dcount: number;
}
export const isDiscussionMessage = (message: IMessage): message is IDiscussionMessage => !!message.drid;
export interface IPrivateMessage extends IMessage {
private: true;
}
export const isPrivateMessage = (message: IMessage): message is IPrivateMessage => !!message.private;
export interface IMessageReactionsNormalized extends IMessage {
reactions: {
[key: string]: {
usernames: Required<IUser['_id']>[];
names: Required<IUser>['name'][];
};
};
}
export const isMessageReactionsNormalized = (message: IMessage): message is IMessageReactionsNormalized =>
Boolean('reactions' in message && message.reactions && message.reactions[0] && 'names' in message.reactions[0]);
export interface IOmnichannelSystemMessage extends IMessage {
navigation?: {
page: {
title: string;
location: {
href: string;
};
token?: string;
};
};
transferData?: {
comment: string;
transferredBy: {
name?: string;
username: string;
};
transferredTo: {
name?: string;
username: string;
};
nextDepartment?: {
_id: string;
name?: string;
};
scope: 'department' | 'agent' | 'queue';
};
requestData?: {
type: 'visitor' | 'user';
visitor?: ILivechatVisitor;
user?: IUser;
};
webRtcCallEndTs?: Date;
comment?: string;
}
export type IVoipMessage = IMessage & {
voipData: {
callDuration?: number;
callStarted?: string;
callWaitingTime?: string;
};
};
export interface IMessageDiscussion extends IMessage {
drid: RoomID;
}
export const isMessageDiscussion = (message: IMessage): message is IMessageDiscussion => {
return 'drid' in message;
};
export type IMessageEdited = IMessage & {
editedAt: Date;
editedBy: Pick<IUser, '_id' | 'username'>;
};
export const isMessageEdited = (message: IMessage): message is IMessageEdited => {
return 'editedAt' in message && 'editedBy' in message;
};
export type IMessageInbox = IMessage & {
// email inbox fields
email?: {
references?: string[];
messageId?: string;
};
};
export const isIMessageInbox = (message: IMessage): message is IMessageInbox => 'email' in message;
export const isVoipMessage = (message: IMessage): message is IVoipMessage => 'voipData' in message;
export type IE2EEMessage = IMessage & {
t: 'e2e';
e2e: 'pending' | 'done';
};
export type IOTRMessage = IMessage & {
t: 'otr' | 'otr-ack';
};
export type IVideoConfMessage = IMessage & {
t: 'videoconf';
};
export const isE2EEMessage = (message: IMessage): message is IE2EEMessage => message.t === 'e2e';
export const isOTRMessage = (message: IMessage): message is IOTRMessage => message.t === 'otr' || message.t === 'otr-ack';
export const isVideoConfMessage = (message: IMessage): message is IVideoConfMessage => message.t === 'videoconf';