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

Commit 979f237

Browse files
authored
Update intentional mentions (MSC3952) to depend on exact_event_match (MSC3758). (#15037)
This replaces the specific `is_room_mention` push rule condition used in MSC3952 with the generic `exact_event_match` push rule condition from MSC3758. No functionality changes due to this.
1 parent d1efc47 commit 979f237

File tree

10 files changed

+26
-59
lines changed

10 files changed

+26
-59
lines changed

changelog.d/15037.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update [MSC3952](https://github.com/matrix-org/matrix-spec-proposals/pull/3952) support based on changes to the MSC.

rust/benches/evaluator.rs

-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ fn bench_match_exact(b: &mut Bencher) {
4545
flattened_keys,
4646
false,
4747
BTreeSet::new(),
48-
false,
4948
10,
5049
Some(0),
5150
Default::default(),
@@ -95,7 +94,6 @@ fn bench_match_word(b: &mut Bencher) {
9594
flattened_keys,
9695
false,
9796
BTreeSet::new(),
98-
false,
9997
10,
10098
Some(0),
10199
Default::default(),
@@ -145,7 +143,6 @@ fn bench_match_word_miss(b: &mut Bencher) {
145143
flattened_keys,
146144
false,
147145
BTreeSet::new(),
148-
false,
149146
10,
150147
Some(0),
151148
Default::default(),
@@ -195,7 +192,6 @@ fn bench_eval_message(b: &mut Bencher) {
195192
flattened_keys,
196193
false,
197194
BTreeSet::new(),
198-
false,
199195
10,
200196
Some(0),
201197
Default::default(),

rust/src/push/base_rules.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ use lazy_static::lazy_static;
2121
use serde_json::Value;
2222

2323
use super::KnownCondition;
24-
use crate::push::Action;
2524
use crate::push::Condition;
2625
use crate::push::EventMatchCondition;
2726
use crate::push::PushRule;
2827
use crate::push::RelatedEventMatchCondition;
2928
use crate::push::SetTweak;
3029
use crate::push::TweakValue;
30+
use crate::push::{Action, ExactEventMatchCondition, SimpleJsonValue};
3131

3232
const HIGHLIGHT_ACTION: Action = Action::SetTweak(SetTweak {
3333
set_tweak: Cow::Borrowed("highlight"),
@@ -168,7 +168,10 @@ pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
168168
rule_id: Cow::Borrowed(".org.matrix.msc3952.is_room_mention"),
169169
priority_class: 5,
170170
conditions: Cow::Borrowed(&[
171-
Condition::Known(KnownCondition::IsRoomMention),
171+
Condition::Known(KnownCondition::ExactEventMatch(ExactEventMatchCondition {
172+
key: Cow::Borrowed("content.org.matrix.msc3952.mentions.room"),
173+
value: Cow::Borrowed(&SimpleJsonValue::Bool(true)),
174+
})),
172175
Condition::Known(KnownCondition::SenderNotificationPermission {
173176
key: Cow::Borrowed("room"),
174177
}),

rust/src/push/evaluator.rs

-7
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ pub struct PushRuleEvaluator {
7373
has_mentions: bool,
7474
/// The user mentions that were part of the message.
7575
user_mentions: BTreeSet<String>,
76-
/// True if the message is a room message.
77-
room_mention: bool,
7876

7977
/// The number of users in the room.
8078
room_member_count: u64,
@@ -116,7 +114,6 @@ impl PushRuleEvaluator {
116114
flattened_keys: BTreeMap<String, JsonValue>,
117115
has_mentions: bool,
118116
user_mentions: BTreeSet<String>,
119-
room_mention: bool,
120117
room_member_count: u64,
121118
sender_power_level: Option<i64>,
122119
notification_power_levels: BTreeMap<String, i64>,
@@ -137,7 +134,6 @@ impl PushRuleEvaluator {
137134
body,
138135
has_mentions,
139136
user_mentions,
140-
room_mention,
141137
room_member_count,
142138
notification_power_levels,
143139
sender_power_level,
@@ -279,7 +275,6 @@ impl PushRuleEvaluator {
279275
false
280276
}
281277
}
282-
KnownCondition::IsRoomMention => self.room_mention,
283278
KnownCondition::ContainsDisplayName => {
284279
if let Some(dn) = display_name {
285280
if !dn.is_empty() {
@@ -529,7 +524,6 @@ fn push_rule_evaluator() {
529524
flattened_keys,
530525
false,
531526
BTreeSet::new(),
532-
false,
533527
10,
534528
Some(0),
535529
BTreeMap::new(),
@@ -562,7 +556,6 @@ fn test_requires_room_version_supports_condition() {
562556
flattened_keys,
563557
false,
564558
BTreeSet::new(),
565-
false,
566559
10,
567560
Some(0),
568561
BTreeMap::new(),

rust/src/push/mod.rs

-13
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,6 @@ pub enum KnownCondition {
336336
ExactEventPropertyContains(ExactEventMatchCondition),
337337
#[serde(rename = "org.matrix.msc3952.is_user_mention")]
338338
IsUserMention,
339-
#[serde(rename = "org.matrix.msc3952.is_room_mention")]
340-
IsRoomMention,
341339
ContainsDisplayName,
342340
RoomMemberCount {
343341
#[serde(skip_serializing_if = "Option::is_none")]
@@ -667,17 +665,6 @@ fn test_deserialize_unstable_msc3952_user_condition() {
667665
));
668666
}
669667

670-
#[test]
671-
fn test_deserialize_unstable_msc3952_room_condition() {
672-
let json = r#"{"kind":"org.matrix.msc3952.is_room_mention"}"#;
673-
674-
let condition: Condition = serde_json::from_str(json).unwrap();
675-
assert!(matches!(
676-
condition,
677-
Condition::Known(KnownCondition::IsRoomMention)
678-
));
679-
}
680-
681668
#[test]
682669
fn test_deserialize_custom_condition() {
683670
let json = r#"{"kind":"custom_tag"}"#;

stubs/synapse/synapse_rust/push.pyi

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class PushRuleEvaluator:
5959
flattened_keys: Mapping[str, JsonValue],
6060
has_mentions: bool,
6161
user_mentions: Set[str],
62-
room_mention: bool,
6362
room_member_count: int,
6463
sender_power_level: Optional[int],
6564
notification_power_levels: Mapping[str, int],

synapse/config/experimental.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
179179
"msc3783_escape_event_match_key", False
180180
)
181181

182-
# MSC3952: Intentional mentions
183-
self.msc3952_intentional_mentions = experimental.get(
184-
"msc3952_intentional_mentions", False
182+
# MSC3952: Intentional mentions, this depends on MSC3758.
183+
self.msc3952_intentional_mentions = (
184+
experimental.get("msc3952_intentional_mentions", False)
185+
and self.msc3758_exact_event_match
185186
)
186187

187188
# MSC3959: Do not generate notifications for edits.

synapse/push/bulk_push_rule_evaluator.py

-4
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ async def _action_for_event_by_user(
400400
mentions = event.content.get(EventContentFields.MSC3952_MENTIONS)
401401
has_mentions = self._intentional_mentions_enabled and isinstance(mentions, dict)
402402
user_mentions: Set[str] = set()
403-
room_mention = False
404403
if has_mentions:
405404
# mypy seems to have lost the type even though it must be a dict here.
406405
assert isinstance(mentions, dict)
@@ -410,8 +409,6 @@ async def _action_for_event_by_user(
410409
user_mentions = set(
411410
filter(lambda item: isinstance(item, str), user_mentions_raw)
412411
)
413-
# Room mention is only true if the value is exactly true.
414-
room_mention = mentions.get("room") is True
415412

416413
evaluator = PushRuleEvaluator(
417414
_flatten_dict(
@@ -420,7 +417,6 @@ async def _action_for_event_by_user(
420417
),
421418
has_mentions,
422419
user_mentions,
423-
room_mention,
424420
room_member_count,
425421
sender_power_level,
426422
notification_levels,

tests/push/test_bulk_push_rule_evaluator.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,14 @@ def _create_and_process(
227227
)
228228
return len(result) > 0
229229

230-
@override_config({"experimental_features": {"msc3952_intentional_mentions": True}})
230+
@override_config(
231+
{
232+
"experimental_features": {
233+
"msc3758_exact_event_match": True,
234+
"msc3952_intentional_mentions": True,
235+
}
236+
}
237+
)
231238
def test_user_mentions(self) -> None:
232239
"""Test the behavior of an event which includes invalid user mentions."""
233240
bulk_evaluator = BulkPushRuleEvaluator(self.hs)
@@ -323,7 +330,14 @@ def test_user_mentions(self) -> None:
323330
)
324331
)
325332

326-
@override_config({"experimental_features": {"msc3952_intentional_mentions": True}})
333+
@override_config(
334+
{
335+
"experimental_features": {
336+
"msc3758_exact_event_match": True,
337+
"msc3952_intentional_mentions": True,
338+
}
339+
}
340+
)
327341
def test_room_mentions(self) -> None:
328342
"""Test the behavior of an event which includes invalid room mentions."""
329343
bulk_evaluator = BulkPushRuleEvaluator(self.hs)

tests/push/test_push_rule_evaluator.py

-23
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def _get_evaluator(
149149
*,
150150
has_mentions: bool = False,
151151
user_mentions: Optional[Set[str]] = None,
152-
room_mention: bool = False,
153152
related_events: Optional[JsonDict] = None,
154153
) -> PushRuleEvaluator:
155154
event = FrozenEvent(
@@ -170,7 +169,6 @@ def _get_evaluator(
170169
_flatten_dict(event),
171170
has_mentions,
172171
user_mentions or set(),
173-
room_mention,
174172
room_member_count,
175173
sender_power_level,
176174
cast(Dict[str, int], power_levels.get("notifications", {})),
@@ -232,27 +230,6 @@ def test_user_mentions(self) -> None:
232230
# Note that invalid data is tested at tests.push.test_bulk_push_rule_evaluator.TestBulkPushRuleEvaluator.test_mentions
233231
# since the BulkPushRuleEvaluator is what handles data sanitisation.
234232

235-
def test_room_mentions(self) -> None:
236-
"""Check for room mentions."""
237-
condition = {"kind": "org.matrix.msc3952.is_room_mention"}
238-
239-
# No room mention shouldn't match.
240-
evaluator = self._get_evaluator({}, has_mentions=True)
241-
self.assertFalse(evaluator.matches(condition, None, None))
242-
243-
# Room mention should match.
244-
evaluator = self._get_evaluator({}, has_mentions=True, room_mention=True)
245-
self.assertTrue(evaluator.matches(condition, None, None))
246-
247-
# A room mention and user mention is valid.
248-
evaluator = self._get_evaluator(
249-
{}, has_mentions=True, user_mentions={"@another:test"}, room_mention=True
250-
)
251-
self.assertTrue(evaluator.matches(condition, None, None))
252-
253-
# Note that invalid data is tested at tests.push.test_bulk_push_rule_evaluator.TestBulkPushRuleEvaluator.test_mentions
254-
# since the BulkPushRuleEvaluator is what handles data sanitisation.
255-
256233
def _assert_matches(
257234
self, condition: JsonDict, content: JsonMapping, msg: Optional[str] = None
258235
) -> None:

0 commit comments

Comments
 (0)