Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Support enabling/disabling pushers (from MSC3881) #13799

Merged
merged 18 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
2 changes: 1 addition & 1 deletion synapse/rest/client/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:

for pusher in filtered_pushers:
if self._msc3881_enabled:
pusher["org.matrix.msc3881.enabled"] = bool(pusher["enabled"])
pusher["org.matrix.msc3881.enabled"] = pusher["enabled"]
del pusher["enabled"]

return 200, {"pushers": filtered_pushers}
Expand Down
13 changes: 13 additions & 0 deletions synapse/storage/databases/main/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@ def _decode_pushers_rows(self, rows: Iterable[dict]) -> Iterator[PusherConfig]:
)
continue

# Pushers created while support for the 'enabled' field is not active
# (either because they were created before said support existed or because
# they were created while the experimental implementation is turned off)
# will have the 'enabled' column set to NULL, which needs to be interpreted
# as True.
if r["enabled"] is None:
r["enabled"] = True
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved

# If we're using SQLite, then boolean values are integers. This is
# troublesome since some code using the return value of this method might
# expect it to be a boolean, or will expose it to clients (in responses).
r["enabled"] = bool(r["enabled"])

yield PusherConfig(**r)

async def get_pushers_by_app_id_and_pushkey(
Expand Down
22 changes: 21 additions & 1 deletion tests/push/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,10 @@ def test_disable(self) -> None:
channel = self.make_request("GET", "/pushers", access_token=access_token)
self.assertEqual(channel.code, 200)
self.assertEqual(len(channel.json_body["pushers"]), 1)
self.assertFalse(channel.json_body["pushers"][0]["org.matrix.msc3881.enabled"])

enabled = channel.json_body["pushers"][0]["org.matrix.msc3881.enabled"]
self.assertFalse(enabled)
self.assertTrue(isinstance(enabled, bool))

@override_config({"experimental_features": {"msc3881_enabled": True}})
def test_enable(self) -> None:
Expand All @@ -860,6 +863,23 @@ def test_enable(self) -> None:
self.assertEqual(len(self.push_attempts), 1)

# Get the pushers for the user and check that it is marked as enabled.
channel = self.make_request("GET", "/pushers", access_token=access_token)
self.assertEqual(channel.code, 200)
self.assertEqual(len(channel.json_body["pushers"]), 1)

enabled = channel.json_body["pushers"][0]["org.matrix.msc3881.enabled"]
self.assertTrue(enabled)
self.assertTrue(isinstance(enabled, bool))

@override_config({"experimental_features": {"msc3881_enabled": True}})
def test_null_enabled(self) -> None:
"""Tests that a pusher that has an 'enabled' column set to NULL (eg pushers
created before the column was introduced) is considered enabled.
"""
# We intentionally set 'enabled' to None so that it's stored as NULL in the
# database.
user_id, access_token = self._make_user_with_pusher("user", enabled=None) # type: ignore[arg-type]

channel = self.make_request("GET", "/pushers", access_token=access_token)
self.assertEqual(channel.code, 200)
self.assertEqual(len(channel.json_body["pushers"]), 1)
Expand Down