common: Add PatternedPushRule::applies_to
This commit is contained in:
parent
045f610e30
commit
5d0fb3924e
@ -113,28 +113,17 @@ impl Ruleset {
|
||||
}
|
||||
}
|
||||
for rule in self.content.iter().filter(|r| r.enabled) {
|
||||
let condition = PushCondition::EventMatch {
|
||||
key: "content.body".into(),
|
||||
pattern: rule.pattern.clone(),
|
||||
};
|
||||
|
||||
if condition.applies(event_map, context) {
|
||||
if rule.applies_to("content.body", event_map, context) {
|
||||
return rule.actions.iter();
|
||||
}
|
||||
}
|
||||
for rule in self.room.iter().filter(|r| r.enabled) {
|
||||
let condition =
|
||||
PushCondition::EventMatch { key: "room_id".into(), pattern: rule.rule_id.clone() };
|
||||
|
||||
if condition.applies(event_map, context) {
|
||||
if 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) {
|
||||
let condition =
|
||||
PushCondition::EventMatch { key: "sender".into(), pattern: rule.rule_id.clone() };
|
||||
|
||||
if condition.applies(event_map, context) {
|
||||
if condition::check_event_match(event_map, "sender", &rule.rule_id, context) {
|
||||
return rule.actions.iter();
|
||||
}
|
||||
}
|
||||
@ -341,6 +330,23 @@ pub struct PatternedPushRule {
|
||||
pub pattern: String,
|
||||
}
|
||||
|
||||
impl PatternedPushRule {
|
||||
/// 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_to(
|
||||
&self,
|
||||
key: &str,
|
||||
event: &FlattenedJson,
|
||||
context: &PushConditionRoomCtx,
|
||||
) -> bool {
|
||||
condition::check_event_match(event, key, &self.pattern, context)
|
||||
}
|
||||
}
|
||||
|
||||
/// Initial set of fields of `PatterenedPushRule`.
|
||||
///
|
||||
/// This struct will not be updated even if additional fields are added to `PatterenedPushRule` in a
|
||||
|
@ -52,6 +52,23 @@ pub enum PushCondition {
|
||||
},
|
||||
}
|
||||
|
||||
pub(super) fn check_event_match(
|
||||
event: &FlattenedJson,
|
||||
key: &str,
|
||||
pattern: &str,
|
||||
context: &PushConditionRoomCtx,
|
||||
) -> bool {
|
||||
let value = match key {
|
||||
"room_id" => context.room_id.as_str(),
|
||||
_ => match event.get(key) {
|
||||
Some(v) => v,
|
||||
None => return false,
|
||||
},
|
||||
};
|
||||
|
||||
value.matches_pattern(pattern, key == "content.body")
|
||||
}
|
||||
|
||||
impl PushCondition {
|
||||
/// Check if this condition applies to the event.
|
||||
///
|
||||
@ -61,17 +78,7 @@ impl PushCondition {
|
||||
/// * `context` - The context of the room at the time of the event.
|
||||
pub fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool {
|
||||
match self {
|
||||
Self::EventMatch { key, pattern } => {
|
||||
let value = match key.as_str() {
|
||||
"room_id" => context.room_id.as_str(),
|
||||
_ => match event.get(key) {
|
||||
Some(v) => v,
|
||||
None => return false,
|
||||
},
|
||||
};
|
||||
|
||||
value.matches_pattern(pattern, key == "content.body")
|
||||
}
|
||||
Self::EventMatch { key, pattern } => check_event_match(event, &key, &pattern, context),
|
||||
Self::ContainsDisplayName => {
|
||||
let value = match event.get("content.body") {
|
||||
Some(v) => v,
|
||||
|
Loading…
x
Reference in New Issue
Block a user