Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

notifications: Fix AssertionError on moving messages with bot mentions.

Prakhar Pratyush472ca78bc26f
diff --git a/zerver/lib/notification_data.py b/zerver/lib/notification_data.py
index c64846118a0..f09d7ffab95 100644
--- a/zerver/lib/notification_data.py
+++ b/zerver/lib/notification_data.py
@@ -134,7 +134,7 @@ class UserMessageNotificationsData:
#
# Remove the `if` block when one can no longer directly upgrade from 11.x to main.
if push_device_registered_user_ids is None:
- push_device_registered = True
+ push_device_registered = True # nocoverage
else:
push_device_registered = user_id in push_device_registered_user_ids
diff --git a/zerver/tests/test_message_edit_notifications.py b/zerver/tests/test_message_edit_notifications.py
index e7c1082b5d9..36b35b6c75d 100644
--- a/zerver/tests/test_message_edit_notifications.py
+++ b/zerver/tests/test_message_edit_notifications.py
@@ -23,7 +23,7 @@ class EditMessageSideEffectsTest(ZulipTestCase):
cordelia = self.example_user("cordelia")
self.register_push_device(cordelia.id)
- def _assert_update_does_not_notify_anybody(self, message_id: int, **request: str) -> None:
+ def _assert_update_does_not_notify_anybody(self, message_id: int, **request: str | int) -> None:
url = "/json/messages/" + str(message_id)
with (
@@ -727,3 +727,58 @@ class EditMessageSideEffectsTest(ZulipTestCase):
self.assertEqual(get_apns_badge_count(mentioned_user), 0)
self.assertEqual(get_apns_badge_count_future(mentioned_user), 1)
+
+ def _send_notifiable_message_and_login_editor(self) -> int:
+ message_id = self._login_and_send_original_stream_message(
+ content="hello @**Cordelia, Lear's daughter**",
+ )
+
+ iago = self.example_user("iago")
+ self.subscribe(iago, "Scotland")
+ self.login_user(iago)
+ return message_id
+
+ # The next three tests cover notifications for topic and channel
+ # edits. It seems obvious that such edits cannot notify anyone,
+ # but historically we had such bugs - see #39888.
+ def test_topic_edit_does_not_notify_anybody(self) -> None:
+ message_id = self._send_notifiable_message_and_login_editor()
+
+ self._assert_update_does_not_notify_anybody(
+ message_id=message_id,
+ topic="new topic",
+ )
+
+ def test_channel_edit_does_not_notify_anybody(self) -> None:
+ message_id = self._send_notifiable_message_and_login_editor()
+
+ self._assert_update_does_not_notify_anybody(
+ message_id=message_id,
+ stream_id=self.get_stream_id("Denmark"),
+ )
+
+ def test_content_edit_that_also_moves_topic_notifies(self) -> None:
+ message_id = self._login_and_send_original_stream_message(content="no mention")
+
+ info = self._get_queued_data_for_message_update(
+ message_id=message_id,
+ content="now we mention @**Cordelia, Lear's daughter**",
+ topic="new topic",
+ )
+
+ cordelia = self.example_user("cordelia")
+ hamlet = self.example_user("hamlet")
+ expected_enqueue_kwargs = self.get_maybe_enqueue_notifications_parameters(
+ user_id=cordelia.id,
+ acting_user_id=hamlet.id,
+ message_id=message_id,
+ mention_email_notify=True,
+ mention_push_notify=True,
+ already_notified={},
+ )
+ self.assertEqual(info["enqueue_kwargs"], expected_enqueue_kwargs)
+
+ queue_names = [queue_message["queue_name"] for queue_message in info["queue_messages"]]
+ self.assertEqual(
+ queue_names, ["missedmessage_mobile_notifications", "missedmessage_emails"]
+ )
diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py
index 4e6ef3171fd..6324bab60c0 100644
--- a/zerver/tornado/event_queue.py
+++ b/zerver/tornado/event_queue.py
@@ -1503,7 +1503,7 @@ def process_message_update_event(
online_push_user_ids = set(event_template.pop("online_push_user_ids", []))
stream_name = event_template.get("stream_name")
message_id = event_template["message_id"]
- rendering_only_update = event_template["rendering_only"]
+ content_edited = "orig_content" in event_template
# TODO/compatibility: We need to set `push_device_registered_user_ids` to None
# for update_message events prior to the introduction of `push_device_registered_user_ids`
@@ -1523,15 +1523,11 @@ def process_message_update_event(
if key != "id":
user_event[key] = user_data[key]
- # Events where `rendering_only_update` is True come from the
- # do_update_embedded_data code path, and represent rendering
- # previews; there should be no real content changes.
- # Therefore, we know only events where `rendering_only_update`
- # is False possibly send notifications.
- if not rendering_only_update:
- # The user we'll get here will be the sender if the message's
- # content was edited, and the editor for topic edits. That's
- # the correct "acting_user" for both cases.
+ # Only events where content edit is involved possibly send notifications.
+ # Skip it for topic and/or channel edit or when a message's content
+ # has a rendering update.
+ if content_edited:
+ # Message's sender is the acting user here, because only they can edit content.
acting_user_id = event_template["user_id"]
flags: Collection[str] = user_event["flags"]