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

Commit a23e2b1

Browse files
Half-ShotH-Shay
authored andcommitted
Add push.enabled option to disable push notification calculation (#14551)
* Add initial option * changelog * Some more linting
1 parent 88c7405 commit a23e2b1

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

changelog.d/14551.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add new `push.enabled` config option to allow opting out of push notification calculation.

docs/usage/configuration/config_documentation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,10 @@ Configuration settings related to push notifications
33553355
This setting defines options for push notifications.
33563356

33573357
This option has a number of sub-options. They are as follows:
3358+
* `enable_push`: Enables or disables push notification calculation. Note, disabling this will also
3359+
stop unread counts being calculated for rooms. This mode of operation is intended
3360+
for homeservers which may only have bots or appservice users connected, or are otherwise
3361+
not interested in push/unread counters. This is enabled by default.
33583362
* `include_content`: Clients requesting push notifications can either have the body of
33593363
the message sent in the notification poke along with other details
33603364
like the sender, or just the event ID and room ID (`event_id_only`).
@@ -3375,6 +3379,7 @@ This option has a number of sub-options. They are as follows:
33753379
Example configuration:
33763380
```yaml
33773381
push:
3382+
enable_push: true
33783383
include_content: false
33793384
group_unread_count_by_room: false
33803385
```

synapse/config/push.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class PushConfig(Config):
2626
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
2727
push_config = config.get("push") or {}
2828
self.push_include_content = push_config.get("include_content", True)
29+
self.enable_push = push_config.get("enabled", True)
2930
self.push_group_unread_count_by_room = push_config.get(
3031
"group_unread_count_by_room", True
3132
)

synapse/push/bulk_push_rule_evaluator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(self, hs: "HomeServer"):
106106
self.store = hs.get_datastores().main
107107
self.clock = hs.get_clock()
108108
self._event_auth_handler = hs.get_event_auth_handler()
109+
self.should_calculate_push_rules = self.hs.config.push.enable_push
109110

110111
self._related_event_match_enabled = self.hs.config.experimental.msc3664_enabled
111112

@@ -269,6 +270,8 @@ async def action_for_events_by_user(
269270
for each event, check if the message should increment the unread count, and
270271
insert the results into the event_push_actions_staging table.
271272
"""
273+
if not self.should_calculate_push_rules:
274+
return
272275
# For batched events the power level events may not have been persisted yet,
273276
# so we pass in the batched events. Thus if the event cannot be found in the
274277
# database we can check in the batch.

tests/push/test_bulk_push_rule_evaluator.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
from synapse.rest.client import login, register, room
77
from synapse.types import create_requester
88

9-
from tests import unittest
9+
from tests.test_utils import simple_async_mock
10+
from tests.unittest import HomeserverTestCase, override_config
1011

1112

12-
class TestBulkPushRuleEvaluator(unittest.HomeserverTestCase):
13+
class TestBulkPushRuleEvaluator(HomeserverTestCase):
1314

1415
servlets = [
1516
admin.register_servlets_for_client_rest_resource,
@@ -72,3 +73,43 @@ def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
7273
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
7374
# should not raise
7475
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
76+
77+
@override_config({"push": {"enabled": False}})
78+
def test_action_for_event_by_user_disabled_by_config(self) -> None:
79+
"""Ensure that push rules are not calculated when disabled in the config"""
80+
# Create a new user and room.
81+
alice = self.register_user("alice", "pass")
82+
token = self.login(alice, "pass")
83+
84+
room_id = self.helper.create_room_as(
85+
alice, room_version=RoomVersions.V9.identifier, tok=token
86+
)
87+
88+
# Alter the power levels in that room to include stringy and floaty levels.
89+
# We need to suppress the validation logic or else it will reject these dodgy
90+
# values. (Presumably this validation was not always present.)
91+
event_creation_handler = self.hs.get_event_creation_handler()
92+
requester = create_requester(alice)
93+
94+
# Create a new message event, and try to evaluate it under the dodgy
95+
# power level event.
96+
event, context = self.get_success(
97+
event_creation_handler.create_event(
98+
requester,
99+
{
100+
"type": "m.room.message",
101+
"room_id": room_id,
102+
"content": {
103+
"msgtype": "m.text",
104+
"body": "helo",
105+
},
106+
"sender": alice,
107+
},
108+
)
109+
)
110+
111+
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
112+
bulk_evaluator._action_for_event_by_user = simple_async_mock() # type: ignore[assignment]
113+
# should not raise
114+
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
115+
bulk_evaluator._action_for_event_by_user.assert_not_called()

0 commit comments

Comments
 (0)