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

Commit 7c4a3a5

Browse files
committed
Update intentional mentions (MSC3952) to depend on exact_event_property_contains (MSC3966).
1 parent c369d82 commit 7c4a3a5

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

changelog.d/15051.misc

Lines changed: 1 addition & 0 deletions
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/src/push/base_rules.rs

Lines changed: 7 additions & 2 deletions
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::PushRule;
2524
use crate::push::RelatedEventMatchTypeCondition;
2625
use crate::push::SetTweak;
2726
use crate::push::TweakValue;
2827
use crate::push::{Action, ExactEventMatchCondition, SimpleJsonValue};
2928
use crate::push::{Condition, EventMatchTypeCondition};
3029
use crate::push::{EventMatchCondition, EventMatchPatternType};
30+
use crate::push::{ExactEventMatchTypeCondition, PushRule};
3131

3232
const HIGHLIGHT_ACTION: Action = Action::SetTweak(SetTweak {
3333
set_tweak: Cow::Borrowed("highlight"),
@@ -144,7 +144,12 @@ pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
144144
PushRule {
145145
rule_id: Cow::Borrowed(".org.matrix.msc3952.is_user_mention"),
146146
priority_class: 5,
147-
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::IsUserMention)]),
147+
conditions: Cow::Borrowed(&[Condition::Known(
148+
KnownCondition::ExactEventPropertyContainsType(ExactEventMatchTypeCondition {
149+
key: Cow::Borrowed("content.org.matrix.msc3952.mentions.user_ids"),
150+
value_type: Cow::Borrowed(&EventMatchPatternType::UserId),
151+
}),
152+
)]),
148153
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
149154
default: true,
150155
default_enabled: true,

