From 32398d187b163f14d63b811e98730647d21fe6f8 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sat, 17 Apr 2021 17:58:53 +0200 Subject: [PATCH] common: Add Ruleset::get_match --- ruma-common/CHANGELOG.md | 3 +++ ruma-common/src/push.rs | 24 +++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ruma-common/CHANGELOG.md b/ruma-common/CHANGELOG.md index 6c6848cf..5ad0f9ac 100644 --- a/ruma-common/CHANGELOG.md +++ b/ruma-common/CHANGELOG.md @@ -11,6 +11,9 @@ Improvements: * Add `push::Ruleset::iter()` for borrowing iteration of rulesets * Add conversions between `AnyPushRule` and `AnyPushRuleRef` (`AnyPushRule::as_ref` and `AnyPushRuleRef::to_owned`) +* Add `push::Ruleset::get_match()` for finding the first matching push rule for + an event. This is pretty much the same thing as `get_actions()` but returns + the entire push rule, not just its actions. # 0.4.0 diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index e28c36b4..77b90024 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -89,6 +89,24 @@ impl Ruleset { } } + /// Get the first push rule that applies to this event, if any. + /// + /// # Arguments + /// + /// * `event` - The raw JSON of a room message event. + /// * `context` - The context of the message and room at the time of the event. + pub fn get_match( + &self, + event: &Raw, + context: &PushConditionRoomCtx, + ) -> Option> + where + T: Serialize, + { + let event = FlattenedJson::from_raw(event); + self.iter().find(|rule| rule.applies(&event, context)) + } + /// Get the push actions that apply to this event. /// /// Returns an empty slice if no push rule applies. @@ -101,11 +119,7 @@ impl Ruleset { where T: Serialize, { - let event = FlattenedJson::from_raw(event); - self.iter() - .find(|rule| rule.applies(&event, context)) - .map(|rule| rule.actions()) - .unwrap_or(&[]) + self.get_match(event, context).map(|rule| rule.actions()).unwrap_or(&[]) } }