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

Commit

Permalink
Update intentional mentions (MSC3952) to depend on `exact_event_prope…
Browse files Browse the repository at this point in the history
…rty_contains` (MSC3966).
  • Loading branch information
clokep committed Feb 10, 2023
1 parent 9786450 commit f3031a2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
11 changes: 9 additions & 2 deletions rust/src/push/base_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,13 @@ pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
PushRule {
rule_id: Cow::Borrowed(".org.matrix.msc3952.is_user_mention"),
priority_class: 5,
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::IsUserMention)]),
conditions: Cow::Borrowed(&[Condition::Known(KnownCondition::ExactEventMatch(
ExactEventMatchCondition {
key: Cow::Borrowed("content.org.matrix.msc3952.mentions.user_ids"),
value: None,
value_type: Some(Cow::Borrowed("user_id")),
},
))]),
actions: Cow::Borrowed(&[Action::Notify, HIGHLIGHT_ACTION, SOUND_ACTION]),
default: true,
default_enabled: true,
Expand All @@ -170,7 +176,8 @@ pub const BASE_APPEND_OVERRIDE_RULES: &[PushRule] = &[
conditions: Cow::Borrowed(&[
Condition::Known(KnownCondition::ExactEventMatch(ExactEventMatchCondition {
key: Cow::Borrowed("content.org.matrix.msc3952.mentions.room"),
value: Cow::Borrowed(&SimpleJsonValue::Bool(true)),
value: Some(Cow::Borrowed(&SimpleJsonValue::Bool(true))),
value_type: None,
})),
Condition::Known(KnownCondition::SenderNotificationPermission {
key: Cow::Borrowed("room"),
Expand Down
31 changes: 27 additions & 4 deletions rust/src/push/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl PushRuleEvaluator {
self.match_related_event_match(event_match, user_id)?
}
KnownCondition::ExactEventPropertyContains(exact_event_match) => {
self.match_exact_event_property_contains(exact_event_match)?
self.match_exact_event_property_contains(exact_event_match, user_id)?
}
KnownCondition::IsUserMention => {
if let Some(uid) = user_id {
Expand Down Expand Up @@ -379,7 +379,12 @@ impl PushRuleEvaluator {
return Ok(false);
}

let value = &exact_event_match.value;
// exact_event_match requires a value to be set.
let value = if let Some(value) = &exact_event_match.value {
value
} else {
return Ok(false);
};

let haystack = if let Some(JsonValue::Value(haystack)) =
self.flattened_keys.get(&*exact_event_match.key)
Expand Down Expand Up @@ -470,13 +475,31 @@ impl PushRuleEvaluator {
fn match_exact_event_property_contains(
&self,
exact_event_match: &ExactEventMatchCondition,
user_id: Option<&str>,
) -> Result<bool, Error> {
// First check if the feature is enabled.
if !self.msc3966_exact_event_property_contains {
return Ok(false);
}

let value = &exact_event_match.value;
let value = if let Some(value) = &exact_event_match.value {
&**value
} else if let Some(value_type) = &exact_event_match.value_type {
// The `value_type` must be "user_id", if we don't have a `user_id`
// then the condition can't match.
let user_id = if let Some(user_id) = user_id {
&SimpleJsonValue::Str(user_id.to_string())
} else {
return Ok(false);
};

match &**value_type {
"user_id" => user_id,
_ => return Ok(false),
}
} else {
return Ok(false);
};

let haystack = if let Some(JsonValue::Array(haystack)) =
self.flattened_keys.get(&*exact_event_match.key)
Expand All @@ -486,7 +509,7 @@ impl PushRuleEvaluator {
return Ok(false);
};

Ok(haystack.contains(&**value))
Ok(haystack.contains(value))
}

/// Match the member count against an 'is' condition
Expand Down
5 changes: 4 additions & 1 deletion rust/src/push/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ pub struct EventMatchCondition {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ExactEventMatchCondition {
pub key: Cow<'static, str>,
pub value: Cow<'static, SimpleJsonValue>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<Cow<'static, SimpleJsonValue>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value_type: Option<Cow<'static, str>>,
}

/// The body of a [`Condition::RelatedEventMatch`]
Expand Down

0 comments on commit f3031a2

Please sign in to comment.