Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default_payload in Push Payloads #127

Merged
merged 24 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/127.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add fallback_content check to include alert field in the APNS payload.
11 changes: 9 additions & 2 deletions sygnal/apnspushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ async def dispatch_notification(self, n, device, context):
) as span_parent:

if n.event_id and not n.type:
payload = self._get_payload_event_id_only(n)
payload = self._get_payload_event_id_only(n, device)
else:
payload = self._get_payload_full(n, log)

Expand Down Expand Up @@ -278,7 +278,7 @@ async def dispatch_notification(self, n, device, context):
retry_delay, twisted_reactor=self.sygnal.reactor
)

def _get_payload_event_id_only(self, n):
def _get_payload_event_id_only(self, n, device):
richvdh marked this conversation as resolved.
Show resolved Hide resolved
"""
Constructs a payload for a notification where we know only the event ID.
Args:
Expand All @@ -299,6 +299,13 @@ def _get_payload_event_id_only(self, n):
if n.counts.missed_calls is not None:
payload["missed_calls"] = n.counts.missed_calls

payload["aps"] = {"mutable-content": 1}
richvdh marked this conversation as resolved.
Show resolved Hide resolved
if (
device.data["fallback_content"] is not None
and device.data["fallback_content"]
):
payload["aps"]["alert"] = {"loc-key": "SINGLE_UNREAD", "loc-args": []}

return payload

def _get_payload_full(self, n, log):
Expand Down
41 changes: 41 additions & 0 deletions tests/test_apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
TEST_CERTFILE_PATH = "/path/to/my/certfile.pem"

DEVICE_EXAMPLE = {"app_id": "com.example.apns", "pushkey": "spqr", "pushkey_ts": 42}
DEVICE_EXAMPLE_FALLBACK = {
"app_id": "com.example.apns",
"pushkey": "spqr",
"pushkey_ts": 42,
"data": {"fallback_content": True},
}


class ApnsTestCase(testutils.TestCase):
Expand Down Expand Up @@ -132,6 +138,41 @@ def test_expected(self):

self.assertEqual({"rejected": []}, resp)

def test_expected_fallback(self):
"""
Tests the expected fallback case: a good response from APNS means we pass on
a good response to the homeserver.
"""
# Arrange
method = self.apns_pushkin_snotif
method.side_effect = testutils.make_async_magic_mock(
NotificationResult("notID", "200")
)

# Act
resp = self._request(
self._make_dummy_notification_event_id_only([DEVICE_EXAMPLE_FALLBACK])
)

# Assert
self.assertEqual(1, method.call_count)
((notification_req,), _kwargs) = method.call_args

self.assertEqual(
{
"room_id": "!slw48wfj34rtnrf:example.com",
"event_id": "$qTOWWTEL48yPm3uT-gdNhFcoHxfKbZuqRVnnWWSkGBs",
"unread_count": 2,
"aps": {
"alert": {"loc-key": "SINGLE_UNREAD", "loc-args": []},
"mutable-content": 1,
},
},
notification_req.message,
)

self.assertEqual({"rejected": []}, resp)

def test_rejection(self):
"""
Tests the rejection case: a rejection response from APNS leads to us
Expand Down
10 changes: 10 additions & 0 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ def _make_dummy_notification(self, devices):
}
}

def _make_dummy_notification_event_id_only(self, devices):
return {
"notification": {
"room_id": "!slw48wfj34rtnrf:example.com",
"event_id": "$qTOWWTEL48yPm3uT-gdNhFcoHxfKbZuqRVnnWWSkGBs",
"counts": {"unread": 2},
"devices": devices,
}
}

def _request(self, payload) -> Union[dict, int]:
"""
Make a dummy request to the notify endpoint with the specified payload
Expand Down