Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
message_send: Fix wildcard_mentioned flag unset for few participants.
Prakhar Pratyush379a08eb1ed9
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.pyindex 4cc926e5ce8..41efa063e16 100644--- a/zerver/actions/message_send.py+++ b/zerver/actions/message_send.py@@ -179,6 +179,7 @@ class RecipientInfoResult:default_bot_user_ids: Set[int]service_bot_tuples: List[Tuple[int, int]]all_bot_user_ids: Set[int]+ topic_participant_user_ids: Set[int]class ActiveUserDict(TypedDict):@@ -210,6 +211,7 @@ def get_recipient_info(topic_wildcard_mention_in_followed_topic_user_ids: Set[int] = set()stream_wildcard_mention_in_followed_topic_user_ids: Set[int] = set()muted_sender_user_ids: Set[int] = get_muting_users(sender_id)+ topic_participant_user_ids: Set[int] = set()if recipient.type == Recipient.PERSONAL:# The sender and recipient may be the same id, so@@ -223,7 +225,6 @@ def get_recipient_info(# of this function for different message types.assert stream_topic is not None- topic_participant_user_ids: Set[int] = set()if possible_topic_wildcard_mention:# A topic participant is anyone who either sent or reacted to messages in the topic.# It is expensive to call `participants_for_topic` if the topic has a large number@@ -464,6 +465,7 @@ def get_recipient_info(default_bot_user_ids=default_bot_user_ids,service_bot_tuples=service_bot_tuples,all_bot_user_ids=all_bot_user_ids,+ topic_participant_user_ids=topic_participant_user_ids,)@@ -625,9 +627,11 @@ def build_message_send_dict(topic_wildcard_mention_in_followed_topic_user_ids = (info.topic_wildcard_mention_in_followed_topic_user_ids)+ topic_participant_user_ids = info.topic_participant_user_idselse:topic_wildcard_mention_user_ids = set()topic_wildcard_mention_in_followed_topic_user_ids = set()+ topic_participant_user_ids = set()"""Once we have the actual list of mentioned ids from messagerendering, we can patch in "default bots" (aka normal bots)@@ -670,6 +674,7 @@ def build_message_send_dict(widget_content=widget_content_dict,limit_unread_user_ids=limit_unread_user_ids,disable_external_notifications=disable_external_notifications,+ topic_participant_user_ids=topic_participant_user_ids,)return message_send_dict@@ -688,17 +693,13 @@ def create_user_messages(mark_as_read_user_ids: Set[int],limit_unread_user_ids: Optional[Set[int]],scheduled_message_to_self: bool,- topic_wildcard_mention_user_ids: Set[int],- topic_wildcard_mention_in_followed_topic_user_ids: Set[int],+ topic_participant_user_ids: Set[int],) -> List[UserMessageLite]:# These properties on the Message are set via# render_markdown by code in the Markdown inline patternsids_with_alert_words = rendering_result.user_ids_with_alert_wordssender_id = message.sender.idis_stream_message = message.is_stream_message()- all_topic_wildcard_mention_user_ids = topic_wildcard_mention_user_ids.union(- topic_wildcard_mention_in_followed_topic_user_ids- )base_flags = 0if rendering_result.mentions_stream_wildcard:@@ -747,7 +748,7 @@ def create_user_messages(flags |= UserMessage.flags.has_alert_wordif (rendering_result.mentions_topic_wildcard- and user_profile_id in all_topic_wildcard_mention_user_ids+ and user_profile_id in topic_participant_user_ids):flags |= UserMessage.flags.wildcard_mentioned@@ -875,8 +876,7 @@ def do_send_messages(mark_as_read_user_ids=mark_as_read_user_ids,limit_unread_user_ids=send_request.limit_unread_user_ids,scheduled_message_to_self=scheduled_message_to_self,- topic_wildcard_mention_user_ids=send_request.topic_wildcard_mention_user_ids,- topic_wildcard_mention_in_followed_topic_user_ids=send_request.topic_wildcard_mention_in_followed_topic_user_ids,+ topic_participant_user_ids=send_request.topic_participant_user_ids,)for um in user_messages:diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex fb8e4aab1b4..5d1528a01c3 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -191,6 +191,8 @@ class SendMessageRequest:# (having stream wildcard) is being sent to, and have the# 'followed_topic_wildcard_mentions_notify' setting ON.stream_wildcard_mention_in_followed_topic_user_ids: Set[int]+ # A topic participant is anyone who either sent or reacted to messages in the topic.+ topic_participant_user_ids: Set[int]links_for_embed: Set[str]widget_content: Optional[Dict[str, Any]]submessages: List[Dict[str, Any]] = field(default_factory=list)diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex 10b3b2947cd..32d315e5df3 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -29,6 +29,7 @@ from zerver.actions.message_send import (from zerver.actions.realm_settings import do_set_realm_propertyfrom zerver.actions.streams import do_change_stream_post_policyfrom zerver.actions.user_groups import add_subgroups_to_user_group, check_add_user_group+from zerver.actions.user_settings import do_change_user_settingfrom zerver.actions.users import do_change_can_forge_sender, do_deactivate_userfrom zerver.lib.addressee import Addresseefrom zerver.lib.exceptions import JsonableError@@ -1805,6 +1806,51 @@ class StreamMessagesTest(ZulipTestCase):self.send_and_verify_stream_wildcard_mention_message("iago", test_fails=True)self.send_and_verify_stream_wildcard_mention_message("iago", sub_count=10)+ def test_wildcard_mentioned_flag_topic_wildcard_mention(self) -> None:+ # For topic wildcard mentions, the 'wildcard_mentioned' flag should be+ # set for all the user messages for topic participants, irrespective of+ # their notifications settings.+ cordelia = self.example_user("cordelia")+ hamlet = self.example_user("hamlet")+ iago = self.example_user("iago")++ for user_profile in [cordelia, hamlet, iago]:+ self.subscribe(user_profile, "Denmark")++ # user | topic participant | wildcard_mentions_notify setting+ # -------- | ----------------- | ----------------------------------+ # cordelia | YES | True+ # hamlet | YES | False+ # iago | NO | True+ self.send_stream_message(cordelia, "Denmark", content="test", topic_name="topic-1")+ do_change_user_setting(cordelia, "wildcard_mentions_notify", True, acting_user=None)+ self.send_stream_message(hamlet, "Denmark", content="Hi @**topic**", topic_name="topic-1")+ message = most_recent_message(cordelia)+ self.assertTrue(+ UserMessage.objects.get(+ user_profile=cordelia, message=message+ ).flags.wildcard_mentioned.is_set+ )++ self.send_stream_message(hamlet, "Denmark", content="test", topic_name="topic-2")+ do_change_user_setting(hamlet, "wildcard_mentions_notify", False, acting_user=None)+ self.send_stream_message(cordelia, "Denmark", content="Hi @**topic**", topic_name="topic-2")+ message = most_recent_message(hamlet)+ self.assertTrue(+ UserMessage.objects.get(+ user_profile=hamlet, message=message+ ).flags.wildcard_mentioned.is_set+ )++ do_change_user_setting(iago, "wildcard_mentions_notify", True, acting_user=None)+ self.send_stream_message(hamlet, "Denmark", content="Hi @**topic**", topic_name="topic-3")+ message = most_recent_message(iago)+ self.assertFalse(+ UserMessage.objects.get(+ user_profile=iago, message=message+ ).flags.wildcard_mentioned.is_set+ )+def test_invalid_wildcard_mention_policy(self) -> None:cordelia = self.example_user("cordelia")self.login_user(cordelia)diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 3eb24ae74e2..965e1c69b8e 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1859,6 +1859,7 @@ class RecipientInfoTest(ZulipTestCase):default_bot_user_ids=set(),service_bot_tuples=[],all_bot_user_ids=set(),+ topic_participant_user_ids=set(),)self.assertEqual(info, expected_info)