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: Simplify `if device exists` checks.

Hashir Sarwarb88567888179
diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py
index fe423811962..b9b4c0d3497 100644
--- a/zerver/lib/push_notifications.py
+++ b/zerver/lib/push_notifications.py
@@ -106,6 +106,8 @@ APNS_MAX_RETRIES = 3
@statsd_increment("apple_push_notification")
def send_apple_push_notification(user_id: int, devices: List[DeviceToken],
payload_data: Dict[str, Any], remote: bool=False) -> None:
+ if not devices:
+ return
# We lazily do the APNS imports as part of optimizing Zulip's base
# import time; since these are only needed in the push
# notification queue worker, it's best to only import them in the
@@ -265,6 +267,8 @@ def send_android_push_notification(devices: List[DeviceToken], data: Dict[str, A
options: Additional options to control the GCM message sent.
For details, see `parse_gcm_options`.
"""
+ if not devices:
+ return
if not gcm_client:
logger.debug("Skipping sending a GCM push notification since "
"PUSH_NOTIFICATION_BOUNCER_URL and ANDROID_GCM_API_KEY are both unset")
@@ -819,9 +823,6 @@ def handle_push_notification(user_profile_id: int, missed_message: Dict[str, Any
apple_devices = list(PushDeviceToken.objects.filter(user=user_profile,
kind=PushDeviceToken.APNS))
- if apple_devices:
- send_apple_push_notification(user_profile.id, apple_devices,
- apns_payload)
+ send_apple_push_notification(user_profile.id, apple_devices, apns_payload)
- if android_devices:
- send_android_push_notification(android_devices, gcm_payload, gcm_options)
+ send_android_push_notification(android_devices, gcm_payload, gcm_options)
diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py
index 9157827a0fa..8385c76aff4 100644
--- a/zerver/tests/test_push_notifications.py
+++ b/zerver/tests/test_push_notifications.py
@@ -1176,6 +1176,7 @@ class TestAPNs(PushNotificationTest):
zerver.lib.push_notifications._apns_client = None
def test_not_configured(self) -> None:
+ self.setup_apns_tokens()
with mock.patch('zerver.lib.push_notifications.get_apns_client') as mock_get, \
mock.patch('zerver.lib.push_notifications.logger') as mock_logging:
mock_get.return_value = None
diff --git a/zilencer/views.py b/zilencer/views.py
index 26e6530688c..0f6abf1109a 100644
--- a/zilencer/views.py
+++ b/zilencer/views.py
@@ -166,11 +166,9 @@ def remote_server_notify_push(request: HttpRequest, entity: Union[UserProfile, R
server=server,
))
- if android_devices:
- send_android_push_notification(android_devices, gcm_payload, gcm_options, remote=True)
+ send_android_push_notification(android_devices, gcm_payload, gcm_options, remote=True)
- if apple_devices:
- send_apple_push_notification(user_id, apple_devices, apns_payload, remote=True)
+ send_apple_push_notification(user_id, apple_devices, apns_payload, remote=True)
return json_success()