push: Consider push rules to not apply to events sent by the user themselves

This commit is contained in:
Jonas Platte 2022-05-11 17:34:29 +02:00 committed by Jonas Platte
parent 24c0a08b2c
commit 6a2950884d
4 changed files with 34 additions and 1 deletions

View File

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

View File

@ -109,7 +109,13 @@ impl Ruleset {
context: &PushConditionRoomCtx,
) -> Option<AnyPushRuleRef<'_>> {
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),

View File

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

View File

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