diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 0cbdf2d4..565ebb73 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -1,5 +1,13 @@ # [unreleased] +Breaking changes: + +* Add `user_id` field to `PushConditionRoomCtx` + +Improvements: + +* All push rules are now considered to not apply to events sent by the user themselves + # 0.9.2 Bug fixes: diff --git a/crates/ruma-common/src/push.rs b/crates/ruma-common/src/push.rs index 82f326df..65b3abdf 100644 --- a/crates/ruma-common/src/push.rs +++ b/crates/ruma-common/src/push.rs @@ -109,7 +109,13 @@ impl Ruleset { context: &PushConditionRoomCtx, ) -> Option> { let event = FlattenedJson::from_raw(event); - self.iter().find(|rule| rule.applies(&event, context)) + + if event.get("sender").map_or(false, |sender| sender == context.user_id) { + // no need to look at the rules if the event was by the user themselves + None + } else { + self.iter().find(|rule| rule.applies(&event, context)) + } } /// Get the push actions that apply to this event. @@ -334,6 +340,10 @@ impl PatternedPushRule { event: &FlattenedJson, context: &PushConditionRoomCtx, ) -> bool { + if event.get("sender").map_or(false, |sender| sender == context.user_id) { + return false; + } + self.enabled && condition::check_event_match(event, key, &self.pattern, context) } } @@ -965,6 +975,7 @@ mod tests { let context_one_to_one = &PushConditionRoomCtx { room_id: room_id!("!dm:server.name").to_owned(), member_count: uint!(2), + user_id: user_id!("@jj:server.name").to_owned(), user_display_name: "Jolly Jumper".into(), users_power_levels: BTreeMap::new(), default_power_level: int!(50), @@ -974,6 +985,7 @@ mod tests { let context_public_room = &PushConditionRoomCtx { room_id: room_id!("!far_west:server.name").to_owned(), member_count: uint!(100), + user_id: user_id!("@jj:server.name").to_owned(), user_display_name: "Jolly Jumper".into(), users_power_levels: BTreeMap::new(), default_power_level: int!(50), @@ -1064,6 +1076,7 @@ mod tests { let context_one_to_one = &PushConditionRoomCtx { room_id: room_id!("!dm:server.name").to_owned(), member_count: uint!(2), + user_id: user_id!("@jj:server.name").to_owned(), user_display_name: "Jolly Jumper".into(), users_power_levels: BTreeMap::new(), default_power_level: int!(50), diff --git a/crates/ruma-common/src/push/condition.rs b/crates/ruma-common/src/push/condition.rs index 0789dd94..a5588268 100644 --- a/crates/ruma-common/src/push/condition.rs +++ b/crates/ruma-common/src/push/condition.rs @@ -75,6 +75,10 @@ impl PushCondition { /// * `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 { + if event.get("sender").map_or(false, |sender| sender == context.user_id) { + return false; + } + match self { Self::EventMatch { key, pattern } => check_event_match(event, key, pattern, context), Self::ContainsDisplayName => { @@ -119,6 +123,9 @@ pub struct PushConditionRoomCtx { /// The number of members in the room. pub member_count: UInt, + /// The users matrix ID. + pub user_id: OwnedUserId, + /// The display name of the current user in the room. pub user_display_name: String, @@ -473,6 +480,7 @@ mod tests { let context = PushConditionRoomCtx { room_id: room_id!("!room:server.name").to_owned(), member_count: uint!(3), + user_id: user_id!("@gorilla:server.name").to_owned(), user_display_name: "Groovy Gorilla".into(), users_power_levels, default_power_level: int!(50), diff --git a/crates/ruma-common/src/push/iter.rs b/crates/ruma-common/src/push/iter.rs index d78501b0..f85a2bd7 100644 --- a/crates/ruma-common/src/push/iter.rs +++ b/crates/ruma-common/src/push/iter.rs @@ -185,6 +185,10 @@ impl<'a> AnyPushRuleRef<'a> { /// * `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 { + if event.get("sender").map_or(false, |sender| sender == context.user_id) { + return false; + } + match self { Self::Override(rule) => rule.applies(event, context), Self::Underride(rule) => rule.applies(event, context),