From b274169ccc119ae6f37438a077148d5608d98894 Mon Sep 17 00:00:00 2001 From: Prakhar Pratyush Date: Sat, 19 Aug 2023 02:18:22 +0530 Subject: [PATCH] util: Fix the 'find_wildcard_mentions' function. The function considers @**all**, @**everyone**, @**stream**, and @**topic** as wildcard mentions. Earlier, the function didn't have a check to match @**topic** in the message_content. --- web/src/util.ts | 2 +- web/tests/util.test.js | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/web/src/util.ts b/web/src/util.ts index 244ddc6cf6..d18d526484 100644 --- a/web/src/util.ts +++ b/web/src/util.ts @@ -193,7 +193,7 @@ export class CachedValue { } export function find_wildcard_mentions(message_content: string): string | null { - const mention = message_content.match(/(^|\s)(@\*{2}(all|everyone|stream)\*{2})($|\s)/); + const mention = message_content.match(/(^|\s)(@\*{2}(all|everyone|stream|topic)\*{2})($|\s)/); if (mention === null) { return null; } diff --git a/web/tests/util.test.js b/web/tests/util.test.js index 5dcf1c4f6e..ddcaf95038 100644 --- a/web/tests/util.test.js +++ b/web/tests/util.test.js @@ -148,7 +148,7 @@ run_test("random_int", () => { }); }); -run_test("all_and_everyone_mentions_regexp", () => { +run_test("wildcard_mentions_regexp", () => { const messages_with_all_mentions = [ "@**all**", "some text before @**all** some text after", @@ -170,6 +170,13 @@ run_test("all_and_everyone_mentions_regexp", () => { "some text before only @**stream**", ]; + const messages_with_topic_mentions = [ + "@**topic**", + "some text before @**topic** some text after", + "@**topic** some text after only", + "some text before only @**topic**", + ]; + const messages_without_all_mentions = [ "@all", "some text before @all some text after", @@ -197,6 +204,15 @@ run_test("all_and_everyone_mentions_regexp", () => { "some_email@**stream**.com", ]; + const messages_without_topic_mentions = [ + "some text before @topic some text after", + "@topic", + "`@topic`", + "some_email@topic.com", + "`@**topic**`", + "some_email@**topic**.com", + ]; + let i; for (i = 0; i < messages_with_all_mentions.length; i += 1) { assert.ok(util.find_wildcard_mentions(messages_with_all_mentions[i])); @@ -210,6 +226,10 @@ run_test("all_and_everyone_mentions_regexp", () => { assert.ok(util.find_wildcard_mentions(messages_with_stream_mentions[i])); } + for (i = 0; i < messages_with_topic_mentions.length; i += 1) { + assert.ok(util.find_wildcard_mentions(messages_with_topic_mentions[i])); + } + for (i = 0; i < messages_without_all_mentions.length; i += 1) { assert.ok(!util.find_wildcard_mentions(messages_without_everyone_mentions[i])); } @@ -221,6 +241,10 @@ run_test("all_and_everyone_mentions_regexp", () => { for (i = 0; i < messages_without_stream_mentions.length; i += 1) { assert.ok(!util.find_wildcard_mentions(messages_without_stream_mentions[i])); } + + for (i = 0; i < messages_without_topic_mentions.length; i += 1) { + assert.ok(!util.find_wildcard_mentions(messages_without_topic_mentions[i])); + } }); run_test("move_array_elements_to_front", () => {