From 695d8d0bd1686096e12f6ba7365f149067b3f580 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 18 Oct 2018 15:09:18 -0700 Subject: [PATCH] get_apns_payload: Require a UserProfile object for the recipient. This is preparatory work for being able to display an unread count badge on iOS, in which case we need to know who the current user is. --- zerver/lib/push_notifications.py | 4 ++-- zerver/tests/test_push_notifications.py | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/zerver/lib/push_notifications.py b/zerver/lib/push_notifications.py index 7b071d43a8..c5ea4ce269 100644 --- a/zerver/lib/push_notifications.py +++ b/zerver/lib/push_notifications.py @@ -522,7 +522,7 @@ def get_common_payload(message: Message) -> Dict[str, Any]: return data -def get_apns_payload(message: Message) -> Dict[str, Any]: +def get_apns_payload(user_profile: UserProfile, message: Message) -> Dict[str, Any]: zulip_data = get_common_payload(message) zulip_data.update({ 'message_ids': [message.id], @@ -640,7 +640,7 @@ def handle_push_notification(user_profile_id: int, missed_message: Dict[str, Any message.trigger = missed_message['trigger'] message.stream_name = missed_message.get('stream_name', None) - apns_payload = get_apns_payload(message) + apns_payload = get_apns_payload(user_profile, message) gcm_payload = get_gcm_payload(user_profile, message) logging.info("Sending push notification to user %s" % (user_profile_id,)) diff --git a/zerver/tests/test_push_notifications.py b/zerver/tests/test_push_notifications.py index 25c7bbca9f..f1bdfba0ab 100644 --- a/zerver/tests/test_push_notifications.py +++ b/zerver/tests/test_push_notifications.py @@ -769,12 +769,13 @@ class TestGetAlertFromMessage(PushNotificationTest): class TestGetAPNsPayload(PushNotificationTest): def test_get_apns_payload(self) -> None: + user_profile = self.example_user("othello") message_id = self.send_huddle_message( self.sender.email, [self.example_email('othello'), self.example_email('cordelia')]) message = Message.objects.get(id=message_id) message.trigger = 'private_message' - payload = apn.get_apns_payload(message) + payload = apn.get_apns_payload(user_profile, message) expected = { 'alert': { 'title': "New private group message from King Hamlet", @@ -801,11 +802,12 @@ class TestGetAPNsPayload(PushNotificationTest): def test_get_apns_payload_stream(self): # type: () -> None + user_profile = self.example_user("othello") stream = Stream.objects.filter(name='Verona').get() message = self.get_message(Recipient.STREAM, stream.id) message.trigger = 'mentioned' message.stream_name = 'Verona' - payload = apn.get_apns_payload(message) + payload = apn.get_apns_payload(user_profile, message) expected = { 'alert': { 'title': "New mention from King Hamlet", @@ -830,12 +832,13 @@ class TestGetAPNsPayload(PushNotificationTest): @override_settings(PUSH_NOTIFICATION_REDACT_CONTENT = True) def test_get_apns_payload_redacted_content(self) -> None: + user_profile = self.example_user("othello") message_id = self.send_huddle_message( self.sender.email, [self.example_email('othello'), self.example_email('cordelia')]) message = Message.objects.get(id=message_id) message.trigger = 'private_message' - payload = apn.get_apns_payload(message) + payload = apn.get_apns_payload(user_profile, message) expected = { 'alert': { 'title': "New private group message from King Hamlet",