-
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.
fix: user highlights matching just part of words (#33776)
- Loading branch information
1 parent
4c4c145
commit f79f3cc
Showing
6 changed files
with
99 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@rocket.chat/meteor': patch | ||
--- | ||
|
||
Fix user highlights not matching only whole words |
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
22 changes: 22 additions & 0 deletions
22
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,22 @@ | ||
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 hl = escapeRegExp(highlight); | ||
const regexp = new RegExp(`(?<!:)\\b${hl}\\b:|:\\b${hl}(?!:)\\b|\\b(?<!:)${hl}(?!:)\\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
69 changes: 69 additions & 0 deletions
69
...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,69 @@ | ||
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; | ||
}); | ||
|
||
it('should return true if highlight case not match', async () => { | ||
const message = { | ||
msg: 'highlighted regular message', | ||
}; | ||
expect(messageContainsHighlight(message, ['ReGuLaR'])).to.be.true; | ||
}); | ||
|
||
it('should return false if the highlight word is an emoji', async () => { | ||
const message = { | ||
msg: 'highlighted :thumbsup: message', | ||
}; | ||
expect(messageContainsHighlight(message, ['thumbsup'])).to.be.false; | ||
}); | ||
|
||
it('should return true for a highlight word beggining with :', async () => { | ||
const message = { | ||
msg: 'highlighted :thumbsup message', | ||
}; | ||
expect(messageContainsHighlight(message, ['thumbsup'])).to.be.true; | ||
}); | ||
|
||
it('should return true for a highlight word ending with :', async () => { | ||
const message = { | ||
msg: 'highlighted thumbsup: message', | ||
}; | ||
expect(messageContainsHighlight(message, ['thumbsup'])).to.be.true; | ||
}); | ||
}); |