Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
user_mention: Fix mentions of deactivated users.
roanster007dc492867af5c
diff --git a/zerver/lib/markdown/__init__.py b/zerver/lib/markdown/__init__.pyindex cefca166f58..0b0896261ff 100644--- a/zerver/lib/markdown/__init__.py+++ b/zerver/lib/markdown/__init__.py@@ -1895,6 +1895,8 @@ class UserMentionPattern(CompiledInlineProcessor):self.zmd.zulip_rendering_result.mentions_topic_wildcard = Trueelif user is not None:assert isinstance(user, FullNameInfo)+ if not user.is_active:+ silent = Trueif not silent:self.zmd.zulip_rendering_result.mentions_user_ids.add(user.id)diff --git a/zerver/lib/mention.py b/zerver/lib/mention.pyindex 8421d46e4c4..48768079ed9 100644--- a/zerver/lib/mention.py+++ b/zerver/lib/mention.py@@ -27,6 +27,7 @@ stream_wildcards = frozenset(["all", "everyone", "stream"])class FullNameInfo:id: intfull_name: str+ is_active: bool@dataclass@@ -94,17 +95,20 @@ class MentionBackend:UserProfile.objects.filter(Q(realm_id=self.realm_id) | Q(email__in=settings.CROSS_REALM_BOT_EMAILS),)- .filter(is_active=True).filter(functools.reduce(lambda a, b: a | b, q_list),).only("id","full_name",+ "is_active",))- user_list = [FullNameInfo(id=row.id, full_name=row.full_name) for row in rows]+ user_list = [+ FullNameInfo(id=row.id, full_name=row.full_name, is_active=row.is_active)+ for row in rows+ ]# We expect callers who take advantage of our cache to supply both# id and full_name in the user mentions in their messages.diff --git a/zerver/tests/test_markdown.py b/zerver/tests/test_markdown.pyindex 9b3504cd88e..ebae8e2c4dd 100644--- a/zerver/tests/test_markdown.py+++ b/zerver/tests/test_markdown.py@@ -253,6 +253,7 @@ class MarkdownMiscTest(ZulipTestCase):FullNameInfo(full_name="Fred Flintstone",id=fred2.id,+ is_active=True,),)self.assertEqual(@@ -260,6 +261,7 @@ class MarkdownMiscTest(ZulipTestCase):FullNameInfo(full_name="Fred Flintstone",id=fred4.id,+ is_active=True,),)@@ -276,6 +278,7 @@ class MarkdownMiscTest(ZulipTestCase):FullNameInfo(full_name=hamlet.full_name,id=hamlet.id,+ is_active=True,),)@@ -2110,6 +2113,48 @@ class MarkdownTest(ZulipTestCase):)self.assertEqual(rendering_result.mentions_user_ids, set())+ def test_mention_deactivated_users(self) -> None:+ sender_user_profile = self.example_user("othello")+ user_profile = self.example_user("hamlet")+ change_user_is_active(user_profile, False)+ msg = Message(+ sender=sender_user_profile,+ sending_client=get_client("test"),+ realm=sender_user_profile.realm,+ )+ user_id = user_profile.id++ content = "@**King Hamlet**"+ rendering_result = render_markdown(msg, content)+ self.assertEqual(+ rendering_result.rendered_content,+ '<p><span class="user-mention silent" '+ f'data-user-id="{user_id}">'+ "King Hamlet</span></p>",+ )+ self.assertEqual(rendering_result.mentions_user_ids, set())++ def test_mention_silent_deactivated_users(self) -> None:+ sender_user_profile = self.example_user("othello")+ user_profile = self.example_user("hamlet")+ change_user_is_active(user_profile, False)+ msg = Message(+ sender=sender_user_profile,+ sending_client=get_client("test"),+ realm=sender_user_profile.realm,+ )+ user_id = user_profile.id++ content = "@_**King Hamlet**"+ rendering_result = render_markdown(msg, content)+ self.assertEqual(+ rendering_result.rendered_content,+ '<p><span class="user-mention silent" '+ f'data-user-id="{user_id}">'+ "King Hamlet</span></p>",+ )+ self.assertEqual(rendering_result.mentions_user_ids, set())+def test_silent_stream_wildcard_mention(self) -> None:user_profile = self.example_user("othello")msg = Message(