Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
event_recipient_ids_for_action_on_messages: Change args.
Mateusz Mandera74b19c069801
diff --git a/zerver/actions/message_delete.py b/zerver/actions/message_delete.pyindex 6a46aff615e..bdc89993f1d 100644--- a/zerver/actions/message_delete.py+++ b/zerver/actions/message_delete.py@@ -81,7 +81,8 @@ def _process_grouped_messages_deletion(# We exclude long-term idle users, since they by definition have no active clients.users_to_notify = event_recipient_ids_for_action_on_messages(- grouped_messages,+ message_ids,+ is_channel_message=message_type == "stream",channel=stream if message_type == "stream" else None,)diff --git a/zerver/actions/message_edit.py b/zerver/actions/message_edit.pyindex d5ccb83e926..35845e7a4b0 100644--- a/zerver/actions/message_edit.py+++ b/zerver/actions/message_edit.py@@ -402,7 +402,9 @@ def get_mentions_for_message_updates(message: Message) -> set[int]:.values_list("user_profile_id", flat=True))- user_ids_having_message_access = event_recipient_ids_for_action_on_messages([message])+ user_ids_having_message_access = event_recipient_ids_for_action_on_messages(+ [message.id], message.is_channel_message+ )return set(mentioned_user_ids) & user_ids_having_message_access@@ -476,7 +478,9 @@ def do_update_embedded_data("rendering_only": True,}- users_to_notify = event_recipient_ids_for_action_on_messages([message])+ users_to_notify = event_recipient_ids_for_action_on_messages(+ [message.id], message.is_channel_message+ )filtered_ums = [um for um in ums if um.user_profile_id in users_to_notify]def user_info(um: UserMessage) -> dict[str, Any]:diff --git a/zerver/actions/reactions.py b/zerver/actions/reactions.pyindex fec7f37dd31..d093cb09ea7 100644--- a/zerver/actions/reactions.py+++ b/zerver/actions/reactions.py@@ -43,7 +43,7 @@ def notify_reaction_update(# Update the cached message since new reaction is added.update_message_cache([message])- user_ids = event_recipient_ids_for_action_on_messages([message])+ user_ids = event_recipient_ids_for_action_on_messages([message.id], message.is_channel_message)send_event_on_commit(user_profile.realm, event, list(user_ids))diff --git a/zerver/actions/submessage.py b/zerver/actions/submessage.pyindex 29d621f2ac9..051b6a2e26f 100644--- a/zerver/actions/submessage.py+++ b/zerver/actions/submessage.py@@ -91,6 +91,8 @@ def do_add_submessage(sender_id=sender_id,content=content,)- target_user_ids = event_recipient_ids_for_action_on_messages([submessage.message])+ target_user_ids = event_recipient_ids_for_action_on_messages(+ [submessage.message.id], submessage.message.is_channel_message+ )send_event_on_commit(realm, event, target_user_ids)diff --git a/zerver/lib/message.py b/zerver/lib/message.pyindex 5ce49d08995..09a640782d3 100644--- a/zerver/lib/message.py+++ b/zerver/lib/message.py@@ -6,7 +6,7 @@ from typing import Any, TypedDictfrom django.conf import settingsfrom django.db import connection-from django.db.models import Exists, F, Max, OuterRef, QuerySet, Sum+from django.db.models import Exists, F, Max, OuterRef, QuerySet, Subquery, Sumfrom django.utils.timezone import now as timezone_nowfrom django.utils.translation import gettext as _from django_cte import CTE, with_cte@@ -602,7 +602,8 @@ def has_message_access(def event_recipient_ids_for_action_on_messages(- messages: list[Message],+ message_ids: list[int],+ is_channel_message: bool,*,channel: Stream | None = None,exclude_long_term_idle_users: bool = True,@@ -611,6 +612,13 @@ def event_recipient_ids_for_action_on_messages((delete, react, etc) is performed on given set of messages, whichare expected to all be in a single conversation.+ is_channel_message needs to be passed from the caller to inform about+ whether we're processing channel or private messages without having+ to do any work to determine it here in this function.+ This is independent from the "channel" optional kwarg, in which the caller+ may pass the relevant channel, but doesn't have to.+ If needed, the channel will be fetched inside this function.+This function aligns with the 'has_message_access' above to ensurethat events reach only those users who have access to the messages.@@ -620,8 +628,7 @@ def event_recipient_ids_for_action_on_messages(they are not subscribed. Such events are limited to those messageswhere the user has a UserMessage row (including `historical` rows)."""- assert len(messages) > 0- message_ids = [message.id for message in messages]+ assert len(message_ids) > 0def get_user_ids_having_usermessage_row_for_messages(message_ids: list[int]) -> set[int]:"""Returns the IDs of users who actually received the messages."""@@ -630,17 +637,20 @@ def event_recipient_ids_for_action_on_messages(usermessages = usermessages.exclude(user_profile__long_term_idle=True)return set(usermessages.values_list("user_profile_id", flat=True))- sample_message = messages[0]- if not sample_message.is_channel_message:+ if not is_channel_message:# For DM, event is sent to users who actually received the message.return get_user_ids_having_usermessage_row_for_messages(message_ids)- channel_id = sample_message.recipient.type_idif channel is None:- channel = Stream.objects.get(id=channel_id)+ sample_message_id = message_ids[0]+ channel = Stream.objects.get(+ recipient_id=Subquery(+ Message.objects.filter(id=sample_message_id).values("recipient_id")[:1]+ )+ )subscriptions = get_active_subscriptions_for_stream_id(- channel_id, include_deactivated_users=False+ channel.id, include_deactivated_users=False)if exclude_long_term_idle_users:subscriptions = subscriptions.exclude(user_profile__long_term_idle=True)diff --git a/zerver/tests/test_message_delete.py b/zerver/tests/test_message_delete.pyindex ae18f51ea06..3fa45324ebf 100644--- a/zerver/tests/test_message_delete.py+++ b/zerver/tests/test_message_delete.py@@ -915,7 +915,7 @@ class DeleteMessageTest(ZulipTestCase):self.assertEqual(stream.first_message_id, message_ids[1])all_messages = Message.objects.filter(id__in=message_ids)- with self.assert_database_query_count(26):+ with self.assert_database_query_count(25):do_delete_messages(realm, all_messages, acting_user=None)stream = get_stream(stream_name, realm)self.assertEqual(stream.first_message_id, None)diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.pyindex 5d849bbbe46..5559b52e51b 100644--- a/zerver/tests/test_retention.py+++ b/zerver/tests/test_retention.py@@ -1151,7 +1151,7 @@ class TestDoDeleteMessages(ZulipTestCase):message_ids = [self.send_stream_message(cordelia, "Verona", str(i)) for i in range(10)]messages = Message.objects.filter(id__in=message_ids)- with self.assert_database_query_count(24):+ with self.assert_database_query_count(23):do_delete_messages(realm, messages, acting_user=None)self.assertFalse(Message.objects.filter(id__in=message_ids).exists())