Add `push.enabled` option to disable push notification calculation (#14551)

* Add initial option

* changelog

* Some more linting
pull/14558/head
Will Hunt 2022-12-01 13:46:24 +00:00 committed by GitHub
parent 781b14ec69
commit 71f3e53ad0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 2 deletions

View File

@ -0,0 +1 @@
Add new `push.enabled` config option to allow opting out of push notification calculation.

View File

@ -3355,6 +3355,10 @@ Configuration settings related to push notifications
This setting defines options for push notifications. This setting defines options for push notifications.
This option has a number of sub-options. They are as follows: 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 * `include_content`: Clients requesting push notifications can either have the body of
the message sent in the notification poke along with other details 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`). 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:
Example configuration: Example configuration:
```yaml ```yaml
push: push:
enable_push: true
include_content: false include_content: false
group_unread_count_by_room: false group_unread_count_by_room: false
``` ```

View File

@ -26,6 +26,7 @@ class PushConfig(Config):
def read_config(self, config: JsonDict, **kwargs: Any) -> None: def read_config(self, config: JsonDict, **kwargs: Any) -> None:
push_config = config.get("push") or {} push_config = config.get("push") or {}
self.push_include_content = push_config.get("include_content", True) 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( self.push_group_unread_count_by_room = push_config.get(
"group_unread_count_by_room", True "group_unread_count_by_room", True
) )

View File

@ -106,6 +106,7 @@ class BulkPushRuleEvaluator:
self.store = hs.get_datastores().main self.store = hs.get_datastores().main
self.clock = hs.get_clock() self.clock = hs.get_clock()
self._event_auth_handler = hs.get_event_auth_handler() 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 self._related_event_match_enabled = self.hs.config.experimental.msc3664_enabled
@ -269,6 +270,8 @@ class BulkPushRuleEvaluator:
for each event, check if the message should increment the unread count, and for each event, check if the message should increment the unread count, and
insert the results into the event_push_actions_staging table. 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, # 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 # so we pass in the batched events. Thus if the event cannot be found in the
# database we can check in the batch. # database we can check in the batch.

View File

@ -6,10 +6,11 @@ from synapse.rest import admin
from synapse.rest.client import login, register, room from synapse.rest.client import login, register, room
from synapse.types import create_requester 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 = [ servlets = [
admin.register_servlets_for_client_rest_resource, admin.register_servlets_for_client_rest_resource,
@ -72,3 +73,43 @@ class TestBulkPushRuleEvaluator(unittest.HomeserverTestCase):
bulk_evaluator = BulkPushRuleEvaluator(self.hs) bulk_evaluator = BulkPushRuleEvaluator(self.hs)
# should not raise # should not raise
self.get_success(bulk_evaluator.action_for_events_by_user([(event, context)])) 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()