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

Commit

Permalink
Add an experimental configuration flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed May 16, 2022
1 parent faf6afb commit 43455d5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:

# MSC3786 (Add a default push rule to ignore m.room.server_acl events)
self.msc3786_enabled: bool = experimental.get("msc3786_enabled", False)

# MSC3772: A push rule for mutual relations.
self.msc3772_enabled: bool = experimental.get("msc3772_enabled", False)
10 changes: 9 additions & 1 deletion synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def __init__(self, hs: "HomeServer"):
resizable=False,
)

# Whether to support MSC3772 is supported.
self._relations_match_enabled = self.hs.config.experimental.msc3772_enabled

async def _get_rules_for_event(
self, event: EventBase, context: EventContext
) -> Dict[str, List[Dict[str, Any]]]:
Expand Down Expand Up @@ -214,7 +217,12 @@ async def action_for_event_by_user(
relations = await self.store.get_mutual_event_relations(event)

evaluator = PushRuleEvaluatorForEvent(
event, len(room_members), sender_power_level, power_levels, relations
event,
len(room_members),
sender_power_level,
power_levels,
relations,
self._relations_match_enabled,
)

# If the event is not a state event check if any users ignore the sender.
Expand Down
7 changes: 6 additions & 1 deletion synapse/push/push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ def __init__(
sender_power_level: int,
power_levels: Dict[str, Union[int, Dict[str, int]]],
relations: Set[Tuple[str, str, str]],
relations_match_enabled: bool,
):
self._event = event
self._room_member_count = room_member_count
self._sender_power_level = sender_power_level
self._power_levels = power_levels
self._relations = relations
self._relations_match_enabled = relations_match_enabled

# Maps strings of e.g. 'content.body' -> event["content"]["body"]
self._value_cache = _flatten_dict(event)
Expand Down Expand Up @@ -190,7 +192,10 @@ def matches(
return _sender_notification_permission(
self._event, condition, self._sender_power_level, self._power_levels
)
elif condition["kind"] == "org.matrix.msc3772.relation_match":
elif (
condition["kind"] == "org.matrix.msc3772.relation_match"
and self._relations_match_enabled
):
return self._relation_match(condition, user_id)
else:
return True
Expand Down
5 changes: 5 additions & 0 deletions synapse/storage/databases/main/push_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def _is_experimental_rule_enabled(
and not experimental_config.msc3786_enabled
):
return False
if (
rule_id == "global/underride/.org.matrix.msc3772.thread_reply"
and not experimental_config.msc3772_enabled
):
return False
return True


Expand Down
16 changes: 15 additions & 1 deletion tests/push/test_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@

class PushRuleEvaluatorTestCase(unittest.TestCase):
def _get_evaluator(
self, content: JsonDict, relations: Optional[Set[Tuple[str, str, str]]] = None
self,
content: JsonDict,
relations: Optional[Set[Tuple[str, str, str]]] = None,
relations_match_enabled: bool = False,
) -> PushRuleEvaluatorForEvent:
event = FrozenEvent(
{
Expand All @@ -49,6 +52,7 @@ def _get_evaluator(
sender_power_level,
power_levels,
relations or set(),
relations_match_enabled,
)

def test_display_name(self) -> None:
Expand Down Expand Up @@ -285,9 +289,19 @@ def test_tweaks_for_actions(self) -> None:

def test_relation_match(self) -> None:
"""Test the relation_match push rule kind."""

# Check if the experimental feature is disabled.
evaluator = self._get_evaluator(
{}, {("m.annotation", "@user:test", "m.reaction")}
)
condition = {"kind": "relation_match"}
# Oddly, an unknown condition always matches.
self.assertTrue(evaluator.matches(condition, "@user:test", "foo"))

# A push rule evaluator with the experimental rule enabled.
evaluator = self._get_evaluator(
{}, {("m.annotation", "@user:test", "m.reaction")}, True
)

# Check just relation type.
condition = {
Expand Down

0 comments on commit 43455d5

Please sign in to comment.