From aa8e00f88640b0e15e6783098c8ef4209bdcab4a Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 17 Apr 2021 17:44:24 +0200 Subject: [PATCH] common: Add more useful methods to AnyPushRule, AnyPushRuleRef --- ruma-common/src/push.rs | 39 +++--------------- ruma-common/src/push/iter.rs | 77 ++++++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 41 deletions(-) diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index 572bfa23..b964b6a9 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -105,39 +105,12 @@ impl Ruleset { where T: Serialize, { - let event_map = &FlattenedJson::from_raw(event); - - for rule in &self.override_ { - if rule.applies(event_map, context) { - return rule.actions.iter(); - } - } - for rule in &self.content { - if rule.applies_to("content.body", event_map, context) { - return rule.actions.iter(); - } - } - for rule in &self.room { - if rule.enabled - && condition::check_event_match(event_map, "room_id", &rule.rule_id, context) - { - return rule.actions.iter(); - } - } - for rule in &self.sender { - if rule.enabled - && condition::check_event_match(event_map, "sender", &rule.rule_id, context) - { - return rule.actions.iter(); - } - } - for rule in &self.underride { - if rule.applies(event_map, context) { - return rule.actions.iter(); - } - } - - [].iter() + let event = FlattenedJson::from_raw(event); + self.iter() + .find(|rule| rule.applies(&event, context)) + .map(|rule| rule.actions()) + .unwrap_or(&[]) + .iter() } } diff --git a/ruma-common/src/push/iter.rs b/ruma-common/src/push/iter.rs index 8386dd6e..7662e5e8 100644 --- a/ruma-common/src/push/iter.rs +++ b/ruma-common/src/push/iter.rs @@ -1,6 +1,9 @@ use indexmap::set::{IntoIter as IndexSetIntoIter, Iter as IndexSetIter}; -use super::{ConditionalPushRule, PatternedPushRule, Ruleset, SimplePushRule}; +use super::{ + condition, Action, ConditionalPushRule, FlattenedJson, PatternedPushRule, PushConditionRoomCtx, + Ruleset, SimplePushRule, +}; /// The kinds of push rules that are available. #[derive(Clone, Debug)] @@ -33,15 +36,29 @@ impl AnyPushRule { } } + /// Get the `enabled` flag of the push rule. + pub fn enabled(&self) -> bool { + self.as_ref().enabled() + } + + /// Get the `actions` of the push rule. + pub fn actions(&self) -> &[Action] { + self.as_ref().actions() + } + /// Get the `rule_id` of the push rule. pub fn rule_id(&self) -> &str { - match self { - Self::Override(rule) => &rule.rule_id, - Self::Underride(rule) => &rule.rule_id, - Self::Content(rule) => &rule.rule_id, - Self::Room(rule) => &rule.rule_id, - Self::Sender(rule) => &rule.rule_id, - } + self.as_ref().rule_id() + } + + /// 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(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool { + self.as_ref().applies(event, context) } } @@ -126,6 +143,28 @@ impl<'a> AnyPushRuleRef<'a> { } } + /// Get the `enabled` flag of the push rule. + pub fn enabled(self) -> bool { + match self { + Self::Override(rule) => rule.enabled, + Self::Underride(rule) => rule.enabled, + Self::Content(rule) => rule.enabled, + Self::Room(rule) => rule.enabled, + Self::Sender(rule) => rule.enabled, + } + } + + /// Get the `actions` of the push rule. + pub fn actions(self) -> &'a [Action] { + match self { + Self::Override(rule) => &rule.actions, + Self::Underride(rule) => &rule.actions, + Self::Content(rule) => &rule.actions, + Self::Room(rule) => &rule.actions, + Self::Sender(rule) => &rule.actions, + } + } + /// Get the `rule_id` of the push rule. pub fn rule_id(self) -> &'a str { match self { @@ -136,6 +175,28 @@ impl<'a> AnyPushRuleRef<'a> { Self::Sender(rule) => &rule.rule_id, } } + + /// 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(self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool { + match self { + Self::Override(rule) => rule.applies(event, context), + Self::Underride(rule) => rule.applies(event, context), + Self::Content(rule) => rule.applies_to("content.body", event, context), + Self::Room(rule) => { + rule.enabled + && condition::check_event_match(event, "room_id", &rule.rule_id, context) + } + Self::Sender(rule) => { + rule.enabled + && condition::check_event_match(event, "sender", &rule.rule_id, context) + } + } + } } /// Iterator type for `Ruleset`