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_notifications: Replace `pm_users` field with `recipient_user_ids`.

Prakhar Pratyush7ebaa9f4e4a2
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 4d9477dd5e6..fcd214b72c6 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,14 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 429**
+
+* Replaced the `pm_users` field with `recipient_user_ids` in
+[E2EE mobile push notifications payload](/api/mobile-notifications)
+for group direct message. Previously, `pm_users` was included only
+for group DMs; `recipient_user_ids` is present for both 1:1 and
+group DM conversations.
+
**Feature level 428**
* [`GET /events`](/api/get-events): When a user is deactivated,
diff --git a/api_docs/mobile-notifications.md b/api_docs/mobile-notifications.md
index 4709f49721b..8b1e045dbec 100644
--- a/api_docs/mobile-notifications.md
+++ b/api_docs/mobile-notifications.md
@@ -59,10 +59,10 @@ Sample JSON data that gets encrypted:
{
"content": "test content",
"message_id": 46,
- "pm_users": "6,10,12,15",
"realm_name": "Zulip Dev",
"realm_url": "http://zulip.testserver",
"recipient_type": "direct",
+ "recipient_user_ids": [6,10,12,15],
"sender_avatar_url": "https://secure.gravatar.com/avatar/818c212b9f8830dfef491b3f7da99a14?d=identicon&version=1",
"sender_full_name": "aaron",
"sender_id": 6,
@@ -72,14 +72,16 @@ Sample JSON data that gets encrypted:
}
```
-- **Group direct messages**: The `pm_users` string field is only
-present for group direct messages, containing a sorted comma-separated
-list of all user IDs in the group direct message conversation,
-including both `user_id` and `sender_id`.
+- The `recipient_user_ids` field is a sorted array of all user IDs in
+the direct message conversation, including both `user_id` and
+`sender_id`.
-**Changes**: New in Zulip 11.0 (feature level 413).
+**Changes**: In Zulip 12.0 (feature level 429), replaced the
+`pm_users` field with `recipient_user_ids`. The old `pm_users` field
+was only present for group DMs, and was a string containing a
+comma-separated list of sorted user IDs.
-### New group direct message
+New in Zulip 11.0 (feature level 413).
### Remove notifications
diff --git a/version.py b/version.py
index b64b2dba78c..6d4f62554a0 100644
--- a/version.py
+++ b/version.py
@@ -35,7 +35,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# new level means in api_docs/changelog.md, as well as "**Changes**"
# entries in the endpoint's documentation in `zulip.yaml`.
-API_FEATURE_LEVEL = 428
+API_FEATURE_LEVEL = 429
# Bump the minor PROVISION_VERSION to indicate that folks should provision
# only when going from an old version of the code to a newer version. Bump
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index 099e82656f3..5d15f5e1705 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -1032,16 +1032,29 @@ def get_message_payload(
data["channel_id"] = channel_id
data["topic"] = get_topic_display_name(message.topic_name(), user_profile.default_language)
- elif message.recipient.type == Recipient.DIRECT_MESSAGE_GROUP:
- data["recipient_type"] = "private" if for_legacy_clients else "direct"
- # For group DMs, we need to fetch the users for the pm_users field.
- # Note that this doesn't do a separate database query, because both
- # functions use the get_display_recipient_by_id cache.
- recipients = get_display_recipient(message.recipient)
- if len(recipients) > 2:
- data["pm_users"] = direct_message_group_users(message.recipient.id)
- else: # Recipient.PERSONAL
- data["recipient_type"] = "private" if for_legacy_clients else "direct"
+ else:
+ assert message.recipient.type in [Recipient.PERSONAL, Recipient.DIRECT_MESSAGE_GROUP]
+ if for_legacy_clients:
+ data["recipient_type"] = "private"
+ if message.recipient.type == Recipient.DIRECT_MESSAGE_GROUP:
+ # For group DMs, we need to fetch the users for the pm_users field.
+ # Note that this doesn't do a separate database query, because both
+ # functions use the get_display_recipient_by_id cache.
+ recipients = get_display_recipient(message.recipient)
+ if len(recipients) > 2:
+ data["pm_users"] = direct_message_group_users(message.recipient.id)
+ else:
+ data["recipient_type"] = "direct"
+ # For 1:1 and group DMs, we need to fetch the users for the `recipient_user_ids`
+ # field. Note that this doesn't do a separate database query, because it uses
+ # the `get_display_recipient_by_id` cache.
+ display_recipients = get_display_recipient(message.recipient)
+ recipient_user_ids = set(
+ display_recipient["id"] for display_recipient in display_recipients
+ )
+ if len(recipient_user_ids) == 1: # Recipient.PERSONAL
+ recipient_user_ids.add(message.sender_id)
+ data["recipient_user_ids"] = sorted(recipient_user_ids)
return data
diff --git a/zerver/tests/test_e2ee_push_notifications.py b/zerver/tests/test_e2ee_push_notifications.py
index ba6f9af401f..a2dc84f08d6 100644
--- a/zerver/tests/test_e2ee_push_notifications.py
+++ b/zerver/tests/test_e2ee_push_notifications.py
@@ -733,6 +733,43 @@ class SendPushNotificationTest(E2EEPushNotificationTestCase):
"user_id": hamlet.id,
"sender_id": aaron.id,
"recipient_type": "direct",
+ "recipient_user_ids": sorted([aaron.id, hamlet.id]),
+ "type": "message",
+ "message_id": message_id,
+ "time": datetime_to_timestamp(time_now),
+ "content": "test content",
+ "sender_full_name": aaron.full_name,
+ "sender_avatar_url": absolute_avatar_url(aaron),
+ }
+ with mock.patch("zerver.lib.push_notifications.send_push_notifications") as m:
+ handle_push_notification(hamlet.id, missed_message)
+
+ self.assertEqual(m.call_args.args[1], expected_payload_data_to_encrypt)
+
+ def test_payload_data_to_encrypt_group_direct_message(self) -> None:
+ aaron = self.example_user("aaron")
+ cordelia = self.example_user("cordelia")
+ hamlet = self.example_user("hamlet")
+ realm = get_realm("zulip")
+
+ time_now = now()
+ self.register_push_devices_for_notification()
+ with time_machine.travel(time_now, tick=False):
+ message_id = self.send_group_direct_message(
+ from_user=aaron, to_users=[hamlet, cordelia], skip_capture_on_commit_callbacks=True
+ )
+ missed_message = {
+ "message_id": message_id,
+ "trigger": NotificationTriggers.DIRECT_MESSAGE,
+ }
+
+ expected_payload_data_to_encrypt = {
+ "realm_url": realm.url,
+ "realm_name": realm.name,
+ "user_id": hamlet.id,
+ "sender_id": aaron.id,
+ "recipient_type": "direct",
+ "recipient_user_ids": sorted([aaron.id, cordelia.id, hamlet.id]),
"type": "message",
"message_id": message_id,
"time": datetime_to_timestamp(time_now),