message_flags: Update do_mark_muted_user...read to send event on commit.

Earlier, we were using 'send_event' in
'do_mark_muted_user_messages_as_read' which can lead to a
situation where we enqueue events but the function fails at a
later stage.

Events should not be sent until we know we're not rolling back.

Fixes part of #30489.
This commit is contained in:
Prakhar Pratyush 2024-08-16 17:35:27 +05:30 committed by Tim Abbott
parent 62eb850423
commit 32a4a112b1
1 changed files with 13 additions and 13 deletions

View File

@ -155,24 +155,24 @@ def do_mark_stream_messages_as_read(
return count return count
@transaction.atomic(savepoint=False)
def do_mark_muted_user_messages_as_read( def do_mark_muted_user_messages_as_read(
user_profile: UserProfile, user_profile: UserProfile,
muted_user: UserProfile, muted_user: UserProfile,
) -> int: ) -> int:
with transaction.atomic(savepoint=False): query = (
query = ( UserMessage.select_for_update_query()
UserMessage.select_for_update_query() .filter(user_profile=user_profile, message__sender=muted_user)
.filter(user_profile=user_profile, message__sender=muted_user) .extra(where=[UserMessage.where_unread()]) # noqa: S610
.extra(where=[UserMessage.where_unread()]) # noqa: S610 )
) message_ids = list(query.values_list("message_id", flat=True))
message_ids = list(query.values_list("message_id", flat=True))
if len(message_ids) == 0: if len(message_ids) == 0:
return 0 return 0
count = query.update( count = query.update(
flags=F("flags").bitor(UserMessage.flags.read), flags=F("flags").bitor(UserMessage.flags.read),
) )
event = asdict( event = asdict(
ReadMessagesEvent( ReadMessagesEvent(
@ -182,7 +182,7 @@ def do_mark_muted_user_messages_as_read(
) )
event_time = timezone_now() event_time = timezone_now()
send_event(user_profile.realm, event, [user_profile.id]) send_event_on_commit(user_profile.realm, event, [user_profile.id])
do_clear_mobile_push_notifications_for_ids([user_profile.id], message_ids) do_clear_mobile_push_notifications_for_ids([user_profile.id], message_ids)
do_increment_logging_stat( do_increment_logging_stat(