diff --git a/apps/meteor/app/lib/server/functions/sendMessage.js b/apps/meteor/app/lib/server/functions/sendMessage.js index 37b7f332e896..cb389e13a66c 100644 --- a/apps/meteor/app/lib/server/functions/sendMessage.js +++ b/apps/meteor/app/lib/server/functions/sendMessage.js @@ -1,5 +1,4 @@ import { Match, check } from 'meteor/check'; -import { parser } from '@rocket.chat/message-parser'; import { settings } from '../../../settings'; import { callbacks } from '../../../../lib/callbacks'; @@ -10,9 +9,6 @@ import { FileUpload } from '../../../file-upload/server'; import { hasPermission } from '../../../authorization/server'; import { SystemLogger } from '../../../../server/lib/logger/system'; import { parseUrlsInMessage } from './parseUrlsInMessage'; -import { isE2EEMessage } from '../../../../lib/isE2EEMessage'; - -const { DISABLE_MESSAGE_PARSER = 'false' } = process.env; /** * IMPORTANT @@ -246,13 +242,6 @@ export const sendMessage = function (user, message, room, upsert = false) { parseUrlsInMessage(message); message = callbacks.run('beforeSaveMessage', message, room); - try { - if (message.msg && DISABLE_MESSAGE_PARSER !== 'true' && !isE2EEMessage(message)) { - message.md = parser(message.msg); - } - } catch (e) { - SystemLogger.error(e); // errors logged while the parser is at experimental stage - } if (message) { if (message._id && upsert) { const { _id } = message; diff --git a/apps/meteor/app/lib/server/functions/updateMessage.ts b/apps/meteor/app/lib/server/functions/updateMessage.ts index ef0602995747..410c7f276f5d 100644 --- a/apps/meteor/app/lib/server/functions/updateMessage.ts +++ b/apps/meteor/app/lib/server/functions/updateMessage.ts @@ -1,16 +1,11 @@ import { Meteor } from 'meteor/meteor'; -import { parser } from '@rocket.chat/message-parser'; import type { IMessage, IMessageEdited, IUser } from '@rocket.chat/core-typings'; import { Messages, Rooms } from '../../../models/server'; import { settings } from '../../../settings/server'; import { callbacks } from '../../../../lib/callbacks'; -import { SystemLogger } from '../../../../server/lib/logger/system'; import { Apps } from '../../../apps/server'; import { parseUrlsInMessage } from './parseUrlsInMessage'; -import { isE2EEMessage } from '../../../../lib/isE2EEMessage'; - -const { DISABLE_MESSAGE_PARSER = 'false' } = process.env; export const updateMessage = function (message: IMessage, user: IUser, originalMessage?: IMessage): void { if (!originalMessage) { @@ -50,14 +45,6 @@ export const updateMessage = function (message: IMessage, user: IUser, originalM message = callbacks.run('beforeSaveMessage', message); - try { - if (message.msg && DISABLE_MESSAGE_PARSER !== 'true' && !isE2EEMessage(message)) { - message.md = parser(message.msg); - } - } catch (e: unknown) { - SystemLogger.error(String(e)); // errors logged while the parser is at experimental stage - } - const { _id, ...editedMessage } = message; Messages.update({ _id }, { $set: editedMessage }); diff --git a/apps/meteor/lib/callbacks.ts b/apps/meteor/lib/callbacks.ts index cee171c6260c..c1ad1c3ca34d 100644 --- a/apps/meteor/lib/callbacks.ts +++ b/apps/meteor/lib/callbacks.ts @@ -62,6 +62,7 @@ type EventLikeCallbackSignatures = { * TODO: develop a middleware alternative and grant independence of execution order */ type ChainedCallbackSignatures = { + 'beforeSaveMessage': (message: IMessage, room?: IRoom) => IMessage; 'afterCreateUser': (user: IUser) => IUser; 'afterDeleteRoom': (rid: IRoom['_id']) => IRoom['_id']; 'livechat:afterOnHold': (room: IRoom) => IRoom; diff --git a/apps/meteor/server/hooks/index.ts b/apps/meteor/server/hooks/index.ts index 64d500b223f2..28de58998534 100644 --- a/apps/meteor/server/hooks/index.ts +++ b/apps/meteor/server/hooks/index.ts @@ -1 +1,2 @@ +import './messages/markdownParser'; import './sauMonitorHooks'; diff --git a/apps/meteor/server/hooks/messages/markdownParser.ts b/apps/meteor/server/hooks/messages/markdownParser.ts new file mode 100644 index 000000000000..384c304e4a7f --- /dev/null +++ b/apps/meteor/server/hooks/messages/markdownParser.ts @@ -0,0 +1,25 @@ +import { parser } from '@rocket.chat/message-parser'; + +import { callbacks } from '../../../lib/callbacks'; +import { isE2EEMessage } from '../../../lib/isE2EEMessage'; +import { SystemLogger } from '../../lib/logger/system'; + +if (process.env.DISABLE_MESSAGE_PARSER !== 'true') { + callbacks.add( + 'beforeSaveMessage', + (message) => { + if (!message.msg || isE2EEMessage(message)) { + return message; + } + try { + message.md = parser(message.msg); + } catch (e) { + SystemLogger.error(e); // errors logged while the parser is at experimental stage + } + + return message; + }, + callbacks.priority.HIGH, + 'markdownParser', + ); +} diff --git a/apps/meteor/server/modules/listeners/listeners.module.ts b/apps/meteor/server/modules/listeners/listeners.module.ts index 386ecf6354da..01417179b445 100644 --- a/apps/meteor/server/modules/listeners/listeners.module.ts +++ b/apps/meteor/server/modules/listeners/listeners.module.ts @@ -5,7 +5,7 @@ import { IServiceClass } from '../../sdk/types/ServiceClass'; import { NotificationsModule } from '../notifications/notifications.module'; import { EnterpriseSettings } from '../../sdk/index'; -const { DISABLE_MESSAGE_PARSER = 'false' } = process.env; +const isMessageParserDisabled = process.env.DISABLE_MESSAGE_PARSER === 'true'; const STATUS_MAP: { [k: string]: number } = { [UserStatus.OFFLINE]: 0, @@ -35,7 +35,7 @@ export class ListenersModule { }); service.onEvent('notify.ephemeralMessage', (uid, rid, message) => { - if (message.msg && DISABLE_MESSAGE_PARSER !== 'true') { + if (!isMessageParserDisabled && message.msg) { message.md = parser(message.msg); }