conduit/src/push_rules.rs

248 lines
7.1 KiB
Rust
Raw Normal View History

2020-06-12 11:18:25 +00:00
use ruma::{
push::{
2020-07-25 18:25:24 +00:00
Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule,
PatternedPushRuleInit, PushCondition, RoomMemberCountIs, Ruleset, Tweak,
},
2020-07-26 20:45:10 +00:00
UserId,
2020-06-12 11:18:25 +00:00
};
pub fn default_pushrules(user_id: &UserId) -> Ruleset {
2020-07-25 18:25:24 +00:00
let mut rules = Ruleset::default();
rules.content = vec![contains_user_name_rule(&user_id)];
rules.override_ = vec![
master_rule(),
suppress_notices_rule(),
invite_for_me_rule(),
member_event_rule(),
contains_display_name_rule(),
tombstone_rule(),
roomnotif_rule(),
];
rules.underride = vec![
call_rule(),
encrypted_room_one_to_one_rule(),
room_one_to_one_rule(),
message_rule(),
encrypted_rule(),
];
rules
2020-06-12 11:18:25 +00:00
}
pub fn master_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::DontNotify],
default: true,
enabled: false,
rule_id: ".m.rule.master".to_owned(),
conditions: vec![],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn suppress_notices_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::DontNotify],
default: true,
enabled: true,
rule_id: ".m.rule.suppress_notices".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.msgtype".to_owned(),
pattern: "m.notice".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn invite_for_me_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.invite_for_me".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.membership".to_owned(),
pattern: "m.invite".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn member_event_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::DontNotify],
default: true,
enabled: true,
rule_id: ".m.rule.member_event".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "content.membership".to_owned(),
pattern: "type".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn contains_display_name_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(true)),
],
default: true,
enabled: true,
rule_id: ".m.rule.contains_display_name".to_owned(),
conditions: vec![PushCondition::ContainsDisplayName],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn tombstone_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
rule_id: ".m.rule.tombstone".to_owned(),
conditions: vec![
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.tombstone".to_owned(),
},
PushCondition::EventMatch {
key: "state_key".to_owned(),
pattern: "".to_owned(),
},
],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn roomnotif_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(true))],
default: true,
enabled: true,
rule_id: ".m.rule.roomnotif".to_owned(),
conditions: vec![
PushCondition::EventMatch {
key: "content.body".to_owned(),
pattern: "@room".to_owned(),
},
PushCondition::SenderNotificationPermission {
key: "room".to_owned(),
},
],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn contains_user_name_rule(user_id: &UserId) -> PatternedPushRule {
2020-07-25 18:25:24 +00:00
PatternedPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(true)),
],
default: true,
enabled: true,
rule_id: ".m.rule.contains_user_name".to_owned(),
pattern: user_id.localpart().to_owned(),
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn call_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("ring".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.call".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.call.invite".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(2_u32.into()..),
},
2020-06-12 11:18:25 +00:00
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.encrypted".to_owned(),
},
],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn room_one_to_one_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![
Action::Notify,
Action::SetTweak(Tweak::Sound("default".to_owned())),
Action::SetTweak(Tweak::Highlight(false)),
],
default: true,
enabled: true,
rule_id: ".m.rule.room_one_to_one".to_owned(),
conditions: vec![
PushCondition::RoomMemberCount {
is: RoomMemberCountIs::from(2_u32.into()..),
},
2020-06-12 11:18:25 +00:00
PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.message".to_owned(),
},
],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn message_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
rule_id: ".m.rule.message".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.message".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}
pub fn encrypted_rule() -> ConditionalPushRule {
2020-07-25 18:25:24 +00:00
ConditionalPushRuleInit {
2020-06-12 11:18:25 +00:00
actions: vec![Action::Notify, Action::SetTweak(Tweak::Highlight(false))],
default: true,
enabled: true,
rule_id: ".m.rule.encrypted".to_owned(),
conditions: vec![PushCondition::EventMatch {
key: "type".to_owned(),
pattern: "m.room.encrypted".to_owned(),
}],
}
2020-07-25 18:25:24 +00:00
.into()
2020-06-12 11:18:25 +00:00
}