message_edit: Queue event on commit in do_update_message codepath.

'do_update_message' is within a db transaction, this commit
updates the 'do_clear_mobile_push_notifications_for_ids' function
used in 'do_update_message' to queue event on commit.

Events should not be sent until we know we're not rolling back,
otherwise it can lead to a situation where we enqueue events but
the function fails at a later stage.
This commit is contained in:
Prakhar Pratyush 2024-08-16 16:42:13 +05:30 committed by Tim Abbott
parent a3806b4165
commit ed512f06bb
3 changed files with 33 additions and 24 deletions

View File

@ -15,7 +15,7 @@ from zerver.lib.message import (
format_unread_message_details,
get_raw_unread_data,
)
from zerver.lib.queue import queue_json_publish
from zerver.lib.queue import queue_event_on_commit
from zerver.lib.stream_subscription import get_subscribed_stream_recipient_ids_for_user
from zerver.lib.topic import filter_by_topic_name_via_message
from zerver.lib.user_message import DEFAULT_HISTORICAL_FLAGS, create_historical_user_messages
@ -252,9 +252,9 @@ def do_clear_mobile_push_notifications_for_ids(
}
if settings.MOBILE_NOTIFICATIONS_SHARDS > 1: # nocoverage
shard_id = user_profile_id % settings.MOBILE_NOTIFICATIONS_SHARDS + 1
queue_json_publish(f"missedmessage_mobile_notifications_shard{shard_id}", notice)
queue_event_on_commit(f"missedmessage_mobile_notifications_shard{shard_id}", notice)
else:
queue_json_publish("missedmessage_mobile_notifications", notice)
queue_event_on_commit("missedmessage_mobile_notifications", notice)
def do_update_message_flags(

View File

@ -200,9 +200,14 @@ class UnreadCountTests(ZulipTestCase):
def test_update_flags(self) -> None:
self.login("hamlet")
with self.captureOnCommitCallbacks(execute=True):
result = self.client_post(
"/json/messages/flags",
{"messages": orjson.dumps(self.unread_msg_ids).decode(), "op": "add", "flag": "read"},
{
"messages": orjson.dumps(self.unread_msg_ids).decode(),
"op": "add",
"flag": "read",
},
)
self.assert_json_success(result)
@ -843,6 +848,7 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase):
[message_id, second_message_id, third_message_id],
)
with self.captureOnCommitCallbacks(execute=True):
result = self.client_post(
"/json/mark_topic_as_read",
{
@ -857,6 +863,7 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase):
[second_message_id, third_message_id],
)
with self.captureOnCommitCallbacks(execute=True):
result = self.client_post(
"/json/mark_stream_as_read",
{
@ -873,6 +880,7 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase):
[third_message_id, fourth_message_id],
)
with self.captureOnCommitCallbacks(execute=True):
result = self.client_post("/json/mark_all_as_read", {})
self.assertEqual(self.get_mobile_push_notification_ids(user_profile), [])
mock_push_notifications.assert_called()

View File

@ -4087,6 +4087,7 @@ class TestAPNs(PushNotificationTest):
# Mark the messages as read and test whether
# the count decreases correctly.
for i, message_id in enumerate(message_ids):
with self.captureOnCommitCallbacks(execute=True):
do_update_message_flags(user_profile, "add", "read", [message_id])
self.assertEqual(get_apns_badge_count(user_profile), 0)
self.assertEqual(get_apns_badge_count_future(user_profile), num_messages - i - 1)
@ -5073,10 +5074,10 @@ class TestClearOnRead(ZulipTestCase):
message_id__in=message_ids,
).update(flags=F("flags").bitor(UserMessage.flags.active_mobile_push_notification))
with mock_queue_publish("zerver.actions.message_flags.queue_json_publish") as mock_publish:
with mock_queue_publish("zerver.actions.message_flags.queue_event_on_commit") as m:
assert stream.recipient_id is not None
do_mark_stream_messages_as_read(hamlet, stream.recipient_id)
queue_items = [c[0][1] for c in mock_publish.call_args_list]
queue_items = [c[0][1] for c in m.call_args_list]
groups = [item["message_ids"] for item in queue_items]
self.assert_length(groups, 1)