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.

push_notification: Skip clearing notif for users without push devices.

Prakhar Pratyush70530bf84095
diff --git a/zerver/actions/message_flags.py b/zerver/actions/message_flags.py
index 4a9f5f9848e..1515c96cc1b 100644
--- a/zerver/actions/message_flags.py
+++ b/zerver/actions/message_flags.py
@@ -19,7 +19,7 @@ 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
-from zerver.models import Message, Recipient, UserMessage, UserProfile
+from zerver.models import Message, PushDevice, PushDeviceToken, Recipient, UserMessage, UserProfile
from zerver.tornado.django_api import send_event_on_commit, send_event_rollback_unsafe
@@ -244,7 +244,7 @@ def do_clear_mobile_push_notifications_for_ids(
.extra( # noqa: S610
where=[UserMessage.where_active_push_notification()],
)
- .values_list("user_profile_id", "message_id")
+ .values_list("id", "user_profile_id", "message_id")
)
else:
# This block handles clearing notifications when message(s) get deleted.
@@ -256,14 +256,47 @@ def do_clear_mobile_push_notifications_for_ids(
.extra( # noqa: S610
where=[UserMessage.where_active_push_notification()],
)
- .values_list("user_profile_id", "message_id")
+ .values_list("id", "user_profile_id", "message_id")
)
messages_by_user = defaultdict(list)
- for user_id, message_id in notifications_to_update:
+ for unused, user_id, message_id in notifications_to_update:
messages_by_user[user_id].append(message_id)
+ clear_notifications_user_ids = set(messages_by_user.keys())
+ push_device_registered_user_ids = set(
+ PushDeviceToken.objects.filter(user_id__in=clear_notifications_user_ids)
+ .values_list("user_id", flat=True)
+ .union(
+ # Uses index "zerver_pushdevice_user_bouncer_device_id_idx".
+ PushDevice.objects.filter(
+ user_id__in=clear_notifications_user_ids, bouncer_device_id__isnull=False
+ ).values_list("user_id", flat=True)
+ )
+ )
+ push_device_not_registered_user_ids = (
+ clear_notifications_user_ids - push_device_registered_user_ids
+ )
+
+ usermessages_to_update_ids = []
+ for um_id, user_id, unused in notifications_to_update:
+ if user_id in push_device_not_registered_user_ids:
+ usermessages_to_update_ids.append(um_id)
+
+ if usermessages_to_update_ids:
+ BATCH_SIZE = 1000
+ for i in range(0, len(usermessages_to_update_ids), BATCH_SIZE):
+ with transaction.atomic(savepoint=False):
+ UserMessage.select_for_update_query().filter(
+ id__in=usermessages_to_update_ids[i : i + BATCH_SIZE]
+ ).update(
+ flags=F("flags").bitand(~UserMessage.flags.active_mobile_push_notification)
+ )
+
for user_profile_id, event_message_ids in messages_by_user.items():
+ if user_profile_id in push_device_not_registered_user_ids:
+ continue
+
notice = {
"type": "remove",
"user_profile_id": user_profile_id,
diff --git a/zerver/models/push_notifications.py b/zerver/models/push_notifications.py
index cac17ea675e..50486f4a3bf 100644
--- a/zerver/models/push_notifications.py
+++ b/zerver/models/push_notifications.py
@@ -131,7 +131,8 @@ class PushDevice(AbstractPushDevice):
]
indexes = [
models.Index(
- # Used in 'send_push_notifications' and `send_e2ee_test_push_notification_api`.
+ # Used in 'send_push_notifications', `send_e2ee_test_push_notification_api`,
+ # 'get_recipient_info', and 'do_clear_mobile_push_notifications_for_ids'.
fields=["user", "bouncer_device_id"],
condition=Q(bouncer_device_id__isnull=False),
name="zerver_pushdevice_user_bouncer_device_id_idx",
diff --git a/zerver/tests/test_message_flags.py b/zerver/tests/test_message_flags.py
index 66b5accc572..3413fea6ab3 100644
--- a/zerver/tests/test_message_flags.py
+++ b/zerver/tests/test_message_flags.py
@@ -9,6 +9,7 @@ from typing_extensions import override
from zerver.actions.message_flags import do_update_message_flags
from zerver.actions.streams import do_change_stream_group_based_setting, do_change_stream_permission
from zerver.actions.user_groups import check_add_user_group
+from zerver.actions.user_settings import do_change_user_setting
from zerver.actions.user_topics import do_set_user_topic_visibility_policy
from zerver.lib.fix_unreads import fix, fix_unsubscribed
from zerver.lib.message import (
@@ -30,6 +31,7 @@ from zerver.lib.test_helpers import get_subscription
from zerver.lib.user_message import DEFAULT_HISTORICAL_FLAGS, create_historical_user_messages
from zerver.models import (
Message,
+ PushDevice,
Recipient,
Stream,
Subscription,
@@ -952,6 +954,54 @@ class PushNotificationMarkReadFlowsTest(ZulipTestCase):
mock_push_notifications.assert_called()
mock_send_push_notifications.assert_called()
+ @mock.patch("zerver.lib.push_notifications.send_push_notifications")
+ @mock.patch("zerver.lib.push_notifications.push_notifications_configured", return_value=True)
+ def test_skip_clear_notification_for_user_without_push_device(
+ self,
+ mock_push_notifications: mock.MagicMock,
+ mock_send_push_notifications: mock.MagicMock,
+ ) -> None:
+ hamlet = self.example_user("hamlet")
+ cordelia = self.example_user("cordelia")
+ self.login_user(cordelia)
+ self.register_push_device(cordelia.id)
+ do_change_user_setting(cordelia, "enable_stream_push_notifications", True, acting_user=None)
+
+ # Initially, no active push notifications.
+ self.assertEqual(self.get_mobile_push_notification_ids(cordelia), [])
+
+ verona = self.subscribe(cordelia, "Verona")
+ message_ids = [self.send_stream_message(hamlet, "Verona", str(i)) for i in range(10)]
+
+ # Verify push notifications sent to `cordelia` for `message_ids`
+ self.assertEqual(
+ self.get_mobile_push_notification_ids(cordelia),
+ message_ids,
+ )
+
+ # Device unregistered. Verify that no event to revoke notifications gets
+ # enqueued to `missedmessage_mobile_notifications` and `active_mobile_push_notification`
+ # flag is unset.
+ PushDevice.objects.all().delete()
+ with (
+ mock.patch(
+ "zerver.actions.message_flags.queue_event_on_commit"
+ ) as mock_queue_event_on_commit,
+ self.captureOnCommitCallbacks(execute=True),
+ ):
+ result = self.client_post(
+ "/json/mark_stream_as_read",
+ {
+ "stream_id": str(verona.id),
+ },
+ )
+ self.assert_json_success(result)
+ mock_queue_event_on_commit.assert_not_called()
+ self.assertEqual(
+ self.get_mobile_push_notification_ids(cordelia),
+ [],
+ )
+
class MarkAllAsReadEndpointTest(ZulipTestCase):
def test_mark_all_as_read_endpoint(self) -> None:
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index b532d77af94..87f5ce742be 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -2503,6 +2503,7 @@ class TestClearOnRead(ZulipTestCase):
user_profile_id=hamlet.id,
message_id__in=message_ids,
).update(flags=F("flags").bitor(UserMessage.flags.active_mobile_push_notification))
+ self.register_push_device(hamlet.id)
with mock_queue_publish("zerver.actions.message_flags.queue_event_on_commit") as m:
assert stream.recipient_id is not None