From 5d0fb3924e0e559d22a6013f82d00eb832b76487 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 17 Apr 2021 17:26:17 +0200 Subject: [PATCH] common: Add PatternedPushRule::applies_to --- ruma-common/src/push.rs | 34 ++++++++++++++++++------------- ruma-common/src/push/condition.rs | 29 ++++++++++++++++---------- 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index c32e9228..9dc8c24b 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -113,28 +113,17 @@ impl Ruleset { } } for rule in self.content.iter().filter(|r| r.enabled) { - let condition = PushCondition::EventMatch { - key: "content.body".into(), - pattern: rule.pattern.clone(), - }; - - if condition.applies(event_map, context) { + if rule.applies_to("content.body", event_map, context) { return rule.actions.iter(); } } for rule in self.room.iter().filter(|r| r.enabled) { - let condition = - PushCondition::EventMatch { key: "room_id".into(), pattern: rule.rule_id.clone() }; - - if condition.applies(event_map, context) { + if condition::check_event_match(event_map, "room_id", &rule.rule_id, context) { return rule.actions.iter(); } } for rule in self.sender.iter().filter(|r| r.enabled) { - let condition = - PushCondition::EventMatch { key: "sender".into(), pattern: rule.rule_id.clone() }; - - if condition.applies(event_map, context) { + if condition::check_event_match(event_map, "sender", &rule.rule_id, context) { return rule.actions.iter(); } } @@ -341,6 +330,23 @@ pub struct PatternedPushRule { 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`. /// /// This struct will not be updated even if additional fields are added to `PatterenedPushRule` in a diff --git a/ruma-common/src/push/condition.rs b/ruma-common/src/push/condition.rs index 80774b16..22ae60cf 100644 --- a/ruma-common/src/push/condition.rs +++ b/ruma-common/src/push/condition.rs @@ -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 { /// 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. pub fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool { match self { - Self::EventMatch { key, pattern } => { - 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::EventMatch { key, pattern } => check_event_match(event, &key, &pattern, context), Self::ContainsDisplayName => { let value = match event.get("content.body") { Some(v) => v,