From a018f2611b0132867729e6d009d9802d9fdea985 Mon Sep 17 00:00:00 2001 From: Lauryn Menard Date: Thu, 7 Dec 2023 19:03:21 +0100 Subject: [PATCH] push-notifications: Update payload for realm name and event string. Adds `user.realm.string_id` as the realm name to the base payload for notifications. Uses this realm name in the body of the alert in the `apns_data`. Changes the event string from "test-by-device-token" to "test". Fixes #28075. --- api_docs/changelog.md | 7 +++++++ version.py | 2 +- zerver/lib/push_notifications.py | 8 ++++++-- zerver/openapi/zulip.yaml | 8 +++++++- zerver/tests/test_push_notifications.py | 27 +++++++++++++++++++++---- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/api_docs/changelog.md b/api_docs/changelog.md index 333ce0132c..50efdf400b 100644 --- a/api_docs/changelog.md +++ b/api_docs/changelog.md @@ -20,6 +20,13 @@ format used by the Zulip server that they are interacting with. ## Changes in Zulip 8.0 +**Feature level 234** + +* Mobile push notifications now include a `realm_name` field. +* [`POST /mobile_push/test_notification`](/api/test-notify) now sends + a test notification with `test` rather than `test-by-device-token` + in the `event` field. + **Feature level 233** * [`POST /register`](/api/register-queue), [`GET /events`](/api/get-events): diff --git a/version.py b/version.py index 0c9c9e7995..3f95bc0aa2 100644 --- a/version.py +++ b/version.py @@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3" # Changes should be accompanied by documentation explaining what the # new level means in api_docs/changelog.md, as well as "**Changes**" # entries in the endpoint's documentation in `zulip.yaml`. -API_FEATURE_LEVEL = 233 +API_FEATURE_LEVEL = 234 # 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 916b441eb6..187710e9f2 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -915,6 +915,7 @@ def get_base_payload(user_profile: UserProfile) -> Dict[str, Any]: data["server"] = settings.EXTERNAL_HOST data["realm_id"] = user_profile.realm.id data["realm_uri"] = user_profile.realm.uri + data["realm_name"] = user_profile.realm.name data["user_id"] = user_profile.id return data @@ -1405,7 +1406,7 @@ def send_test_push_notification_directly_to_devices( remote: Optional["RemoteZulipServer"] = None, ) -> None: payload = copy.deepcopy(base_payload) - payload["event"] = "test-by-device-token" + payload["event"] = "test" apple_devices = [device for device in devices if device.kind == PushDeviceToken.APNS] android_devices = [device for device in devices if device.kind == PushDeviceToken.GCM] @@ -1415,10 +1416,13 @@ def send_test_push_notification_directly_to_devices( android_payload = copy.deepcopy(payload) realm_uri = base_payload["realm_uri"] + realm_name = base_payload["realm_name"] apns_data = { "alert": { "title": _("Test notification"), - "body": _("This is a test notification from {realm_uri}.").format(realm_uri=realm_uri), + "body": _("This is a test notification from {realm_name} ({realm_uri}).").format( + realm_name=realm_name, realm_uri=realm_uri + ), }, "sound": "default", "custom": {"zulip": apple_payload}, diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 1bddf90069..7beaf8609b 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -9503,7 +9503,13 @@ paths: This endpoint allows a user to trigger a test push notification to their selected mobile device, or all their mobile devices. - **Changes**: New in Zulip 8.0 (feature level 217). + **Changes**: Starting with Zulip 8.0 (feature level 234), test + notifications sent via this endpoint use `test` rather than + `test-by-device-token` in the `event` field. (All mobile push + notifications also now include a `realm_name` field as well as + of this feature level). + + New in Zulip 8.0 (feature level 217). parameters: - name: token in: query diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index 6faddd4752..61a740a797 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -197,9 +197,10 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase): expected_android_payload = { "server": "testserver", "realm_id": user.realm_id, + "realm_name": "Zulip Dev", "realm_uri": "http://zulip.testserver", "user_id": user.id, - "event": "test-by-device-token", + "event": "test", "time": datetime_to_timestamp(time_now), } expected_gcm_options = {"priority": "high"} @@ -220,16 +221,17 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase): expected_apple_payload = { "alert": { "title": "Test notification", - "body": "This is a test notification from http://zulip.testserver.", + "body": "This is a test notification from Zulip Dev (http://zulip.testserver).", }, "sound": "default", "custom": { "zulip": { "server": "testserver", "realm_id": user.realm_id, + "realm_name": "Zulip Dev", "realm_uri": "http://zulip.testserver", "user_id": user.id, - "event": "test-by-device-token", + "event": "test", } }, } @@ -302,9 +304,10 @@ class SendTestPushNotificationEndpointTest(BouncerTestCase): expected_payload = { "server": "testserver", "realm_id": user.realm_id, + "realm_name": "Zulip Dev", "realm_uri": "http://zulip.testserver", "user_id": user.id, - "event": "test-by-device-token", + "event": "test", "time": datetime_to_timestamp(time_now), } expected_gcm_options = {"priority": "high"} @@ -2390,6 +2393,7 @@ class HandlePushNotificationTest(PushNotificationTest): "zulip": { "server": "testserver", "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": "http://zulip.testserver", "user_id": self.user_profile.id, "event": "remove", @@ -2400,6 +2404,7 @@ class HandlePushNotificationTest(PushNotificationTest): { "server": "testserver", "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": "http://zulip.testserver", "user_id": self.user_profile.id, "event": "remove", @@ -2453,6 +2458,7 @@ class HandlePushNotificationTest(PushNotificationTest): { "server": "testserver", "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": "http://zulip.testserver", "user_id": self.user_profile.id, "event": "remove", @@ -2470,6 +2476,7 @@ class HandlePushNotificationTest(PushNotificationTest): "zulip": { "server": "testserver", "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": "http://zulip.testserver", "user_id": self.user_profile.id, "event": "remove", @@ -2949,6 +2956,7 @@ class TestGetAPNsPayload(PushNotificationTest): "sender_id": self.sender.id, "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "time": datetime_to_timestamp(message.date_sent), @@ -2992,6 +3000,7 @@ class TestGetAPNsPayload(PushNotificationTest): "sender_id": self.sender.id, "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "time": datetime_to_timestamp(message.date_sent), @@ -3024,6 +3033,7 @@ class TestGetAPNsPayload(PushNotificationTest): "topic": message.topic_name(), "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": self.sender.id, "time": datetime_to_timestamp(message.date_sent), @@ -3062,6 +3072,7 @@ class TestGetAPNsPayload(PushNotificationTest): "topic": message.topic_name(), "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "time": datetime_to_timestamp(message.date_sent), @@ -3099,6 +3110,7 @@ class TestGetAPNsPayload(PushNotificationTest): "topic": message.topic_name(), "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "mentioned_user_group_id": user_group.id, @@ -3137,6 +3149,7 @@ class TestGetAPNsPayload(PushNotificationTest): "topic": message.topic_name(), "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "time": datetime_to_timestamp(message.date_sent), @@ -3198,6 +3211,7 @@ class TestGetAPNsPayload(PushNotificationTest): "sender_id": self.sender.id, "server": settings.EXTERNAL_HOST, "realm_id": self.sender.realm.id, + "realm_name": self.sender.realm.name, "realm_uri": self.sender.realm.uri, "user_id": user_profile.id, "time": datetime_to_timestamp(message.date_sent), @@ -3247,6 +3261,7 @@ class TestGetAPNsPayload(PushNotificationTest): "topic": message.topic_name(), "server": settings.EXTERNAL_HOST, "realm_id": hamlet.realm.id, + "realm_name": hamlet.realm.name, "realm_uri": hamlet.realm.uri, "user_id": polonius.id, "time": datetime_to_timestamp(message.date_sent), @@ -3285,6 +3300,7 @@ class TestGetGCMPayload(PushNotificationTest): "content_truncated": truncate_content, "server": settings.EXTERNAL_HOST, "realm_id": hamlet.realm.id, + "realm_name": hamlet.realm.name, "realm_uri": hamlet.realm.uri, "sender_id": hamlet.id, "sender_email": hamlet.email, @@ -3343,6 +3359,7 @@ class TestGetGCMPayload(PushNotificationTest): "content_truncated": False, "server": settings.EXTERNAL_HOST, "realm_id": hamlet.realm.id, + "realm_name": hamlet.realm.name, "realm_uri": hamlet.realm.uri, "sender_id": hamlet.id, "sender_email": hamlet.email, @@ -3375,6 +3392,7 @@ class TestGetGCMPayload(PushNotificationTest): "content_truncated": False, "server": settings.EXTERNAL_HOST, "realm_id": hamlet.realm.id, + "realm_name": hamlet.realm.name, "realm_uri": hamlet.realm.uri, "sender_id": hamlet.id, "sender_email": hamlet.email, @@ -3424,6 +3442,7 @@ class TestGetGCMPayload(PushNotificationTest): "content_truncated": False, "server": settings.EXTERNAL_HOST, "realm_id": hamlet.realm.id, + "realm_name": hamlet.realm.name, "realm_uri": hamlet.realm.uri, "sender_id": hamlet.id, "sender_email": f"user{hamlet.id}@zulip.testserver",