From 058de3900506e52f736b2a2bb876eac8fef51472 Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 19 Sep 2017 12:35:02 +0100 Subject: [PATCH] Support format=event_id_only as per https://github.com/matrix-org/synapse/pull/2450 --- sygnal/__init__.py | 20 +++++++----- sygnal/apnspushkin.py | 73 +++++++++++++++++++++++++++++-------------- sygnal/gcmpushkin.py | 2 +- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/sygnal/__init__.py b/sygnal/__init__.py index b79c621e..6e561e6d 100644 --- a/sygnal/__init__.py +++ b/sygnal/__init__.py @@ -87,13 +87,19 @@ def __init__(self, raw): class Notification: def __init__(self, notif): - attrs = [ 'id', 'type', 'sender' ] - for a in attrs: - if a not in notif: - raise InvalidNotificationException("Expected '%s' key" % (a,)) - self.__dict__[a] = notif[a] - - optional_attrs = ['room_name', 'room_alias', 'prio', 'membership', 'sender_display_name', 'content', 'room_id', 'user_is_target'] + optional_attrs = [ + 'room_name', + 'room_alias', + 'prio', + 'membership', + 'sender_display_name', + 'content', + 'event_id', + 'room_id', + 'user_is_target', + 'type', + 'sender', + ] for a in optional_attrs: if a in notif: self.__dict__[a] = notif[a] diff --git a/sygnal/apnspushkin.py b/sygnal/apnspushkin.py index 7143bbd6..439684ae 100644 --- a/sygnal/apnspushkin.py +++ b/sygnal/apnspushkin.py @@ -112,6 +112,40 @@ def dispatchNotification(self, n): # failure because HSes should probably have a fresh token # if they actually want to use it + payload = None + if n.event_id and not n.type: + payload = self.get_payload_event_id_only(n) + else: + payload = self.get_payload_full(n) + + prio = 10 + if n.prio == 'low': + prio = 5 + + tries = 0 + for t,d in tokens.items(): + while tries < ApnsPushkin.MAX_TRIES: + thispayload = payload + if 'aps' in thispayload: + thispayload = payload.copy() + thispayload['aps'] = thispayload['aps'].copy() + if d.tweaks.sound: + thispayload['aps']['sound'] = d.tweaks.sound + logger.info("'%s' -> %s", thispayload, t) + try: + res = self.pushbaby.send(thispayload, base64.b64decode(t), priority=prio) + break + except: + logger.exception("Exception sending push") + + tries += 1 + + if tries == ApnsPushkin.MAX_TRIES: + raise NotificationDispatchException("Max retries exceeded") + + return rejected + + def get_payload_full(self, n): from_display = n.sender if n.sender_display_name is not None: from_display = n.sender_display_name @@ -213,36 +247,29 @@ def dispatchNotification(self, n): logger.info("Nothing to do for alert of type %s", n.type) return rejected - prio = 10 - if n.prio == 'low': - prio = 5 - payload = {} if loc_key and n.room_id: payload['room_id'] = n.room_id - tries = 0 - for t,d in tokens.items(): - while tries < ApnsPushkin.MAX_TRIES: - thispayload = payload.copy() - thisaps = aps.copy() - if d.tweaks.sound: - thisaps['sound'] = d.tweaks.sound - thispayload['aps'] = thisaps - logger.info("'%s' -> %s", thispayload, t) - try: - res = self.pushbaby.send(thispayload, base64.b64decode(t), priority=prio) - break - except: - logger.exception("Exception sending push") + payload['aps'] = aps - tries += 1 - - if tries == ApnsPushkin.MAX_TRIES: - raise NotificationDispatchException("Max retries exceeded") + return payload - return rejected + def get_payload_event_id_only(self, n): + payload = {} + + if n.room_id: + payload['room_id'] = n.room_id + if n.event_id: + payload['event_id'] = n.event_id + + if n.counts.unread is not None: + payload['unread_count'] = n.counts.unread + if n.counts.missed_calls is not None: + payload['missed_calls'] = n.counts.missed_calls + + return payload def on_push_failed(self, token, identifier, status): logger.error("Error sending push to token %s, status %s", token, status) diff --git a/sygnal/gcmpushkin.py b/sygnal/gcmpushkin.py index 3d860bdd..bb02e956 100644 --- a/sygnal/gcmpushkin.py +++ b/sygnal/gcmpushkin.py @@ -176,7 +176,7 @@ def dispatchNotification(self, n): @staticmethod def build_data(n): data = {} - for attr in ['id', 'type', 'sender', 'room_name', 'room_alias', 'membership', + for attr in ['event_id', 'type', 'sender', 'room_name', 'room_alias', 'membership', 'sender_display_name', 'content', 'room_id']: if hasattr(n, attr): data[attr] = getattr(n, attr)