-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathprocessTooLongMessage.ts
More file actions
61 lines (50 loc) · 1.83 KB
/
processTooLongMessage.ts
File metadata and controls
61 lines (50 loc) · 1.83 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
import type { IMessage } from '@rocket.chat/core-typings';
import { GenericModal, imperativeModal } from '@rocket.chat/ui-client';
import { settings } from '../../../../app/settings/client';
import { t } from '../../../../app/utils/lib/i18n';
import { dispatchToastMessage } from '../../toast';
import type { ChatAPI } from '../ChatAPI';
export const processTooLongMessage = async (chat: ChatAPI, { msg }: Pick<IMessage, 'msg'>): Promise<boolean> => {
const maxAllowedSize = settings.get('Message_MaxAllowedSize');
if (msg.length <= maxAllowedSize) {
return false;
}
const fileUploadsEnabled = settings.get('FileUpload_Enabled');
const convertLongMessagesToAttachment = settings.get('Message_AllowConvertLongMessagesToAttachment');
if (chat.currentEditingMessage.getMID() || !fileUploadsEnabled || !convertLongMessagesToAttachment) {
dispatchToastMessage({ type: 'error', message: new Error(t('Message_too_long')) });
chat.composer?.setText(msg);
return true;
}
await new Promise<void>((resolve) => {
const onConfirm = async (): Promise<void> => {
const contentType = 'text/plain';
const messageBlob = new Blob([msg], { type: contentType });
const fileName = `${Meteor.user()?.username ?? 'anonymous'} - ${new Date()}.txt`; // TODO: proper naming and formatting
const file = new File([messageBlob], fileName, {
type: contentType,
lastModified: Date.now(),
});
imperativeModal.close();
await chat.flows.uploadFiles([file]);
resolve();
};
const onClose = (): void => {
chat.composer?.setText(msg);
imperativeModal.close();
resolve();
};
imperativeModal.open({
component: GenericModal,
props: {
title: t('Message_too_long'),
children: t('Send_it_as_attachment_instead_question'),
onConfirm,
onClose,
onCancel: onClose,
variant: 'warning',
},
});
});
return true;
};