Skip to content

Commit

Permalink
Support format=event_id_only
Browse files Browse the repository at this point in the history
  • Loading branch information
dbkr committed Sep 19, 2017
1 parent c7992fd commit 058de39
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
20 changes: 13 additions & 7 deletions sygnal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
73 changes: 50 additions & 23 deletions sygnal/apnspushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sygnal/gcmpushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 058de39

Please sign in to comment.