diff --git a/apps/meteor/app/lib/server/functions/notifications/index.ts b/apps/meteor/app/lib/server/functions/notifications/index.ts index 200d028c0aee8..538ff2ad5ed91 100644 --- a/apps/meteor/app/lib/server/functions/notifications/index.ts +++ b/apps/meteor/app/lib/server/functions/notifications/index.ts @@ -47,22 +47,3 @@ export function replaceMentionedUsernamesWithFullNames(message: string, mentions }); return message; } - -/** - * Checks if a message contains a user highlight - * - * @param {string} message - * @param {array|undefined} highlights - * - * @returns {boolean} - */ -export function messageContainsHighlight(message: Pick, highlights: string[] | undefined): boolean { - if (!highlights || highlights.length === 0) { - return false; - } - - return highlights.some((highlight: string) => { - const regexp = new RegExp(`\\b${escapeRegExp(highlight)}\\b`, 'i'); - return regexp.test(message.msg); - }); -} diff --git a/apps/meteor/app/lib/server/functions/notifications/messageContainsHighlight.ts b/apps/meteor/app/lib/server/functions/notifications/messageContainsHighlight.ts new file mode 100644 index 0000000000000..e95930172f169 --- /dev/null +++ b/apps/meteor/app/lib/server/functions/notifications/messageContainsHighlight.ts @@ -0,0 +1,21 @@ +import type { IMessage } from '@rocket.chat/core-typings'; +import { escapeRegExp } from '@rocket.chat/string-helpers'; + +/** + * Checks if a message contains a user highlight + * + * @param {string} message + * @param {array|undefined} highlights + * + * @returns {boolean} + */ +export function messageContainsHighlight(message: Pick, highlights: string[] | undefined): boolean { + if (!highlights || highlights.length === 0) { + return false; + } + + return highlights.some((highlight: string) => { + const regexp = new RegExp(`\\b${escapeRegExp(highlight)}\\b`, 'i'); + return regexp.test(message.msg); + }); +} diff --git a/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.ts b/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.ts index dcbabf9fba4d0..e729b9652cb67 100644 --- a/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.ts +++ b/apps/meteor/app/lib/server/lib/notifyUsersOnMessage.ts @@ -6,7 +6,7 @@ import moment from 'moment'; import { callbacks } from '../../../../lib/callbacks'; import { settings } from '../../../settings/server'; -import { messageContainsHighlight } from '../functions/notifications'; +import { messageContainsHighlight } from '../functions/notifications/messageContainsHighlight'; import { notifyOnSubscriptionChanged, notifyOnSubscriptionChangedByRoomIdAndUserId, diff --git a/apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts b/apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts index 94c25f4762220..fe65ebe5a5d24 100644 --- a/apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts +++ b/apps/meteor/app/lib/server/lib/sendNotificationsOnMessage.ts @@ -17,9 +17,10 @@ import { roomCoordinator } from '../../../../server/lib/rooms/roomCoordinator'; import { hasPermissionAsync } from '../../../authorization/server/functions/hasPermission'; import { Notification } from '../../../notification-queue/server/NotificationQueue'; import { settings } from '../../../settings/server'; -import { messageContainsHighlight, parseMessageTextPerUser, replaceMentionedUsernamesWithFullNames } from '../functions/notifications'; +import { parseMessageTextPerUser, replaceMentionedUsernamesWithFullNames } from '../functions/notifications'; import { notifyDesktopUser, shouldNotifyDesktop } from '../functions/notifications/desktop'; import { getEmailData, shouldNotifyEmail } from '../functions/notifications/email'; +import { messageContainsHighlight } from '../functions/notifications/messageContainsHighlight'; import { getPushData, shouldNotifyMobile } from '../functions/notifications/mobile'; import { getMentions } from './notifyUsersOnMessage'; diff --git a/apps/meteor/tests/unit/app/lib/server/functions/notifications/messageContainsHighlight.tests.ts b/apps/meteor/tests/unit/app/lib/server/functions/notifications/messageContainsHighlight.tests.ts new file mode 100644 index 0000000000000..ab813029785fa --- /dev/null +++ b/apps/meteor/tests/unit/app/lib/server/functions/notifications/messageContainsHighlight.tests.ts @@ -0,0 +1,41 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import { messageContainsHighlight } from '../../../../../../../app/lib/server/functions/notifications/messageContainsHighlight'; + +describe('messageContainsHighlight', () => { + it('should return false for no highlights', async () => { + const message = { + msg: 'regular message', + }; + expect(messageContainsHighlight(message, [])).to.be.false; + }); + + it('should return true when find a highlight in the beggining of the message', async () => { + const message = { + msg: 'highlighted regular message', + }; + expect(messageContainsHighlight(message, ['highlighted'])).to.be.true; + }); + + it('should return true when find a highlight in the end of the message', async () => { + const message = { + msg: 'highlighted regular message', + }; + expect(messageContainsHighlight(message, ['message'])).to.be.true; + }); + + it('should return false if the highlight is just part of the word', async () => { + const message = { + msg: 'highlighted regular message', + }; + expect(messageContainsHighlight(message, ['light'])).to.be.false; + }); + + it('should return true if find one of the multiple highlights', async () => { + const message = { + msg: 'highlighted regular message', + }; + expect(messageContainsHighlight(message, ['high', 'ssage', 'regular', 'light'])).to.be.true; + }); +});