rust/src/push/evaluator.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,30 @@ impl PushRuleEvaluator {
310310
Some(Cow::Borrowed(pattern)),
311311
)?
312312
}
313-
KnownCondition::ExactEventPropertyContains(exact_event_match) => {
314-
self.match_exact_event_property_contains(exact_event_match)?
313+
KnownCondition::ExactEventPropertyContains(exact_event_match) => self
314+
.match_exact_event_property_contains(
315+
exact_event_match.key.clone(),
316+
exact_event_match.value.clone(),
317+
)?,
318+
KnownCondition::ExactEventPropertyContainsType(exact_event_match) => {
319+
// The `pattern_type` can either be "user_id" or "user_localpart",
320+
// either way if we don't have a `user_id` then the condition can't
321+
// match.
322+
let user_id = if let Some(user_id) = user_id {
323+
user_id
324+
} else {
325+
return Ok(false);
326+
};
327+
328+
let pattern = match &*exact_event_match.value_type {
329+
EventMatchPatternType::UserId => user_id,
330+
EventMatchPatternType::UserLocalpart => get_localpart_from_id(user_id)?,
331+
};
332+
333+
self.match_exact_event_property_contains(
334+
exact_event_match.key.clone(),
335+
Cow::Borrowed(&SimpleJsonValue::Str(pattern.to_string())),
336+
)?
315337
}
316338
KnownCondition::IsUserMention => {
317339
if let Some(uid) = user_id {
@@ -456,24 +478,21 @@ impl PushRuleEvaluator {
456478
/// Evaluates a `exact_event_property_contains` condition. (MSC3758)
457479
fn match_exact_event_property_contains(
458480
&self,
459-
exact_event_match: &ExactEventMatchCondition,
481+
key: Cow<str>,
482+
value: Cow<SimpleJsonValue>,
460483
) -> Result<bool, Error> {
461484
// First check if the feature is enabled.
462485
if !self.msc3966_exact_event_property_contains {
463486
return Ok(false);
464487
}
465488

466-
let value = &exact_event_match.value;
467-
468-
let haystack = if let Some(JsonValue::Array(haystack)) =
469-
self.flattened_keys.get(&*exact_event_match.key)
470-
{
489+
let haystack = if let Some(JsonValue::Array(haystack)) = self.flattened_keys.get(&*key) {
471490
haystack
472491
} else {
473492
return Ok(false);
474493
};
475494

476-
Ok(haystack.contains(&**value))
495+
Ok(haystack.contains(&value))
477496
}
478497

479498
/// Match the member count against an 'is' condition

rust/src/push/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ pub enum KnownCondition {
340340
RelatedEventMatchType(RelatedEventMatchTypeCondition),
341341
#[serde(rename = "org.matrix.msc3966.exact_event_property_contains")]
342342
ExactEventPropertyContains(ExactEventMatchCondition),
343+
// Identical to exact_event_property_contains but gives predefined patterns. Cannot be added by users.
344+
#[serde(
345+
skip_deserializing,
346+
rename = "org.matrix.msc3966.exact_event_property_contains"
347+
)]
348+
ExactEventPropertyContainsType(ExactEventMatchTypeCondition),
343349
#[serde(rename = "org.matrix.msc3952.is_user_mention")]
344350
IsUserMention,
345351
ContainsDisplayName,
@@ -398,6 +404,15 @@ pub struct ExactEventMatchCondition {
398404
pub value: Cow<'static, SimpleJsonValue>,
399405
}
400406

407+
/// The body of a [`Condition::ExactEventMatch`] that uses user_id or user_localpart as a pattern.
408+
#[derive(Serialize, Debug, Clone)]
409+
pub struct ExactEventMatchTypeCondition {
410+
pub key: Cow<'static, str>,
411+
// During serialization, the pattern_type property gets replaced with a
412+
// pattern property of the correct value in synapse.push.clientformat.format_push_rules_for_user.
413+
pub value_type: Cow<'static, EventMatchPatternType>,
414+
}
415+
401416
/// The body of a [`Condition::RelatedEventMatch`]
402417
#[derive(Serialize, Deserialize, Debug, Clone)]
403418
pub struct RelatedEventMatchCondition {

synapse/config/experimental.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,19 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
179179
"msc3873_escape_event_match_key", False
180180
)
181181

182-
# MSC3952: Intentional mentions, this depends on MSC3758.
182+
# MSC3966: exact_event_property_contains push rule condition.
183+
self.msc3966_exact_event_property_contains = experimental.get(
184+
"msc3966_exact_event_property_contains", False
185+
)
186+
187+
# MSC3952: Intentional mentions, this depends on MSC3758 and MSC3966.
183188
self.msc3952_intentional_mentions = (
184189
experimental.get("msc3952_intentional_mentions", False)
185190
and self.msc3758_exact_event_match
191+
and self.msc3966_exact_event_property_contains
186192
)
187193

188194
# MSC3959: Do not generate notifications for edits.
189195
self.msc3958_supress_edit_notifs = experimental.get(
190196
"msc3958_supress_edit_notifs", False
191197
)
192-
193-
# MSC3966: exact_event_property_contains push rule condition.
194-
self.msc3966_exact_event_property_contains = experimental.get(
195-
"msc3966_exact_event_property_contains", False
196-
)

synapse/push/clientformat.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ def format_push_rules_for_user(
4141

4242
rulearray.append(template_rule)
4343

44-
pattern_type = template_rule.pop("pattern_type", None)
45-
if pattern_type == "user_id":
46-
template_rule["pattern"] = user.to_string()
47-
elif pattern_type == "user_localpart":
48-
template_rule["pattern"] = user.localpart
44+
for type_key in ("pattern", "value"):
45+
type_value = template_rule.pop(f"{type_key}_type", None)
46+
if type_value == "user_id":
47+
template_rule[type_key] = user.to_string()
48+
elif type_value == "user_localpart":
49+
template_rule[type_key] = user.localpart
4950

5051
template_rule["enabled"] = enabled
5152

tests/push/test_bulk_push_rule_evaluator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ def _create_and_process(
233233
"experimental_features": {
234234
"msc3758_exact_event_match": True,
235235
"msc3952_intentional_mentions": True,
236+
"msc3966_exact_event_property_contains": True,
236237
}
237238
}
238239
)
@@ -336,6 +337,7 @@ def test_user_mentions(self) -> None:
336337
"experimental_features": {
337338
"msc3758_exact_event_match": True,
338339
"msc3952_intentional_mentions": True,
340+
"msc3966_exact_event_property_contains": True,
339341
}
340342
}
341343
)

0 commit comments

Comments
 (0)