common: Add PatternedPushRule::applies_to

This commit is contained in:
Jonas Platte 2021-04-17 17:26:17 +02:00
parent 045f610e30
commit 5d0fb3924e
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
2 changed files with 38 additions and 25 deletions

View File

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

View File

@ -52,17 +52,13 @@ pub enum PushCondition {
},
}
impl PushCondition {
/// Check if this condition 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::EventMatch { key, pattern } => {
let value = match key.as_str() {
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,
@ -72,6 +68,17 @@ impl PushCondition {
value.matches_pattern(pattern, key == "content.body")
}
impl PushCondition {
/// Check if this condition 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::EventMatch { key, pattern } => check_event_match(event, &key, &pattern, context),
Self::ContainsDisplayName => {
let value = match event.get("content.body") {
Some(v) => v,