common: Add more useful methods to AnyPushRule, AnyPushRuleRef

This commit is contained in:
Jonas Platte 2021-04-17 17:44:24 +02:00
parent fc423b3620
commit aa8e00f886
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 75 additions and 41 deletions

View File

@ -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()
}
}

View File

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