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

Commit

Permalink
Implement MSC3786: Add a default push rule to ignore m.room.server_ac…
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBrandner committed May 10, 2022
1 parent d80a7ab commit ade3008
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
1 change: 1 addition & 0 deletions changelog.d/12601.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement MSC3786: Add a default push rule to ignore m.room.server_acl events.
3 changes: 3 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:

# MSC2815 (allow room moderators to view redacted event content)
self.msc2815_enabled: bool = experimental.get("msc2815_enabled", False)

# MSC3786 (Add a default push rule to ignore m.room.server_acl events)
self.msc3786_enabled: bool = experimental.get("msc3786_enabled", False)
15 changes: 15 additions & 0 deletions synapse/push/baserules.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ def make_base_prepend_rules(
],
"actions": ["dont_notify"],
},
# XXX: This is an experimental rule that is only enabled if msc3786_enabled
# is enabled, if it is not the rule gets filtered out in _load_rules() in
# PushRulesWorkerStore
{
"rule_id": "global/override/.org.matrix.msc3786.rule.room.server_acl",
"conditions": [
{
"kind": "event_match",
"key": "type",
"pattern": "m.room.server_acl",
"_cache_key": "_room_server_acl",
}
],
"actions": ["dont_notify"],
},
]


Expand Down
48 changes: 37 additions & 11 deletions synapse/storage/databases/main/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from typing import TYPE_CHECKING, Dict, List, Tuple, Union

from synapse.api.errors import StoreError
from synapse.config.homeserver import ExperimentalConfig
from synapse.push.baserules import list_with_base_rules
from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
from synapse.storage._base import SQLBaseStore, db_to_json
Expand All @@ -42,7 +43,21 @@
logger = logging.getLogger(__name__)


def _load_rules(rawrules, enabled_map):
def _is_experimental_rule_enabled(
rule_id: str, experimental_config: ExperimentalConfig
) -> bool:
"""Used by `_load_rules` to filter out experimental rules when they
have not been enabled.
"""
if (
rule_id == "global/override/.org.matrix.msc3786.rule.room.server_acl"
and not experimental_config.msc3786_enabled
):
return False
return True


def _load_rules(rawrules, enabled_map, experimental_config: ExperimentalConfig):
ruleslist = []
for rawrule in rawrules:
rule = dict(rawrule)
Expand All @@ -51,17 +66,26 @@ def _load_rules(rawrules, enabled_map):
rule["default"] = False
ruleslist.append(rule)

# We're going to be mutating this a lot, so do a deep copy
rules = list(list_with_base_rules(ruleslist))
# We're going to be mutating this a lot, so copy it. We also filter out
# any experimental default push rules that aren't enabled.
rules = [
rule
for rule in list_with_base_rules(ruleslist)
if _is_experimental_rule_enabled(rule["rule_id"], experimental_config)
]

for i, rule in enumerate(rules):
rule_id = rule["rule_id"]
if rule_id in enabled_map:
if rule.get("enabled", True) != bool(enabled_map[rule_id]):
# Rules are cached across users.
rule = dict(rule)
rule["enabled"] = bool(enabled_map[rule_id])
rules[i] = rule

if rule_id not in enabled_map:
continue
if rule.get("enabled", True) == bool(enabled_map[rule_id]):
continue

# Rules are cached across users.
rule = dict(rule)
rule["enabled"] = bool(enabled_map[rule_id])
rules[i] = rule

return rules

Expand Down Expand Up @@ -141,7 +165,7 @@ async def get_push_rules_for_user(self, user_id):

enabled_map = await self.get_push_rules_enabled_for_user(user_id)

return _load_rules(rows, enabled_map)
return _load_rules(rows, enabled_map, self.hs.config.experimental)

@cached(max_entries=5000)
async def get_push_rules_enabled_for_user(self, user_id) -> Dict[str, bool]:
Expand Down Expand Up @@ -200,7 +224,9 @@ async def bulk_get_push_rules(self, user_ids):
enabled_map_by_user = await self.bulk_get_push_rules_enabled(user_ids)

for user_id, rules in results.items():
results[user_id] = _load_rules(rules, enabled_map_by_user.get(user_id, {}))
results[user_id] = _load_rules(
rules, enabled_map_by_user.get(user_id, {}), self.hs.config.experimental
)

return results

Expand Down

0 comments on commit ade3008

Please sign in to comment.