common: Add Ruleset::get_match

This commit is contained in:
Jonas Platte 2021-04-17 17:58:53 +02:00
parent c4e918cfc4
commit 32398d187b
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 22 additions and 5 deletions

View File

@ -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

View File

@ -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<T>(
&self,
event: &Raw<T>,
context: &PushConditionRoomCtx,
) -> Option<AnyPushRuleRef<'_>>
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(&[])
}
}