Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
markdown: Fix invalid mention bug for user group mention.
m-e-l-u-h-a-ndadbba0c2593
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex 67df4ea4f67..07aeb9b1a94 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1844,11 +1844,13 @@ class UserMentionPattern(markdown.inlinepatterns.InlineProcessor):return None, None, None-class UserGroupMentionPattern(markdown.inlinepatterns.Pattern):- def handleMatch(self, m: Match[str]) -> Optional[Element]:- match = m.group(2)-+class UserGroupMentionPattern(markdown.inlinepatterns.InlineProcessor):+ def handleMatch( # type: ignore[override] # supertype incompatible with supersupertype+ self, m: Match[str], data: str+ ) -> Union[Tuple[None, None, None], Tuple[Element, int, int]]:+ match = m.group(1)db_data = self.md.zulip_db_data+if self.md.zulip_message and db_data is not None:name = extract_user_group(match)user_group = db_data["mention_data"].get_user_group(name)@@ -1859,15 +1861,15 @@ class UserGroupMentionPattern(markdown.inlinepatterns.Pattern):else:# Don't highlight @-mentions that don't refer to a valid user# group.- return None+ return None, None, Noneel = Element("span")el.set("class", "user-group-mention")el.set("data-user-group-id", user_group_id)text = f"@{name}"el.text = markdown.util.AtomicString(text)- return el- return None+ return el, m.start(), m.end()+ return None, None, Noneclass StreamPattern(CompiledPattern):diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 09a59deae54..e24444bde3f 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -2056,6 +2056,27 @@ class MarkdownTest(ZulipTestCase):self.assertEqual(msg.mentions_user_ids, {user_profile.id})self.assertEqual(msg.mentions_user_group_ids, {user_group.id})+ def test_invalid_user_group_followed_by_valid_mention_single(self) -> None:+ sender_user_profile = self.example_user("othello")+ user_profile = self.example_user("hamlet")+ msg = Message(sender=sender_user_profile, sending_client=get_client("test"))+ user_id = user_profile.id+ user_group = self.create_user_group_for_test("support")++ content = "@**King Hamlet** @*Invalid user group* @*support*"+ self.assertEqual(+ render_markdown(msg, content),+ '<p><span class="user-mention" '+ f'data-user-id="{user_id}">'+ "@King Hamlet</span> "+ "@<em>Invalid user group</em> "+ '<span class="user-group-mention" '+ f'data-user-group-id="{user_group.id}">'+ "@support</span></p>",+ )+ self.assertEqual(msg.mentions_user_ids, {user_profile.id})+ self.assertEqual(msg.mentions_user_group_ids, {user_group.id})+def test_user_group_mention_atomic_string(self) -> None:sender_user_profile = self.example_user("othello")realm = get_realm("zulip")