Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
notifications: Fix missed message email notifications of welcome bot.
Prakhar Pratyush1a400b21e72c
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.pyindex 7d4e896123a..56ffa06ae80 100644--- a/zerver/actions/message_send.py+++ b/zerver/actions/message_send.py@@ -504,6 +504,7 @@ def build_message_send_dict(email_gateway: bool = False,mention_backend: Optional[MentionBackend] = None,limit_unread_user_ids: Optional[Set[int]] = None,+ disable_external_notifications: bool = False,) -> SendMessageRequest:"""Returns a dictionary that can be passed into do_send_messages. Inproduction, this is always called by check_message, but some@@ -610,6 +611,7 @@ def build_message_send_dict(links_for_embed=links_for_embed,widget_content=widget_content_dict,limit_unread_user_ids=limit_unread_user_ids,+ disable_external_notifications=disable_external_notifications,)return message_send_dict@@ -893,6 +895,7 @@ def do_send_messages(user_id=user_id,flags=user_flags.get(user_id, []),private_message=(message_type == "private"),+ disable_external_notifications=send_request.disable_external_notifications,online_push_user_ids=send_request.online_push_user_ids,pm_mention_push_disabled_user_ids=send_request.pm_mention_push_disabled_user_ids,pm_mention_email_disabled_user_ids=send_request.pm_mention_email_disabled_user_ids,@@ -926,6 +929,7 @@ def do_send_messages(wildcard_mention_user_ids=list(send_request.wildcard_mention_user_ids),muted_sender_user_ids=list(send_request.muted_sender_user_ids),all_bot_user_ids=list(send_request.all_bot_user_ids),+ disable_external_notifications=send_request.disable_external_notifications,)if send_request.message.is_stream_message():@@ -1346,6 +1350,7 @@ def check_message(skip_stream_access_check: bool = False,mention_backend: Optional[MentionBackend] = None,limit_unread_user_ids: Optional[Set[int]] = None,+ disable_external_notifications: bool = False,) -> SendMessageRequest:"""Seehttps://zulip.readthedocs.io/en/latest/subsystems/sending-messages.html@@ -1477,6 +1482,7 @@ def check_message(email_gateway=email_gateway,mention_backend=mention_backend,limit_unread_user_ids=limit_unread_user_ids,+ disable_external_notifications=disable_external_notifications,)if (@@ -1499,6 +1505,7 @@ def _internal_prep_message(email_gateway: bool = False,mention_backend: Optional[MentionBackend] = None,limit_unread_user_ids: Optional[Set[int]] = None,+ disable_external_notifications: bool = False,) -> Optional[SendMessageRequest]:"""Create a message object and checks it, but doesn't send it or save it to the database.@@ -1530,6 +1537,7 @@ def _internal_prep_message(email_gateway=email_gateway,mention_backend=mention_backend,limit_unread_user_ids=limit_unread_user_ids,+ disable_external_notifications=disable_external_notifications,)except JsonableError as e:logging.exception(@@ -1593,6 +1601,7 @@ def internal_prep_private_message(content: str,*,mention_backend: Optional[MentionBackend] = None,+ disable_external_notifications: bool = False,) -> Optional[SendMessageRequest]:"""See _internal_prep_message for details of how this works.@@ -1609,13 +1618,23 @@ def internal_prep_private_message(addressee=addressee,content=content,mention_backend=mention_backend,+ disable_external_notifications=disable_external_notifications,)def internal_send_private_message(- sender: UserProfile, recipient_user: UserProfile, content: str+ sender: UserProfile,+ recipient_user: UserProfile,+ content: str,+ *,+ disable_external_notifications: bool = False,) -> Optional[int]:- message = internal_prep_private_message(sender, recipient_user, content)+ message = internal_prep_private_message(+ sender,+ recipient_user,+ content,+ disable_external_notifications=disable_external_notifications,+ )if message is None:return Nonemessage_ids = do_send_messages([message])diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex d41a83478fc..cdfd5e14e97 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -169,6 +169,7 @@ class SendMessageRequest:delivery_type: Optional[str] = Nonelimit_unread_user_ids: Optional[Set[int]] = Noneservice_queue_events: Optional[Dict[str, List[Dict[str, Any]]]] = None+ disable_external_notifications: bool = False# We won't try to fetch more unread message IDs from the database thandiff --git a/zerver/lib/notification_data.py b/zerver/lib/notification_data.pyindex 900955057dc..10792ee7b6e 100644--- a/zerver/lib/notification_data.py+++ b/zerver/lib/notification_data.py@@ -20,6 +20,7 @@ class UserMessageNotificationsData:stream_push_notify: boolstream_email_notify: boolsender_is_muted: bool+ disable_external_notifications: booldef __post_init__(self) -> None:# Check that there's no dubious data.@@ -36,6 +37,7 @@ class UserMessageNotificationsData:user_id: int,flags: Collection[str],private_message: bool,+ disable_external_notifications: bool,online_push_user_ids: Set[int],pm_mention_push_disabled_user_ids: Set[int],pm_mention_email_disabled_user_ids: Set[int],@@ -60,6 +62,7 @@ class UserMessageNotificationsData:stream_push_notify=False,stream_email_notify=False,sender_is_muted=False,+ disable_external_notifications=False,)# `wildcard_mention_user_ids` are those user IDs for whom wildcard mentions should@@ -96,6 +99,7 @@ class UserMessageNotificationsData:stream_push_notify=(user_id in stream_push_user_ids),stream_email_notify=(user_id in stream_email_user_ids),sender_is_muted=(user_id in muted_sender_user_ids),+ disable_external_notifications=disable_external_notifications,)# For these functions, acting_user_id is the user sent a message@@ -112,6 +116,9 @@ class UserMessageNotificationsData:if self.sender_is_muted:return True+ if self.disable_external_notifications:+ return True+return Falsedef is_notifiable(self, acting_user_id: int, idle: bool) -> bool:diff --git a/zerver/lib/onboarding.py b/zerver/lib/onboarding.pyindex 2049759d647..b1048624728 100644--- a/zerver/lib/onboarding.py+++ b/zerver/lib/onboarding.py@@ -93,7 +93,12 @@ def send_initial_pms(user: UserProfile) -> None:)internal_send_private_message(- get_system_bot(settings.WELCOME_BOT, user.realm_id), user, content+ get_system_bot(settings.WELCOME_BOT, user.realm_id),+ user,+ content,+ # Note: Welcome bot doesn't trigger email/push notifications,+ # as this is intended to be seen contextually in the application.+ disable_external_notifications=True,)@@ -214,7 +219,15 @@ def send_welcome_bot_response(send_request: SendMessageRequest) -> None:welcome_bot = get_system_bot(settings.WELCOME_BOT, send_request.message.sender.realm_id)human_response_lower = send_request.message.content.lower()content = select_welcome_bot_response(human_response_lower)- internal_send_private_message(welcome_bot, send_request.message.sender, content)++ internal_send_private_message(+ welcome_bot,+ send_request.message.sender,+ content,+ # Note: Welcome bot doesn't trigger email/push notifications,+ # as this is intended to be seen contextually in the application.+ disable_external_notifications=True,+ )@transaction.atomicdiff --git a/zerver/lib/test_classes.py b/zerver/lib/test_classes.pyindex 6c017772811..5839e629376 100644--- a/zerver/lib/test_classes.py+++ b/zerver/lib/test_classes.py@@ -1686,6 +1686,7 @@ Output:stream_email_notify=kwargs.get("stream_email_notify", False),stream_push_notify=kwargs.get("stream_push_notify", False),sender_is_muted=kwargs.get("sender_is_muted", False),+ disable_external_notifications=kwargs.get("disable_external_notifications", False),)def get_maybe_enqueue_notifications_parameters(diff --git a/zerver/tests/test_event_queue.py b/zerver/tests/test_event_queue.pyindex 19d7d0c60d1..711bfb4f815 100644--- a/zerver/tests/test_event_queue.py+++ b/zerver/tests/test_event_queue.py@@ -5,6 +5,7 @@ from unittest import mockimport orjsonfrom django.http import HttpRequest, HttpResponse+from zerver.actions.message_send import internal_send_private_messagefrom zerver.actions.muted_users import do_mute_userfrom zerver.actions.streams import do_change_subscription_propertyfrom zerver.actions.user_settings import do_change_user_setting@@ -674,6 +675,31 @@ class MissedMessageHookTest(ZulipTestCase):self.destroy_event_queue(hambot, self.client_descriptor.event_queue.id)self.client_descriptor = hamlet_client_descriptor+ # Internal PMs+ def test_disable_external_notifications(self) -> None:+ # The disable_external_notifications parameter, used for messages sent by welcome bot,+ # should result in no email/push notifications being sent regardless of the message type.+ msg_id = internal_send_private_message(+ self.iago, self.user_profile, "Test Content", disable_external_notifications=True+ )+ assert msg_id is not None+ with mock.patch("zerver.tornado.event_queue.maybe_enqueue_notifications") as mock_enqueue:+ missedmessage_hook(self.user_profile.id, self.client_descriptor, True)+ mock_enqueue.assert_called_once()+ args_dict = mock_enqueue.call_args_list[0][1]++ self.assert_maybe_enqueue_notifications_call_args(+ args_dict=args_dict,+ message_id=msg_id,+ user_id=self.user_profile.id,+ pm_email_notify=True,+ pm_push_notify=True,+ disable_external_notifications=True,+ # disable_external_notifications parameter set to False would have resulted in+ # already_notified={"email_notified": True, "push_notified": True}+ already_notified={"email_notified": False, "push_notified": False},+ )+class FileReloadLogicTest(ZulipTestCase):def test_persistent_queue_filename(self) -> None:diff --git a/zerver/tests/test_notification_data.py b/zerver/tests/test_notification_data.pyindex 8644ae0dff2..f529ab0d01d 100644--- a/zerver/tests/test_notification_data.py+++ b/zerver/tests/test_notification_data.py@@ -113,6 +113,23 @@ class TestNotificationData(ZulipTestCase):)self.assertFalse(user_data.is_push_notifiable(acting_user_id=acting_user_id, idle=True))+ # 'disable_external_notifications' takes precedence over other flags.+ user_data = self.create_user_notifications_data_object(+ user_id=user_id,+ pm_push_notify=True,+ pm_email_notify=True,+ mention_push_notify=True,+ mention_email_notify=True,+ wildcard_mention_push_notify=True,+ wildcard_mention_email_notify=True,+ disable_external_notifications=True,+ )+ self.assertEqual(+ user_data.get_push_notification_trigger(acting_user_id=acting_user_id, idle=True),+ None,+ )+ self.assertFalse(user_data.is_push_notifiable(acting_user_id=acting_user_id, idle=True))+def test_is_email_notifiable(self) -> None:user_id = self.example_user("hamlet").idacting_user_id = self.example_user("cordelia").id@@ -209,6 +226,23 @@ class TestNotificationData(ZulipTestCase):)self.assertFalse(user_data.is_email_notifiable(acting_user_id=acting_user_id, idle=True))+ # Message sender is the welcome bot.+ user_data = self.create_user_notifications_data_object(+ user_id=user_id,+ pm_push_notify=True,+ pm_email_notify=True,+ mention_push_notify=True,+ mention_email_notify=True,+ wildcard_mention_push_notify=True,+ wildcard_mention_email_notify=True,+ disable_external_notifications=True,+ )+ self.assertEqual(+ user_data.get_email_notification_trigger(acting_user_id=acting_user_id, idle=True),+ None,+ )+ self.assertFalse(user_data.is_email_notifiable(acting_user_id=acting_user_id, idle=True))+def test_is_notifiable(self) -> None:# This is just for coverage purposes. We've already tested all scenarios above,# and `is_notifiable` is a simple OR of the email and push functions.@@ -224,6 +258,7 @@ class TestNotificationData(ZulipTestCase):user_id=user_id,flags=["mentioned"],private_message=True,+ disable_external_notifications=False,online_push_user_ids=set(),pm_mention_email_disabled_user_ids=set(),pm_mention_push_disabled_user_ids=set(),diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 54e276f3977..3fe48726eee 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -958,7 +958,7 @@ class LoginTest(ZulipTestCase):ContentType.objects.clear_cache()# Ensure the number of queries we make is not O(streams)- with self.assert_database_query_count(96), cache_tries_captured() as cache_tries:+ with self.assert_database_query_count(95), cache_tries_captured() as cache_tries:with self.captureOnCommitCallbacks(execute=True):self.register(self.nonreg_email("test"), "test")diff --git a/zerver/tests/test_tutorial.py b/zerver/tests/test_tutorial.pyindex 856f28662c0..1f7c5f161b1 100644--- a/zerver/tests/test_tutorial.py+++ b/zerver/tests/test_tutorial.py@@ -15,7 +15,15 @@ class TutorialTests(ZulipTestCase):user = self.example_user("hamlet")welcome_bot = get_system_bot(settings.WELCOME_BOT, user.realm_id)content = "Shortened welcome message."- internal_send_private_message(welcome_bot, user, content)+ internal_send_private_message(+ welcome_bot,+ user,+ content,+ # disable_external_notifications set to False will still lead+ # the tests to pass. Setting this to True, because we contextually+ # set this to true for welcome_bot in the codebase.+ disable_external_notifications=True,+ )def test_tutorial_status(self) -> None:user = self.example_user("hamlet")diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 68da24f9686..38f03dcc26c 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -804,7 +804,7 @@ class QueryCountTest(ZulipTestCase):events: List[Mapping[str, Any]] = []- with self.assert_database_query_count(91):+ with self.assert_database_query_count(90):with cache_tries_captured() as cache_tries:with self.tornado_redirected_to_list(events, expected_num_events=11):fred = do_create_user(diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.pyindex b579416245f..0a206322b87 100644--- a/zerver/tornado/event_queue.py+++ b/zerver/tornado/event_queue.py@@ -782,6 +782,9 @@ def missedmessage_hook(stream_email_notify=internal_data.get("stream_email_notify", False),# Since one is by definition idle, we don't need to check online_push_enabledonline_push_enabled=False,+ disable_external_notifications=internal_data.get(+ "disable_external_notifications", False+ ),)mentioned_user_group_id = internal_data.get("mentioned_user_group_id")@@ -932,6 +935,7 @@ def process_message_event(wildcard_mention_user_ids = set(event_template.get("wildcard_mention_user_ids", []))muted_sender_user_ids = set(event_template.get("muted_sender_user_ids", []))all_bot_user_ids = set(event_template.get("all_bot_user_ids", []))+ disable_external_notifications = event_template.get("disable_external_notifications", False)wide_dict: Dict[str, Any] = event_template["message_dict"]@@ -972,6 +976,7 @@ def process_message_event(user_id=user_profile_id,flags=flags,private_message=private_message,+ disable_external_notifications=disable_external_notifications,online_push_user_ids=online_push_user_ids,pm_mention_push_disabled_user_ids=pm_mention_push_disabled_user_ids,pm_mention_email_disabled_user_ids=pm_mention_email_disabled_user_ids,@@ -1127,6 +1132,7 @@ def process_message_update_event(wildcard_mention_user_ids = set(event_template.pop("wildcard_mention_user_ids", []))muted_sender_user_ids = set(event_template.pop("muted_sender_user_ids", []))all_bot_user_ids = set(event_template.pop("all_bot_user_ids", []))+ disable_external_notifications = event_template.pop("disable_external_notifications", False)# TODO/compatibility: Translation code for the rename of# `push_notify_user_ids` to `online_push_user_ids`. Remove this@@ -1178,6 +1184,7 @@ def process_message_update_event(user_id=user_profile_id,flags=flags,private_message=(stream_name is None),+ disable_external_notifications=disable_external_notifications,online_push_user_ids=online_push_user_ids,pm_mention_push_disabled_user_ids=pm_mention_push_disabled_user_ids,pm_mention_email_disabled_user_ids=pm_mention_email_disabled_user_ids,