Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
push_notifications: Parse `push/e2ee/notify` response using Pydantic.
Prakhar Pratyush5a37942c6176
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.pyindex 1aac367121a..124a5e27474 100644--- a/zerver/lib/push_notifications.py+++ b/zerver/lib/push_notifications.py@@ -8,7 +8,7 @@ from collections.abc import Mapping, Sequencefrom dataclasses import asdict, dataclass, fieldfrom email.headerregistry import Addressfrom functools import cache-from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypeAlias, Union+from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypeAlias, Union, castimport lxml.htmlimport orjson@@ -28,7 +28,8 @@ from firebase_admin import messaging as firebase_messagingfrom firebase_admin.messaging import UnregisteredError as FCMUnregisteredErrorfrom nacl.encoding import Base64Encoderfrom nacl.public import PublicKey, SealedBox-from typing_extensions import NotRequired, TypedDict, override+from pydantic import TypeAdapter+from typing_extensions import TypedDict, overridefrom analytics.lib.counts import COUNT_STATS, do_increment_logging_statfrom zerver.actions.realm_settings import (@@ -1411,7 +1412,13 @@ class SendNotificationResponseData(TypedDict):android_successfully_sent_count: intapple_successfully_sent_count: intdelete_device_ids: list[int]- realm_push_status: NotRequired[RealmPushStatusDict]+++class SendNotificationRemoteResponseData(SendNotificationResponseData):+ realm_push_status: RealmPushStatusDict+++send_notification_remote_response_data_adapter = TypeAdapter(SendNotificationRemoteResponseData)FCMPriority: TypeAlias = Literal["high", "normal"]@@ -1521,10 +1528,11 @@ def send_push_notifications(# Send push notificationtry:+ response_data: SendNotificationResponseData | SendNotificationRemoteResponseDataif settings.ZILENCER_ENABLED:from zilencer.lib.push_notifications import send_e2ee_push_notifications- response_data: SendNotificationResponseData = send_e2ee_push_notifications(+ response_data = send_e2ee_push_notifications(push_requests,realm=user_profile.realm,)@@ -1534,16 +1542,7 @@ def send_push_notifications("push_requests": [asdict(push_request) for push_request in push_requests],}result = send_json_to_push_bouncer("POST", "push/e2ee/notify", post_data)- assert isinstance(result["android_successfully_sent_count"], int) # for mypy- assert isinstance(result["apple_successfully_sent_count"], int) # for mypy- assert isinstance(result["delete_device_ids"], list) # for mypy- assert isinstance(result["realm_push_status"], dict) # for mypy- response_data = {- "android_successfully_sent_count": result["android_successfully_sent_count"],- "apple_successfully_sent_count": result["apple_successfully_sent_count"],- "delete_device_ids": result["delete_device_ids"],- "realm_push_status": result["realm_push_status"], # type: ignore[typeddict-item] # TODO: Can't use isinstance() with TypedDict type- }+ response_data = send_notification_remote_response_data_adapter.validate_python(result)except (MissingRemoteRealmError, PushNotificationsDisallowedByBouncerError) as e:reason = e.reason if isinstance(e, PushNotificationsDisallowedByBouncerError) else e.msglogger.warning("Bouncer refused to send E2EE push notification: %s", reason)@@ -1593,8 +1592,11 @@ def send_push_notifications(apple_successfully_sent_count,)- realm_push_status_dict = response_data.get("realm_push_status")- if realm_push_status_dict is not None:+ if "realm_push_status" in response_data:+ # Cannot use `isinstance` with `TypedDict`s to make mypy know+ # which of the `TypedDict`s in the Union this is - so just cast it.+ response_data = cast(SendNotificationRemoteResponseData, response_data)+ realm_push_status_dict = response_data["realm_push_status"]can_push = realm_push_status_dict["can_push"]do_set_realm_property(user_profile.realm,