push: Consider push rules to not apply to events sent by the user themselves
This commit is contained in:
parent
24c0a08b2c
commit
6a2950884d
@ -1,5 +1,13 @@
|
|||||||
# [unreleased]
|
# [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
|
# 0.9.2
|
||||||
|
|
||||||
Bug fixes:
|
Bug fixes:
|
||||||
|
@ -109,8 +109,14 @@ impl Ruleset {
|
|||||||
context: &PushConditionRoomCtx,
|
context: &PushConditionRoomCtx,
|
||||||
) -> Option<AnyPushRuleRef<'_>> {
|
) -> Option<AnyPushRuleRef<'_>> {
|
||||||
let event = FlattenedJson::from_raw(event);
|
let event = FlattenedJson::from_raw(event);
|
||||||
|
|
||||||
|
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))
|
self.iter().find(|rule| rule.applies(&event, context))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the push actions that apply to this event.
|
/// Get the push actions that apply to this event.
|
||||||
///
|
///
|
||||||
@ -334,6 +340,10 @@ impl PatternedPushRule {
|
|||||||
event: &FlattenedJson,
|
event: &FlattenedJson,
|
||||||
context: &PushConditionRoomCtx,
|
context: &PushConditionRoomCtx,
|
||||||
) -> bool {
|
) -> 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)
|
self.enabled && condition::check_event_match(event, key, &self.pattern, context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -965,6 +975,7 @@ mod tests {
|
|||||||
let context_one_to_one = &PushConditionRoomCtx {
|
let context_one_to_one = &PushConditionRoomCtx {
|
||||||
room_id: room_id!("!dm:server.name").to_owned(),
|
room_id: room_id!("!dm:server.name").to_owned(),
|
||||||
member_count: uint!(2),
|
member_count: uint!(2),
|
||||||
|
user_id: user_id!("@jj:server.name").to_owned(),
|
||||||
user_display_name: "Jolly Jumper".into(),
|
user_display_name: "Jolly Jumper".into(),
|
||||||
users_power_levels: BTreeMap::new(),
|
users_power_levels: BTreeMap::new(),
|
||||||
default_power_level: int!(50),
|
default_power_level: int!(50),
|
||||||
@ -974,6 +985,7 @@ mod tests {
|
|||||||
let context_public_room = &PushConditionRoomCtx {
|
let context_public_room = &PushConditionRoomCtx {
|
||||||
room_id: room_id!("!far_west:server.name").to_owned(),
|
room_id: room_id!("!far_west:server.name").to_owned(),
|
||||||
member_count: uint!(100),
|
member_count: uint!(100),
|
||||||
|
user_id: user_id!("@jj:server.name").to_owned(),
|
||||||
user_display_name: "Jolly Jumper".into(),
|
user_display_name: "Jolly Jumper".into(),
|
||||||
users_power_levels: BTreeMap::new(),
|
users_power_levels: BTreeMap::new(),
|
||||||
default_power_level: int!(50),
|
default_power_level: int!(50),
|
||||||
@ -1064,6 +1076,7 @@ mod tests {
|
|||||||
let context_one_to_one = &PushConditionRoomCtx {
|
let context_one_to_one = &PushConditionRoomCtx {
|
||||||
room_id: room_id!("!dm:server.name").to_owned(),
|
room_id: room_id!("!dm:server.name").to_owned(),
|
||||||
member_count: uint!(2),
|
member_count: uint!(2),
|
||||||
|
user_id: user_id!("@jj:server.name").to_owned(),
|
||||||
user_display_name: "Jolly Jumper".into(),
|
user_display_name: "Jolly Jumper".into(),
|
||||||
users_power_levels: BTreeMap::new(),
|
users_power_levels: BTreeMap::new(),
|
||||||
default_power_level: int!(50),
|
default_power_level: int!(50),
|
||||||
|
@ -75,6 +75,10 @@ impl PushCondition {
|
|||||||
/// * `event` - The flattened JSON representation of a room message event.
|
/// * `event` - The flattened JSON representation of a room message event.
|
||||||
/// * `context` - The context of the room at the time of the event.
|
/// * `context` - The context of the room at the time of the event.
|
||||||
pub fn applies(&self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool {
|
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 {
|
match self {
|
||||||
Self::EventMatch { key, pattern } => check_event_match(event, key, pattern, context),
|
Self::EventMatch { key, pattern } => check_event_match(event, key, pattern, context),
|
||||||
Self::ContainsDisplayName => {
|
Self::ContainsDisplayName => {
|
||||||
@ -119,6 +123,9 @@ pub struct PushConditionRoomCtx {
|
|||||||
/// The number of members in the room.
|
/// The number of members in the room.
|
||||||
pub member_count: UInt,
|
pub member_count: UInt,
|
||||||
|
|
||||||
|
/// The users matrix ID.
|
||||||
|
pub user_id: OwnedUserId,
|
||||||
|
|
||||||
/// The display name of the current user in the room.
|
/// The display name of the current user in the room.
|
||||||
pub user_display_name: String,
|
pub user_display_name: String,
|
||||||
|
|
||||||
@ -473,6 +480,7 @@ mod tests {
|
|||||||
let context = PushConditionRoomCtx {
|
let context = PushConditionRoomCtx {
|
||||||
room_id: room_id!("!room:server.name").to_owned(),
|
room_id: room_id!("!room:server.name").to_owned(),
|
||||||
member_count: uint!(3),
|
member_count: uint!(3),
|
||||||
|
user_id: user_id!("@gorilla:server.name").to_owned(),
|
||||||
user_display_name: "Groovy Gorilla".into(),
|
user_display_name: "Groovy Gorilla".into(),
|
||||||
users_power_levels,
|
users_power_levels,
|
||||||
default_power_level: int!(50),
|
default_power_level: int!(50),
|
||||||
|
@ -185,6 +185,10 @@ impl<'a> AnyPushRuleRef<'a> {
|
|||||||
/// * `event` - The flattened JSON representation of a room message event.
|
/// * `event` - The flattened JSON representation of a room message event.
|
||||||
/// * `context` - The context of the room at the time of the event.
|
/// * `context` - The context of the room at the time of the event.
|
||||||
pub fn applies(self, event: &FlattenedJson, context: &PushConditionRoomCtx) -> bool {
|
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 {
|
match self {
|
||||||
Self::Override(rule) => rule.applies(event, context),
|
Self::Override(rule) => rule.applies(event, context),
|
||||||
Self::Underride(rule) => rule.applies(event, context),
|
Self::Underride(rule) => rule.applies(event, context),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user