message_flags: Update 'do_update_message_flags' to send event on commit.

Earlier, we were using 'send_event' in do_update_message_flags
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 18:00:41 +05:30 committed by Tim Abbott
parent 32a4a112b1
commit 9026e6ecc1
2 changed files with 38 additions and 41 deletions

View File

@ -273,7 +273,7 @@ def do_update_message_flags(
flagattr = getattr(UserMessage.flags, flag)
flag_target = flagattr if is_adding else 0
with transaction.atomic(savepoint=False):
with transaction.atomic(durable=True):
if flag == "read" and not is_adding:
# We have an invariant that all stream messages marked as
# unread must be in streams the user is subscribed to.
@ -374,9 +374,11 @@ def do_update_message_flags(
# details on the messages required to update the client's
# `unread_msgs` data structure.
raw_unread_data = get_raw_unread_data(user_profile, messages)
event["message_details"] = format_unread_message_details(user_profile.id, raw_unread_data)
event["message_details"] = format_unread_message_details(
user_profile.id, raw_unread_data
)
send_event(user_profile.realm, event, [user_profile.id])
send_event_on_commit(user_profile.realm, event, [user_profile.id])
if flag == "read" and is_adding:
event_time = timezone_now()

View File

@ -2,7 +2,7 @@ from typing import TYPE_CHECKING, Any
from unittest import mock
import orjson
from django.db import connection, transaction
from django.db import connection
from typing_extensions import override
from zerver.actions.message_flags import do_update_message_flags
@ -1603,7 +1603,6 @@ class MessageAccessTests(ZulipTestCase):
# Starring private stream messages you didn't receive fails.
self.login("cordelia")
with transaction.atomic():
result = self.change_star(message_ids)
self.assert_json_error(result, "Invalid message(s)")
@ -1619,7 +1618,6 @@ class MessageAccessTests(ZulipTestCase):
# can't see it if you didn't receive the message and are
# not subscribed.
self.login("cordelia")
with transaction.atomic():
result = self.change_star(message_ids)
self.assert_json_error(result, "Invalid message(s)")
@ -1661,7 +1659,6 @@ class MessageAccessTests(ZulipTestCase):
guest_user = self.example_user("polonius")
self.login_user(guest_user)
with transaction.atomic():
result = self.change_star(message_id)
self.assert_json_error(result, "Invalid message(s)")
@ -1693,14 +1690,12 @@ class MessageAccessTests(ZulipTestCase):
guest_user = self.example_user("polonius")
self.login_user(guest_user)
with transaction.atomic():
result = self.change_star(message_id)
self.assert_json_error(result, "Invalid message(s)")
# Guest user can't access messages of subscribed private streams if
# history is not public to subscribers
self.subscribe(guest_user, stream_name)
with transaction.atomic():
result = self.change_star(message_id)
self.assert_json_error(result, "Invalid message(s)")