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

@@ -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. In
production, 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:
"""See
https://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 None
message_ids = do_send_messages([message])