Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
push_notification: Remove notification if user no longer mentioned.
Aman Agrawal984c2d2777ac
diff --git a/zerver/lib/actions.py b/zerver/lib/actions.pyindex e5d03192265..1a71c92fad3 100644--- a/zerver/lib/actions.py+++ b/zerver/lib/actions.py@@ -4168,6 +4168,26 @@ def do_mark_stream_messages_as_read(user_profile: UserProfile,None, event_time, increment=min(1, count))return count+def do_update_mobile_push_notification(message: Message, prior_mention_user_ids: Set[int]) -> None:+ # Called during the message edit code path to remove mobile push+ # notifications for users who are no longer mentioned following+ # the edit. See #15428 for details.+ #+ # A perfect implementation would also support updating the message+ # in a sent notification if a message was edited to mention a+ # group rather than a user (or vise versa), though it is likely+ # not worth the effort to do such a change.+ #+ # This implementation is subtly incorrect, in that it will remove+ # the push notification for a user who was mentioned (and then the+ # mention removed) in a stream that they have configured to+ # receive push notifications by default. That is likely worth fixing.+ if not message.is_stream_message():+ return++ remove_notify_users = prior_mention_user_ids - message.mentions_user_ids+ do_clear_mobile_push_notifications_for_ids(list(remove_notify_users), [message.id])+def do_clear_mobile_push_notifications_for_ids(user_profile_ids: List[int],message_ids: List[int]) -> None:# This functions supports clearing notifications for several users@@ -4532,6 +4552,8 @@ def do_update_message(user_profile: UserProfile, message: Message,else:event['wildcard_mention_user_ids'] = []+ do_update_mobile_push_notification(message, prior_mention_user_ids)+if topic_name is not None or new_stream is not None:orig_topic_name = message.topic_name()event["propagate_mode"] = propagate_modediff --git a/zerver/tests/test_message_edit_notifications.py b/zerver/tests/test_message_edit_notifications.pyindex 00800c1e78e..17eef61926e 100644--- a/zerver/tests/test_message_edit_notifications.py+++ b/zerver/tests/test_message_edit_notifications.py@@ -4,6 +4,7 @@ from unittest import mockfrom django.utils.timezone import now as timezone_nowfrom zerver.lib.actions import get_client+from zerver.lib.push_notifications import get_apns_badge_countfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.models import Subscription, UserPresencefrom zerver.tornado.event_queue import maybe_enqueue_notifications@@ -493,3 +494,40 @@ class EditMessageSideEffectsTest(ZulipTestCase):# Because Cordelia is FULLY present, we don't need to send any offline# push notifications or missed message emails.self.assertEqual(len(info['queue_messages']), 0)++ def test_clear_notification_when_mention_removed(self) -> None:+ mentioned_user = self.example_user('iago')+ self.assertEqual(get_apns_badge_count(mentioned_user), 0)++ with mock.patch('zerver.lib.push_notifications.push_notifications_enabled', return_value=True):+ message_id = self._login_and_send_original_stream_message(+ content="@**Iago**",+ )++ self.assertEqual(get_apns_badge_count(mentioned_user), 1)++ self._get_queued_data_for_message_update(+ message_id=message_id,+ content="Removed mention"+ )++ self.assertEqual(get_apns_badge_count(mentioned_user), 0)++ def test_clear_notification_when_group_mention_removed(self) -> None:+ group_mentioned_user = self.example_user('cordelia')+ self.assertEqual(get_apns_badge_count(group_mentioned_user), 0)++ with mock.patch('zerver.lib.push_notifications.push_notifications_enabled', return_value=True):+ message_id = self._login_and_send_original_stream_message(+ content="Hello @*hamletcharacters*",+ )++ self.assertEqual(get_apns_badge_count(group_mentioned_user), 1)++ self._get_queued_data_for_message_update(+ message_id=message_id,+ content="Removed group mention",+ expect_short_circuit=True,+ )++ self.assertEqual(get_apns_badge_count(group_mentioned_user), 0)