diff --git a/ruma-common/src/push.rs b/ruma-common/src/push.rs index 9dc8c24b..572bfa23 100644 --- a/ruma-common/src/push.rs +++ b/ruma-common/src/push.rs @@ -107,27 +107,31 @@ impl Ruleset { { let event_map = &FlattenedJson::from_raw(event); - for rule in self.override_.iter().filter(|r| r.enabled) { + for rule in &self.override_ { if rule.applies(event_map, context) { return rule.actions.iter(); } } - for rule in self.content.iter().filter(|r| r.enabled) { + for rule in &self.content { if rule.applies_to("content.body", event_map, context) { return rule.actions.iter(); } } - for rule in self.room.iter().filter(|r| r.enabled) { - if condition::check_event_match(event_map, "room_id", &rule.rule_id, context) { + 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.iter().filter(|r| r.enabled) { - if condition::check_event_match(event_map, "sender", &rule.rule_id, context) { + 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.iter().filter(|r| r.enabled) { + for rule in &self.underride { if rule.applies(event_map, context) { return rule.actions.iter(); } @@ -238,6 +242,18 @@ pub struct ConditionalPushRule { pub conditions: Vec, } +impl ConditionalPushRule { + /// 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.enabled && self.conditions.iter().all(|cond| cond.applies(event, context)) + } +} + /// Initial set of fields of `ConditionalPushRule`. /// /// This struct will not be updated even if additional fields are added to `ConditionalPushRule` in @@ -270,18 +286,6 @@ impl From for ConditionalPushRule { } } -impl ConditionalPushRule { - /// 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.conditions.iter().all(|cond| cond.applies(event, context)) - } -} - // The following trait are needed to be able to make // an IndexSet of the type @@ -343,7 +347,7 @@ impl PatternedPushRule { event: &FlattenedJson, context: &PushConditionRoomCtx, ) -> bool { - condition::check_event_match(event, key, &self.pattern, context) + self.enabled && condition::check_event_match(event, key, &self.pattern, context) } }