Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

mention: Determine @topic mention during message rendering.

Prakhar Pratyush0891f9f65abf
diff --git a/web/src/markdown.js b/web/src/markdown.js
index 86d3447b7ad..4cf56fa76d9 100644
--- a/web/src/markdown.js
+++ b/web/src/markdown.js
@@ -125,6 +125,20 @@ function parse_with_options({raw_content, helper_config, options}) {
return `<span class="${classes}" data-user-id="*">${_.escape(display_text)}</span>`;
}
+ if (mention === "topic") {
+ let classes;
+ let display_text;
+ if (silently) {
+ classes = "topic-mention silent";
+ display_text = mention;
+ } else {
+ // Topic Wildcard mention
+ display_text = "@" + mention;
+ classes = "topic-mention";
+ }
+
+ return `<span class="${classes}">${_.escape(display_text)}</span>`;
+ }
let full_name;
let user_id;
@@ -231,12 +245,15 @@ function parse_with_options({raw_content, helper_config, options}) {
return undefined;
},
silencedMentionHandler(quote) {
- // Silence quoted mentions.
+ // Silence quoted personal and stream wildcard mentions.
quote = quote.replaceAll(
/(<span class="user-mention)(" data-user-id="(\d+|\*)">)@/g,
"$1 silent$2",
);
+ // Silence quoted topic wildcard mentions.
+ quote = quote.replaceAll(/(<span class="topic-mention)(">)@/g, "$1 silent$2");
+
// Silence quoted user group mentions.
quote = quote.replaceAll(
/(<span class="user-group-mention)(" data-user-group-id="\d+">)@/g,
diff --git a/web/styles/rendered_markdown.css b/web/styles/rendered_markdown.css
index 719564c4c67..42d8ae4d8c4 100644
--- a/web/styles/rendered_markdown.css
+++ b/web/styles/rendered_markdown.css
@@ -161,7 +161,8 @@
}
.user-group-mention,
- .user-mention {
+ .user-mention,
+ .topic-mention {
font-size: 14px;
line-height: 17px;
padding: 0 3px;
@@ -170,7 +171,8 @@
display: inline-block;
}
- .user-mention {
+ .user-mention,
+ .topic-mention {
color: var(--color-text-other-mention);
background-color: var(--color-background-text-direct-mention);
diff --git a/web/tests/markdown.test.js b/web/tests/markdown.test.js
index 93902eb5127..460b60abd2c 100644
--- a/web/tests/markdown.test.js
+++ b/web/tests/markdown.test.js
@@ -420,6 +420,21 @@ test("marked", () => {
expected:
'<blockquote>\n<p>Stream Wildcard mention in quote: <span class="user-mention silent" data-user-id="*">all</span></p>\n</blockquote>\n<blockquote>\n<p>Another stream wildcard mention in quote: <span class="user-mention silent" data-user-id="*">all</span></p>\n</blockquote>',
},
+ {
+ input: "Topic Wildcard mention: @**topic**\nTopic Wildcard silent mention: @_**topic**",
+ expected:
+ '<p>Topic Wildcard mention: <span class="topic-mention">@topic</span><br>\nTopic Wildcard silent mention: <span class="topic-mention silent">topic</span></p>',
+ },
+ {
+ input: "> Topic Wildcard mention in quote: @**topic**\n\n> Another topic wildcard mention in quote: @_**topic**",
+ expected:
+ '<blockquote>\n<p>Topic Wildcard mention in quote: <span class="topic-mention silent">topic</span></p>\n</blockquote>\n<blockquote>\n<p>Another topic wildcard mention in quote: <span class="topic-mention silent">topic</span></p>\n</blockquote>',
+ },
+ {
+ input: "```quote\nTopic Wildcard mention in quote: @**topic**\n```\n\n```quote\nAnother topic wildcard mention in quote: @_**topic**\n```",
+ expected:
+ '<blockquote>\n<p>Topic Wildcard mention in quote: <span class="topic-mention silent">topic</span></p>\n</blockquote>\n<blockquote>\n<p>Another topic wildcard mention in quote: <span class="topic-mention silent">topic</span></p>\n</blockquote>',
+ },
{
input: "User group mention: @*backend*\nUser group silent mention: @_*hamletcharacters*",
expected:
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.py
index 68b8d1e7f00..5e8b750adab 100644
--- a/zerver/lib/markdown/__init__.py
+++ b/zerver/lib/markdown/__init__.py
@@ -113,6 +113,7 @@ class LinkInfo(TypedDict):
@dataclass
class MessageRenderingResult:
rendered_content: str
+ mentions_topic_wildcard: bool
mentions_stream_wildcard: bool
mentions_user_ids: Set[int]
mentions_user_group_ids: Set[int]
@@ -1777,6 +1778,7 @@ class UserMentionPattern(CompiledInlineProcessor):
silent = m.group("silent") == "_"
db_data: Optional[DbData] = self.zmd.zulip_db_data
if db_data is not None:
+ topic_wildcard = mention.user_mention_matches_topic_wildcard(name)
stream_wildcard = mention.user_mention_matches_stream_wildcard(name)
# For @**|id** and @**name|id** mention syntaxes.
@@ -1796,10 +1798,14 @@ class UserMentionPattern(CompiledInlineProcessor):
# For @**name** syntax.
user = db_data.mention_data.get_user_by_name(name)
+ user_id = None
if stream_wildcard:
if not silent:
self.zmd.zulip_rendering_result.mentions_stream_wildcard = True
user_id = "*"
+ elif topic_wildcard:
+ if not silent:
+ self.zmd.zulip_rendering_result.mentions_topic_wildcard = True
elif user is not None:
assert isinstance(user, FullNameInfo)
@@ -1812,13 +1818,16 @@ class UserMentionPattern(CompiledInlineProcessor):
return None, None, None
el = Element("span")
- el.set("data-user-id", user_id)
- text = f"{name}"
- if silent:
- el.set("class", "user-mention silent")
+ if user_id:
+ el.set("data-user-id", user_id)
+ text = f"@{name}"
+ if topic_wildcard:
+ el.set("class", "topic-mention")
else:
el.set("class", "user-mention")
- text = f"@{text}"
+ if silent:
+ el.set("class", el.get("class", "") + " silent")
+ text = f"{name}"
el.text = markdown.util.AtomicString(text)
return el, m.start(), m.end()
return None, None, None
@@ -2492,6 +2501,7 @@ def do_convert(
# Filters such as UserMentionPattern need a message.
rendering_result: MessageRenderingResult = MessageRenderingResult(
rendered_content="",
+ mentions_topic_wildcard=False,
mentions_stream_wildcard=False,
mentions_user_ids=set(),
mentions_user_group_ids=set(),
diff --git a/zerver/lib/mention.py b/zerver/lib/mention.py
index 55f5ff77e06..56a4768396e 100644
--- a/zerver/lib/mention.py
+++ b/zerver/lib/mention.py
@@ -150,6 +150,10 @@ class MentionBackend:
return result
+def user_mention_matches_topic_wildcard(mention: str) -> bool:
+ return mention in topic_wildcards
+
+
def user_mention_matches_stream_wildcard(mention: str) -> bool:
return mention in stream_wildcards
diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.py
index e61fde60a17..2e17370ce79 100644
--- a/zerver/tests/test_markdown.py
+++ b/zerver/tests/test_markdown.py
@@ -51,6 +51,7 @@ from zerver.lib.mention import (
possible_mentions,
possible_user_group_mentions,
stream_wildcards,
+ topic_wildcards,
)
from zerver.lib.message import render_markdown
from zerver.lib.test_classes import ZulipTestCase
@@ -1826,6 +1827,20 @@ class MarkdownTest(ZulipTestCase):
with_language, without_language = re.findall(r"<pre>(.*?)$", rendered, re.MULTILINE)
self.assertFalse(with_language == without_language)
+ def test_mention_topic_wildcard(self) -> None:
+ user_profile = self.example_user("othello")
+ msg = Message(sender=user_profile, sending_client=get_client("test"))
+
+ for topic_wildcard in topic_wildcards:
+ content = f"@**{topic_wildcard}** test"
+ rendering_result = render_markdown(msg, content)
+ self.assertEqual(
+ rendering_result.rendered_content,
+ f'<p><span class="topic-mention">@{topic_wildcard}</span> test</p>',
+ )
+ self.assertTrue(rendering_result.mentions_topic_wildcard)
+ self.assertFalse(rendering_result.mentions_stream_wildcard)
+
def test_mention_stream_wildcard(self) -> None:
user_profile = self.example_user("othello")
msg = Message(sender=user_profile, sending_client=get_client("test"))
@@ -1837,8 +1852,21 @@ class MarkdownTest(ZulipTestCase):
rendering_result.rendered_content,
f'<p><span class="user-mention" data-user-id="*">@{stream_wildcard}</span> test</p>',
)
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
self.assertTrue(rendering_result.mentions_stream_wildcard)
+ def test_mention_at_topic_wildcard(self) -> None:
+ user_profile = self.example_user("othello")
+ msg = Message(sender=user_profile, sending_client=get_client("test"))
+
+ for topic_wildcard in topic_wildcards:
+ content = f"@{topic_wildcard} test"
+ rendering_result = render_markdown(msg, content)
+ self.assertEqual(rendering_result.rendered_content, f"<p>@{topic_wildcard} test</p>")
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
+ self.assertFalse(rendering_result.mentions_stream_wildcard)
+ self.assertEqual(rendering_result.mentions_user_ids, set())
+
def test_mention_at_stream_wildcard(self) -> None:
user_profile = self.example_user("othello")
msg = Message(sender=user_profile, sending_client=get_client("test"))
@@ -1847,6 +1875,7 @@ class MarkdownTest(ZulipTestCase):
content = f"@{stream_wildcard} test"
rendering_result = render_markdown(msg, content)
self.assertEqual(rendering_result.rendered_content, f"<p>@{stream_wildcard} test</p>")
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
self.assertFalse(rendering_result.mentions_stream_wildcard)
self.assertEqual(rendering_result.mentions_user_ids, set())
@@ -1953,6 +1982,19 @@ class MarkdownTest(ZulipTestCase):
)
self.assertFalse(rendering_result.mentions_stream_wildcard)
+ def test_silent_topic_wildcard_mention(self) -> None:
+ user_profile = self.example_user("othello")
+ msg = Message(sender=user_profile, sending_client=get_client("test"))
+
+ for wildcard in topic_wildcards:
+ content = f"@_**{wildcard}**"
+ rendering_result = render_markdown(msg, content)
+ self.assertEqual(
+ rendering_result.rendered_content,
+ f'<p><span class="topic-mention silent">{wildcard}</span></p>',
+ )
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
+
def test_mention_invalid_followed_by_valid(self) -> None:
sender_user_profile = self.example_user("othello")
user_profile = self.example_user("hamlet")
@@ -2125,6 +2167,7 @@ class MarkdownTest(ZulipTestCase):
rendering_result = render_markdown(message, content)
self.assertEqual(rendering_result.rendered_content, expected)
self.assertFalse(rendering_result.mentions_stream_wildcard)
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
for wildcard in stream_wildcards:
assert_silent_mention(f"> @**{wildcard}**", wildcard)
@@ -2132,6 +2175,27 @@ class MarkdownTest(ZulipTestCase):
assert_silent_mention(f"```quote\n@**{wildcard}**\n```", wildcard)
assert_silent_mention(f"```quote\n@_**{wildcard}**\n```", wildcard)
+ def test_topic_wildcard_mention_in_quotes(self) -> None:
+ user_profile = self.example_user("othello")
+ message = Message(sender=user_profile, sending_client=get_client("test"))
+
+ def assert_silent_mention(content: str, wildcard: str) -> None:
+ expected = (
+ "<blockquote>\n<p>"
+ f'<span class="topic-mention silent">{wildcard}</span>'
+ "</p>\n</blockquote>"
+ )
+ rendering_result = render_markdown(message, content)
+ self.assertEqual(rendering_result.rendered_content, expected)
+ self.assertFalse(rendering_result.mentions_stream_wildcard)
+ self.assertFalse(rendering_result.mentions_topic_wildcard)
+
+ for wildcard in topic_wildcards:
+ assert_silent_mention(f"> @**{wildcard}**", wildcard)
+ assert_silent_mention(f"> @_**{wildcard}**", wildcard)
+ assert_silent_mention(f"```quote\n@**{wildcard}**\n```", wildcard)
+ assert_silent_mention(f"```quote\n@_**{wildcard}**\n```", wildcard)
+
def test_mention_duplicate_full_name(self) -> None:
realm = get_realm("zulip")