-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66e4fac
commit 15e56df
Showing
5 changed files
with
65 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
apps/meteor/app/lib/server/functions/notifications/messageContainsHighlight.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IMessage, 'msg'>, 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); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...eteor/tests/unit/app/lib/server/functions/notifications/messageContainsHighlight.tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
}); |