reaction: Use the generic 'event_recipient_ids_for_action_on_messages'.

This commit updates the 'notify_reaction_update' function to use
the generic 'event_recipient_ids_for_action_on_messages' function.

It helps to add hardening such that if the invariant "no usermessage
row corresponding to a message exists if the user loses access to the
message" is violated due to some bug, it has minimal user impact.
This commit is contained in:
Prakhar Pratyush 2024-09-27 20:15:27 +05:30 committed by Tim Abbott
parent c3f2615e20
commit 5c23a3f186
1 changed files with 3 additions and 24 deletions

View File

@ -5,15 +5,15 @@ from zerver.lib.emoji import check_emoji_request, get_emoji_data
from zerver.lib.exceptions import ReactionExistsError
from zerver.lib.message import (
access_message_and_usermessage,
event_recipient_ids_for_action_on_messages,
set_visibility_policy_possible,
should_change_visibility_policy,
visibility_policy_for_participation,
)
from zerver.lib.message_cache import update_message_cache
from zerver.lib.stream_subscription import subscriber_ids_with_stream_history_access
from zerver.lib.streams import access_stream_by_id
from zerver.lib.user_message import create_historical_user_messages
from zerver.models import Message, Reaction, Recipient, Stream, UserMessage, UserProfile
from zerver.models import Message, Reaction, UserProfile
from zerver.tornado.django_api import send_event_on_commit
@ -43,28 +43,7 @@ def notify_reaction_update(
# Update the cached message since new reaction is added.
update_message_cache([message])
# Recipients for message update events, including reactions, are
# everyone who got the original message, plus subscribers of
# streams with the access to stream's full history.
#
# This means reactions won't live-update in preview narrows for a
# stream the user isn't yet subscribed to; this is the right
# performance tradeoff to avoid sending every reaction to public
# stream messages to all users.
#
# To ensure that reactions do live-update for any user who has
# actually participated in reacting to a message, we add a
# "historical" UserMessage row for any user who reacts to message,
# subscribing them to future notifications, even if they are not
# subscribed to the stream.
user_ids = set(
UserMessage.objects.filter(message=message.id).values_list("user_profile_id", flat=True)
)
if message.recipient.type == Recipient.STREAM:
stream_id = message.recipient.type_id
stream = Stream.objects.get(id=stream_id)
user_ids |= subscriber_ids_with_stream_history_access(stream)
user_ids = event_recipient_ids_for_action_on_messages([message])
send_event_on_commit(user_profile.realm, event, list(user_ids))