common: Add PatternedPushRule::applies_to

This commit is contained in:
Jonas Platte 2021-04-17 17:26:17 +02:00
parent 045f610e30
commit 5d0fb3924e
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 38 additions and 25 deletions

View File

@ -113,28 +113,17 @@ impl Ruleset {
} }
} }
for rule in self.content.iter().filter(|r| r.enabled) { for rule in self.content.iter().filter(|r| r.enabled) {
let condition = PushCondition::EventMatch { if rule.applies_to("content.body", event_map, context) {
key: "content.body".into(),
pattern: rule.pattern.clone(),
};
if condition.applies(event_map, context) {
return rule.actions.iter(); return rule.actions.iter();
} }
} }
for rule in self.room.iter().filter(|r| r.enabled) { for rule in self.room.iter().filter(|r| r.enabled) {
let condition = if condition::check_event_match(event_map, "room_id", &rule.rule_id, context) {
PushCondition::EventMatch { key: "room_id".into(), pattern: rule.rule_id.clone() };
if condition.applies(event_map, context) {
return rule.actions.iter(); return rule.actions.iter();
} }
} }
for rule in self.sender.iter().filter(|r| r.enabled) { for rule in self.sender.iter().filter(|r| r.enabled) {
let condition = if condition::check_event_match(event_map, "sender", &rule.rule_id, context) {
PushCondition::EventMatch { key: "sender".into(), pattern: rule.rule_id.clone() };
if condition.applies(event_map, context) {
return rule.actions.iter(); return rule.actions.iter();
} }
} }
@ -341,6 +330,23 @@ pub struct PatternedPushRule {
pub pattern: String, pub pattern: String,
} }
impl PatternedPushRule {
/// Check if the push rule applies to the event.
///
/// # Arguments
///
/// * `event` - The flattened JSON representation of a room message event.
/// * `context` - The context of the room at the time of the event.
pub fn applies_to(
&self,
key: &str,
event: &FlattenedJson,
context: &PushConditionRoomCtx,
) -> bool {
condition::check_event_match(event, key, &self.pattern, context)
}
}
/// Initial set of fields of `PatterenedPushRule`. /// Initial set of fields of `PatterenedPushRule`.
/// ///
/// This struct will not be updated even if additional fields are added to `PatterenedPushRule` in a /// This struct will not be updated even if additional fields are added to `PatterenedPushRule` in a

View File

@ -52,6 +52,23 @@ pub enum PushCondition {
}, },
} }
pub(super) fn check_event_match(
event: &FlattenedJson,
key: &str,
pattern: &str,
context: &PushConditionRoomCtx,
) -> bool {
let value = match key {
"room_id" => context.room_id.as_str(),
_ => match event.get(key) {
Some(v) => v,
None => return false,
},
};
value.matches_pattern(pattern, key == "content.body")
}
impl PushCondition { impl PushCondition {
/// Check if this condition applies to the event. /// Check if this condition applies to the event.
/// ///
@ -61,17 +78,7 @@ impl PushCondition {
/// * `context` - The context of the room at the time of the event. /// * `context` - The context of the room at the time of the event.
pub fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool { pub fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool {
match self { match self {
Self::EventMatch { key, pattern } => { Self::EventMatch { key, pattern } => check_event_match(event, &key, &pattern, context),
let value = match key.as_str() {
"room_id" => context.room_id.as_str(),
_ => match event.get(key) {
Some(v) => v,
None => return false,
},
};
value.matches_pattern(pattern, key == "content.body")
}
Self::ContainsDisplayName => { Self::ContainsDisplayName => {
let value = match event.get("content.body") { let value = match event.get("content.body") {
Some(v) => v, Some(v) => v,