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

Commit

Permalink
Add push.enabled option to disable push notification calculation (#…
Browse files Browse the repository at this point in the history
…14551)

* Add initial option

* changelog

* Some more linting
  • Loading branch information
Half-Shot authored and H-Shay committed Dec 13, 2022
1 parent 88c7405 commit a23e2b1
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/14551.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add new `push.enabled` config option to allow opting out of push notification calculation.
5 changes: 5 additions & 0 deletions docs/usage/configuration/config_documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3355,6 +3355,10 @@ Configuration settings related to push notifications
This setting defines options for push notifications.

This option has a number of sub-options. They are as follows:
* `enable_push`: Enables or disables push notification calculation. Note, disabling this will also
stop unread counts being calculated for rooms. This mode of operation is intended
for homeservers which may only have bots or appservice users connected, or are otherwise
not interested in push/unread counters. This is enabled by default.
* `include_content`: Clients requesting push notifications can either have the body of
the message sent in the notification poke along with other details
like the sender, or just the event ID and room ID (`event_id_only`).
Expand All @@ -3375,6 +3379,7 @@ This option has a number of sub-options. They are as follows:
Example configuration:
```yaml
push:
enable_push: true
include_content: false
group_unread_count_by_room: false
```
Expand Down
1 change: 1 addition & 0 deletions synapse/config/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PushConfig(Config):
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
push_config = config.get("push") or {}
self.push_include_content = push_config.get("include_content", True)
self.enable_push = push_config.get("enabled", True)
self.push_group_unread_count_by_room = push_config.get(
"group_unread_count_by_room", True
)
Expand Down
3 changes: 3 additions & 0 deletions synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastores().main
self.clock = hs.get_clock()
self._event_auth_handler = hs.get_event_auth_handler()
self.should_calculate_push_rules = self.hs.config.push.enable_push

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

Expand Down Expand Up @@ -269,6 +270,8 @@ async def action_for_events_by_user(
for each event, check if the message should increment the unread count, and
insert the results into the event_push_actions_staging table.
"""
if not self.should_calculate_push_rules:
return
# For batched events the power level events may not have been persisted yet,
# so we pass in the batched events. Thus if the event cannot be found in the
# database we can check in the batch.
Expand Down
45 changes: 43 additions & 2 deletions tests/push/test_bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
from synapse.rest.client import login, register, room
from synapse.types import create_requester

from tests import unittest
from tests.test_utils import simple_async_mock
from tests.unittest import HomeserverTestCase, override_config


class TestBulkPushRuleEvaluator(unittest.HomeserverTestCase):
class TestBulkPushRuleEvaluator(HomeserverTestCase):

servlets = [
admin.register_servlets_for_client_rest_resource,
Expand Down Expand Up @@ -72,3 +73,43 @@ def test_action_for_event_by_user_handles_noninteger_power_levels(self) -> None:
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
# should not raise
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))

@override_config({"push": {"enabled": False}})
def test_action_for_event_by_user_disabled_by_config(self) -> None:
"""Ensure that push rules are not calculated when disabled in the config"""
# Create a new user and room.
alice = self.register_user("alice", "pass")
token = self.login(alice, "pass")

room_id = self.helper.create_room_as(
alice, room_version=RoomVersions.V9.identifier, tok=token
)

# Alter the power levels in that room to include stringy and floaty levels.
# We need to suppress the validation logic or else it will reject these dodgy
# values. (Presumably this validation was not always present.)
event_creation_handler = self.hs.get_event_creation_handler()
requester = create_requester(alice)

# Create a new message event, and try to evaluate it under the dodgy
# power level event.
event, context = self.get_success(
event_creation_handler.create_event(
requester,
{
"type": "m.room.message",
"room_id": room_id,
"content": {
"msgtype": "m.text",
"body": "helo",
},
"sender": alice,
},
)
)

bulk_evaluator = BulkPushRuleEvaluator(self.hs)
bulk_evaluator._action_for_event_by_user = simple_async_mock() # type: ignore[assignment]
# should not raise
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)]))
bulk_evaluator._action_for_event_by_user.assert_not_called()

0 comments on commit a23e2b1

Please sign in to comment.