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

Support the backwards compatibility features in MSC3952. #14958

Merged
merged 9 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions rust/src/push/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct PushRuleEvaluator {
/// The "content.body", if any.
body: String,

/// True if the event has a messages property and MSC3952 support is enabled.
has_mentions: bool,
clokep marked this conversation as resolved.
Show resolved Hide resolved
/// The user mentions that were part of the message.
user_mentions: BTreeSet<String>,
/// True if the message is a room message.
Expand Down Expand Up @@ -105,6 +107,7 @@ impl PushRuleEvaluator {
#[new]
pub fn py_new(
flattened_keys: BTreeMap<String, String>,
has_mentions: bool,
user_mentions: BTreeSet<String>,
room_mention: bool,
room_member_count: u64,
Expand All @@ -123,6 +126,7 @@ impl PushRuleEvaluator {
Ok(PushRuleEvaluator {
flattened_keys,
body,
has_mentions,
user_mentions,
room_mention,
room_member_count,
Expand Down Expand Up @@ -154,6 +158,16 @@ impl PushRuleEvaluator {
continue;
}

// For backwards-compatibility the legacy mention rules are disabled
// only if the event contains the 'm.mentions' property.
clokep marked this conversation as resolved.
Show resolved Hide resolved
if self.has_mentions
&& (push_rule.rule_id == ".m.rule.contains_display_name"
|| push_rule.rule_id == ".m.rule.contains_user_name"
|| push_rule.rule_id == ".m.rule.roomnotif")
{
continue;
}

let rule_id = &push_rule.rule_id().to_string();
let extev_flag = &RoomVersionFeatures::ExtensibleEvents.as_str().to_string();
let supports_extensible_events = self.room_version_feature_flags.contains(extev_flag);
Expand Down
1 change: 1 addition & 0 deletions stubs/synapse/synapse_rust/push.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PushRuleEvaluator:
def __init__(
self,
flattened_keys: Mapping[str, str],
has_mentions: bool,
user_mentions: Set[str],
room_mention: bool,
room_member_count: int,
Expand Down
8 changes: 7 additions & 1 deletion synapse/push/bulk_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def __init__(self, hs: "HomeServer"):
self.should_calculate_push_rules = self.hs.config.push.enable_push

self._related_event_match_enabled = self.hs.config.experimental.msc3664_enabled
self._intentional_mentions_enabled = (
self.hs.config.experimental.msc3952_intentional_mentions
)

self.room_push_rule_cache_metrics = register_cache(
"cache",
Expand Down Expand Up @@ -364,9 +367,11 @@ async def _action_for_event_by_user(

# Pull out any user and room mentions.
mentions = event.content.get(EventContentFields.MSC3952_MENTIONS)
has_mentions = self._intentional_mentions_enabled and isinstance(mentions, dict)
user_mentions: Set[str] = set()
room_mention = False
if isinstance(mentions, dict):
if has_mentions:
assert isinstance(mentions, dict)
clokep marked this conversation as resolved.
Show resolved Hide resolved
# Remove out any non-string items and convert to a set.
user_mentions_raw = mentions.get("user_ids")
if isinstance(user_mentions_raw, list):
Expand All @@ -378,6 +383,7 @@ async def _action_for_event_by_user(

evaluator = PushRuleEvaluator(
_flatten_dict(event, room_version=event.room_version),
has_mentions,
user_mentions,
room_mention,
room_member_count,
Expand Down
16 changes: 9 additions & 7 deletions tests/push/test_push_rule_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _get_evaluator(
self,
content: JsonMapping,
*,
has_mentions: bool = False,
user_mentions: Optional[Set[str]] = None,
room_mention: bool = False,
related_events: Optional[JsonDict] = None,
Expand All @@ -62,6 +63,7 @@ def _get_evaluator(
power_levels: Dict[str, Union[int, Dict[str, int]]] = {}
return PushRuleEvaluator(
_flatten_dict(event),
has_mentions,
user_mentions or set(),
room_mention,
room_member_count,
Expand Down Expand Up @@ -102,19 +104,19 @@ def test_user_mentions(self) -> None:
condition = {"kind": "org.matrix.msc3952.is_user_mention"}

# No mentions shouldn't match.
evaluator = self._get_evaluator({})
evaluator = self._get_evaluator({}, has_mentions=True)
self.assertFalse(evaluator.matches(condition, "@user:test", None))

# An empty set shouldn't match
evaluator = self._get_evaluator({}, user_mentions=set())
evaluator = self._get_evaluator({}, has_mentions=True, user_mentions=set())
self.assertFalse(evaluator.matches(condition, "@user:test", None))

# The Matrix ID appearing anywhere in the mentions list should match
evaluator = self._get_evaluator({}, user_mentions={"@user:test"})
evaluator = self._get_evaluator({}, has_mentions=True, user_mentions={"@user:test"})
self.assertTrue(evaluator.matches(condition, "@user:test", None))

evaluator = self._get_evaluator(
{}, user_mentions={"@another:test", "@user:test"}
{}, has_mentions=True, user_mentions={"@another:test", "@user:test"}
)
self.assertTrue(evaluator.matches(condition, "@user:test", None))
clokep marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -126,16 +128,16 @@ def test_room_mentions(self) -> None:
condition = {"kind": "org.matrix.msc3952.is_room_mention"}

# No room mention shouldn't match.
evaluator = self._get_evaluator({})
evaluator = self._get_evaluator({}, has_mentions=True)
self.assertFalse(evaluator.matches(condition, None, None))

# Room mention should match.
evaluator = self._get_evaluator({}, room_mention=True)
evaluator = self._get_evaluator({}, has_mentions=True, room_mention=True)
self.assertTrue(evaluator.matches(condition, None, None))

# A room mention and user mention is valid.
evaluator = self._get_evaluator(
{}, user_mentions={"@another:test"}, room_mention=True
{}, has_mentions=True, user_mentions={"@another:test"}, room_mention=True
)
self.assertTrue(evaluator.matches(condition, None, None))

Expand Down