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.
This commit is contained in:
Tim Abbott 2018-10-18 15:09:18 -07:00
parent a4df001cef
commit 695d8d0bd1
2 changed files with 8 additions and 5 deletions

View File

@ -522,7 +522,7 @@ def get_common_payload(message: Message) -> Dict[str, Any]:
return data 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 = get_common_payload(message)
zulip_data.update({ zulip_data.update({
'message_ids': [message.id], '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.trigger = missed_message['trigger']
message.stream_name = missed_message.get('stream_name', None) 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) gcm_payload = get_gcm_payload(user_profile, message)
logging.info("Sending push notification to user %s" % (user_profile_id,)) logging.info("Sending push notification to user %s" % (user_profile_id,))

View File

@ -769,12 +769,13 @@ class TestGetAlertFromMessage(PushNotificationTest):
class TestGetAPNsPayload(PushNotificationTest): class TestGetAPNsPayload(PushNotificationTest):
def test_get_apns_payload(self) -> None: def test_get_apns_payload(self) -> None:
user_profile = self.example_user("othello")
message_id = self.send_huddle_message( message_id = self.send_huddle_message(
self.sender.email, self.sender.email,
[self.example_email('othello'), self.example_email('cordelia')]) [self.example_email('othello'), self.example_email('cordelia')])
message = Message.objects.get(id=message_id) message = Message.objects.get(id=message_id)
message.trigger = 'private_message' message.trigger = 'private_message'
payload = apn.get_apns_payload(message) payload = apn.get_apns_payload(user_profile, message)
expected = { expected = {
'alert': { 'alert': {
'title': "New private group message from King Hamlet", 'title': "New private group message from King Hamlet",
@ -801,11 +802,12 @@ class TestGetAPNsPayload(PushNotificationTest):
def test_get_apns_payload_stream(self): def test_get_apns_payload_stream(self):
# type: () -> None # type: () -> None
user_profile = self.example_user("othello")
stream = Stream.objects.filter(name='Verona').get() stream = Stream.objects.filter(name='Verona').get()
message = self.get_message(Recipient.STREAM, stream.id) message = self.get_message(Recipient.STREAM, stream.id)
message.trigger = 'mentioned' message.trigger = 'mentioned'
message.stream_name = 'Verona' message.stream_name = 'Verona'
payload = apn.get_apns_payload(message) payload = apn.get_apns_payload(user_profile, message)
expected = { expected = {
'alert': { 'alert': {
'title': "New mention from King Hamlet", 'title': "New mention from King Hamlet",
@ -830,12 +832,13 @@ class TestGetAPNsPayload(PushNotificationTest):
@override_settings(PUSH_NOTIFICATION_REDACT_CONTENT = True) @override_settings(PUSH_NOTIFICATION_REDACT_CONTENT = True)
def test_get_apns_payload_redacted_content(self) -> None: def test_get_apns_payload_redacted_content(self) -> None:
user_profile = self.example_user("othello")
message_id = self.send_huddle_message( message_id = self.send_huddle_message(
self.sender.email, self.sender.email,
[self.example_email('othello'), self.example_email('cordelia')]) [self.example_email('othello'), self.example_email('cordelia')])
message = Message.objects.get(id=message_id) message = Message.objects.get(id=message_id)
message.trigger = 'private_message' message.trigger = 'private_message'
payload = apn.get_apns_payload(message) payload = apn.get_apns_payload(user_profile, message)
expected = { expected = {
'alert': { 'alert': {
'title': "New private group message from King Hamlet", 'title': "New private group message from King Hamlet",