Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Move markdown message parser to a callback #25413

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions apps/meteor/app/lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 0 additions & 13 deletions apps/meteor/app/lib/server/functions/updateMessage.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 });
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/server/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './messages/markdownParser';
import './sauMonitorHooks';
25 changes: 25 additions & 0 deletions apps/meteor/server/hooks/messages/markdownParser.ts
Original file line number Diff line number Diff line change
@@ -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',
);
}
4 changes: 2 additions & 2 deletions apps/meteor/server/modules/listeners/listeners.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down