Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message_send: Check presence for all messages in one query.

Aman Agrawal3e9bc1e13f70
diff --git a/zerver/actions/message_send.py b/zerver/actions/message_send.py
index 2fd933b15ec..c00e72a8738 100644
--- a/zerver/actions/message_send.py
+++ b/zerver/actions/message_send.py
@@ -956,7 +956,7 @@ def create_user_messages(
def filter_presence_idle_user_ids(user_ids: set[int]) -> list[int]:
- # Given a set of user IDs (the recipients of a message), accesses
+ # Given a set of user IDs (the recipients of messages), accesses
# the UserPresence table to determine which of these users are
# currently idle and should potentially get email notifications
# (and push notifications with
@@ -999,6 +999,55 @@ def get_notifiable_if_idle_user_ids(
return user_ids
+def get_presence_idle_user_ids_by_message_id(
+ send_message_requests: list[SendMessageRequest],
+ user_message_flags: dict[int, dict[int, list[str]]],
+) -> dict[int, list[int]]:
+ notifiable_if_idle_user_ids_by_message_id: dict[int, set[int]] = {}
+ for send_request in send_message_requests:
+ user_flags = user_message_flags.get(send_request.message.id, {})
+ user_notifications_data_list = [
+ UserMessageNotificationsData.from_user_id_sets(
+ user_id=user_id,
+ flags=user_flags.get(user_id, []),
+ private_message=not send_request.message.is_channel_message,
+ disable_external_notifications=send_request.disable_external_notifications,
+ online_push_user_ids=send_request.online_push_user_ids,
+ dm_mention_push_disabled_user_ids=send_request.dm_mention_push_disabled_user_ids,
+ dm_mention_email_disabled_user_ids=send_request.dm_mention_email_disabled_user_ids,
+ stream_push_user_ids=send_request.stream_push_user_ids,
+ stream_email_user_ids=send_request.stream_email_user_ids,
+ topic_wildcard_mention_user_ids=send_request.topic_wildcard_mention_user_ids,
+ stream_wildcard_mention_user_ids=send_request.stream_wildcard_mention_user_ids,
+ followed_topic_push_user_ids=send_request.followed_topic_push_user_ids,
+ followed_topic_email_user_ids=send_request.followed_topic_email_user_ids,
+ topic_wildcard_mention_in_followed_topic_user_ids=send_request.topic_wildcard_mention_in_followed_topic_user_ids,
+ stream_wildcard_mention_in_followed_topic_user_ids=send_request.stream_wildcard_mention_in_followed_topic_user_ids,
+ muted_sender_user_ids=send_request.muted_sender_user_ids,
+ all_bot_user_ids=send_request.all_bot_user_ids,
+ push_device_registered_user_ids=send_request.push_device_registered_user_ids,
+ )
+ for user_id in send_request.active_user_ids
+ ]
+ notifiable_if_idle_user_ids_by_message_id[send_request.message.id] = (
+ get_notifiable_if_idle_user_ids(
+ realm=send_request.realm,
+ sender_id=send_request.message.sender_id,
+ user_notifications_data_list=user_notifications_data_list,
+ )
+ )
+
+ idle_user_ids = set(
+ filter_presence_idle_user_ids(
+ set().union(*notifiable_if_idle_user_ids_by_message_id.values())
+ )
+ )
+ return {
+ message_id: sorted(user_ids & idle_user_ids)
+ for message_id, user_ids in notifiable_if_idle_user_ids_by_message_id.items()
+ }
+
+
def get_message_url_and_link(
send_request: SendMessageRequest, message_dict: dict[str, Any]
) -> tuple[str, str]:
@@ -1153,6 +1202,9 @@ def do_send_messages(
do_widget_post_save_actions(send_request)
wide_message_dicts = get_wide_message_dicts(send_message_requests)
+ presence_idle_user_ids_by_message_id = get_presence_idle_user_ids_by_message_id(
+ send_message_requests, user_message_flags
+ )
# This next loop is responsible for notifying other parts of the
# Zulip system about the messages we just committed to the database:
@@ -1303,40 +1355,6 @@ def do_send_messages(
users.append(user_data)
- sender = send_request.message.sender
- recipient_type = wide_message_dict["type"]
- user_notifications_data_list = [
- UserMessageNotificationsData.from_user_id_sets(
- user_id=user_id,
- flags=user_flags.get(user_id, []),
- private_message=recipient_type == "private",
- disable_external_notifications=send_request.disable_external_notifications,
- online_push_user_ids=send_request.online_push_user_ids,
- dm_mention_push_disabled_user_ids=send_request.dm_mention_push_disabled_user_ids,
- dm_mention_email_disabled_user_ids=send_request.dm_mention_email_disabled_user_ids,
- stream_push_user_ids=send_request.stream_push_user_ids,
- stream_email_user_ids=send_request.stream_email_user_ids,
- topic_wildcard_mention_user_ids=send_request.topic_wildcard_mention_user_ids,
- stream_wildcard_mention_user_ids=send_request.stream_wildcard_mention_user_ids,
- followed_topic_push_user_ids=send_request.followed_topic_push_user_ids,
- followed_topic_email_user_ids=send_request.followed_topic_email_user_ids,
- topic_wildcard_mention_in_followed_topic_user_ids=send_request.topic_wildcard_mention_in_followed_topic_user_ids,
- stream_wildcard_mention_in_followed_topic_user_ids=send_request.stream_wildcard_mention_in_followed_topic_user_ids,
- muted_sender_user_ids=send_request.muted_sender_user_ids,
- all_bot_user_ids=send_request.all_bot_user_ids,
- push_device_registered_user_ids=send_request.push_device_registered_user_ids,
- )
- for user_id in send_request.active_user_ids
- ]
-
- presence_idle_user_ids = filter_presence_idle_user_ids(
- get_notifiable_if_idle_user_ids(
- realm=send_request.realm,
- sender_id=sender.id,
- user_notifications_data_list=user_notifications_data_list,
- )
- )
-
if send_request.recipients_for_user_creation_events is not None:
from zerver.actions.create_user import notify_created_user
@@ -1349,7 +1367,7 @@ def do_send_messages(
event = InternalMessageEvent(
message=send_request.message.id,
message_dict=wide_message_dict,
- presence_idle_user_ids=presence_idle_user_ids,
+ presence_idle_user_ids=presence_idle_user_ids_by_message_id[send_request.message.id],
online_push_user_ids=list(send_request.online_push_user_ids),
dm_mention_push_disabled_user_ids=list(send_request.dm_mention_push_disabled_user_ids),
dm_mention_email_disabled_user_ids=list(
diff --git a/zerver/tests/test_channel_fetch.py b/zerver/tests/test_channel_fetch.py
index e2cfca9c56c..68c75b4ebbe 100644
--- a/zerver/tests/test_channel_fetch.py
+++ b/zerver/tests/test_channel_fetch.py
@@ -882,7 +882,7 @@ class GetSubscribersTest(ZulipTestCase):
polonius.id,
]
- with self.assert_database_query_count(59):
+ with self.assert_database_query_count(57):
self.subscribe_via_post(
self.user_profile,
stream_names,
diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.py
index f46ab4ddac3..c3bbcb58b72 100644
--- a/zerver/tests/test_message_send.py
+++ b/zerver/tests/test_message_send.py
@@ -19,6 +19,7 @@ from zerver.actions.message_send import (
do_send_messages,
extract_private_recipients,
extract_stream_indicator,
+ filter_presence_idle_user_ids,
internal_prep_private_message,
internal_prep_stream_message,
internal_prep_stream_message_by_name,
@@ -2861,7 +2862,9 @@ class DoSendMessagesTest(ZulipTestCase):
Hamlet is active and Othello idle, so only the DMs to Othello
and the message mentioning him list an idle user. Othello is
- subscribed to Verona, but the poll doesn't notify him.
+ subscribed to Verona, but the poll doesn't notify him. Only
+ Hamlet and Othello would be notified if idle, so only their
+ presence is checked.
"""
realm = get_realm("zulip")
hamlet = self.example_user("hamlet")
@@ -2893,9 +2896,13 @@ class DoSendMessagesTest(ZulipTestCase):
internal_prep_stream_message(notification_bot, lear_stream, "#1234", "Hello, Lear!"),
]
with (
- self.assert_database_query_count(24),
+ self.assert_database_query_count(21),
self.assert_memcached_count(11),
self.capture_send_event_calls(expected_num_events=6) as events,
+ mock.patch(
+ "zerver.actions.message_send.filter_presence_idle_user_ids",
+ wraps=filter_presence_idle_user_ids,
+ ) as filter_presence_mock,
):
do_send_messages(send_requests)
@@ -2919,6 +2926,7 @@ class DoSendMessagesTest(ZulipTestCase):
[event["event"]["presence_idle_user_ids"] for event in events],
[[], [othello.id], [othello.id], [], [othello.id], []],
)
+ filter_presence_mock.assert_called_once_with({hamlet.id, othello.id})
class PersonalMessageSendTest(ZulipTestCase):
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index 29ceb1a06c4..e5671037f64 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -5426,7 +5426,7 @@ class SubscriptionAPITest(ZulipTestCase):
# the bot should send DMs to all the newly subscribed users, but
# no announcement message.
now = timezone_now()
- with self.assert_database_query_count(74):
+ with self.assert_database_query_count(70):
response = self.subscribe_via_post(
desdemona,
[test_channel.name],