Implement MSC3786: Add a default push rule to ignore m.room.server_acl events (#12601)

Fixes vector-im/element-web#20788
Implements matrix-org/matrix-spec-proposals#3786
This commit is contained in:
Šimon Brandner 2022-05-10 09:57:36 +02:00 committed by GitHub
parent d80a7ab151
commit ade3008821
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 11 deletions

View File

@ -0,0 +1 @@
Implement MSC3786: Add a default push rule to ignore m.room.server_acl events.

View File

@ -81,3 +81,6 @@ class ExperimentalConfig(Config):
# MSC2815 (allow room moderators to view redacted event content) # MSC2815 (allow room moderators to view redacted event content)
self.msc2815_enabled: bool = experimental.get("msc2815_enabled", False) 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)

View File

@ -277,6 +277,21 @@ BASE_APPEND_OVERRIDE_RULES: List[Dict[str, Any]] = [
], ],
"actions": ["dont_notify"], "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"],
},
] ]

View File

@ -17,6 +17,7 @@ import logging
from typing import TYPE_CHECKING, Dict, List, Tuple, Union from typing import TYPE_CHECKING, Dict, List, Tuple, Union
from synapse.api.errors import StoreError from synapse.api.errors import StoreError
from synapse.config.homeserver import ExperimentalConfig
from synapse.push.baserules import list_with_base_rules from synapse.push.baserules import list_with_base_rules
from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker from synapse.replication.slave.storage._slaved_id_tracker import SlavedIdTracker
from synapse.storage._base import SQLBaseStore, db_to_json from synapse.storage._base import SQLBaseStore, db_to_json
@ -42,7 +43,21 @@ if TYPE_CHECKING:
logger = logging.getLogger(__name__) 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 = [] ruleslist = []
for rawrule in rawrules: for rawrule in rawrules:
rule = dict(rawrule) rule = dict(rawrule)
@ -51,17 +66,26 @@ def _load_rules(rawrules, enabled_map):
rule["default"] = False rule["default"] = False
ruleslist.append(rule) ruleslist.append(rule)
# We're going to be mutating this a lot, so do a deep copy # We're going to be mutating this a lot, so copy it. We also filter out
rules = list(list_with_base_rules(ruleslist)) # 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): for i, rule in enumerate(rules):
rule_id = rule["rule_id"] rule_id = rule["rule_id"]
if rule_id in enabled_map:
if rule.get("enabled", True) != bool(enabled_map[rule_id]): if rule_id not in enabled_map:
# Rules are cached across users. continue
rule = dict(rule) if rule.get("enabled", True) == bool(enabled_map[rule_id]):
rule["enabled"] = bool(enabled_map[rule_id]) continue
rules[i] = rule
# Rules are cached across users.
rule = dict(rule)
rule["enabled"] = bool(enabled_map[rule_id])
rules[i] = rule
return rules return rules
@ -141,7 +165,7 @@ class PushRulesWorkerStore(
enabled_map = await self.get_push_rules_enabled_for_user(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) @cached(max_entries=5000)
async def get_push_rules_enabled_for_user(self, user_id) -> Dict[str, bool]: async def get_push_rules_enabled_for_user(self, user_id) -> Dict[str, bool]:
@ -200,7 +224,9 @@ class PushRulesWorkerStore(
enabled_map_by_user = await self.bulk_get_push_rules_enabled(user_ids) enabled_map_by_user = await self.bulk_get_push_rules_enabled(user_ids)
for user_id, rules in results.items(): 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 return results