notifications: Fix missed message email notifications of welcome bot.

A missed message email notification, where the message is the welcome
message sent by the welcome bot on account creation, get sent when
the user somehow not focuses the browser tab during account creation.

No missed message email or push notifications should be sent for the
messages generated by the welcome bot.

'internal_send_private_message' accepts a parameter
'disable_external_notifications' and is set to 'True' when the sender
is 'welcome bot'.

A check is introduced in `trivially_should_not_notify`, not to notify
if `disable_external_notifications` is true.

TestCases are updated to include the `disable_external_notifications`
check in the early (False) return patterns of `is_push_notifiable` and
`is_email_notifiable`.

One query reduced for both `test_create_user_with_multiple_streams`
and `test_register`.
Reason: When welcome bot sends message after user creation
`do_send_messages` calls `get_active_presence_idle_user_ids`,
`user_ids` in `get_active_presence_idle_user_ids` remains empty if
`disable_external_notifications` is true because `is_notifiable` returns
false.
`get_active_presence_idle_user_ids` calls `filter_presence_idle_user_ids`
and since the `user_ids` is empty, the query inside the function doesn't
get executed.

MissedMessageHookTest updated.

Fixes: #22884
This commit is contained in:
Prakhar Pratyush
2022-10-22 16:55:06 +05:30
committed by Tim Abbott
parent b40bbd6ca8
commit 1a400b21e7
11 changed files with 124 additions and 7 deletions

View File

@@ -5,6 +5,7 @@ from unittest import mock
import orjson
from django.http import HttpRequest, HttpResponse
from zerver.actions.message_send import internal_send_private_message
from zerver.actions.muted_users import do_mute_user
from zerver.actions.streams import do_change_subscription_property
from 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: