Skip to main content

matrix_sdk_test/notification_settings/
mod.rs

1use ruma::{
2    RoomId, UserId,
3    power_levels::NotificationPowerLevelsKey,
4    push::{
5        Action, ConditionalPushRule, ConditionalPushRuleInit, EventMatchConditionData,
6        NewConditionalPushRule, NewPushRule, NewSimplePushRule, PatternedPushRule,
7        PatternedPushRuleInit, PredefinedContentRuleId, PredefinedOverrideRuleId, PushCondition,
8        RuleKind, Ruleset, SenderNotificationPermissionConditionData, SoundTweakValue, Tweak,
9    },
10    user_id,
11};
12
13fn user_id() -> &'static UserId {
14    user_id!("@user:matrix.org")
15}
16
17/// The ruleset containing the default spec push rules for the user
18/// `@user:matrix.org`.
19pub fn get_server_default_ruleset() -> Ruleset {
20    Ruleset::server_default(user_id())
21}
22
23/// Build a new ruleset based on the server's default ruleset, by inserting a
24/// list of rules
25pub fn build_ruleset(rule_list: Vec<(RuleKind, &RoomId, bool)>) -> Ruleset {
26    let mut ruleset = get_server_default_ruleset();
27    for (kind, room_id, notify) in rule_list {
28        let actions = if notify {
29            vec![Action::Notify, Action::SetTweak(Tweak::Sound(SoundTweakValue::Default))]
30        } else {
31            vec![]
32        };
33        match kind {
34            RuleKind::Room => {
35                let new_rule = NewSimplePushRule::new(room_id.to_owned(), actions);
36                ruleset.insert(NewPushRule::Room(new_rule), None, None).unwrap();
37            }
38            RuleKind::Override | RuleKind::Underride => {
39                let new_rule = NewConditionalPushRule::new(
40                    room_id.into(),
41                    vec![PushCondition::EventMatch(EventMatchConditionData::new(
42                        "room_id".to_owned(),
43                        room_id.to_string(),
44                    ))],
45                    actions,
46                );
47                let new_push_rule = match kind {
48                    RuleKind::Override => NewPushRule::Override(new_rule),
49                    _ => NewPushRule::Underride(new_rule),
50                };
51                ruleset.insert(new_push_rule, None, None).unwrap();
52            }
53            _ => {}
54        }
55    }
56
57    ruleset
58}
59
60/// The ruleset containing the default spec push rules and the legacy mention
61/// rules for the user `@user:matrix.org`.
62pub fn server_default_ruleset_with_legacy_mentions() -> Ruleset {
63    let mut ruleset = get_server_default_ruleset();
64
65    // In the tests we don't care about the order, so we just add them to the end of
66    // the lists.
67    ruleset.content.insert(contains_user_name_push_rule());
68    ruleset.override_.insert(contains_display_name_push_rule());
69    ruleset.override_.insert(room_notif_push_rule());
70
71    ruleset
72}
73
74/// Room mention rule that was removed from the spec.
75fn room_notif_push_rule() -> ConditionalPushRule {
76    #[allow(deprecated)]
77    ConditionalPushRuleInit {
78        rule_id: PredefinedOverrideRuleId::RoomNotif.to_string(),
79        default: true,
80        enabled: true,
81        conditions: vec![
82            PushCondition::EventMatch(EventMatchConditionData::new(
83                "content.body".into(),
84                "@room".into(),
85            )),
86            PushCondition::SenderNotificationPermission(
87                SenderNotificationPermissionConditionData::new(NotificationPowerLevelsKey::Room),
88            ),
89        ],
90        actions: vec![Action::Notify],
91    }
92    .into()
93}
94
95/// User mention rule that was removed from the spec.
96fn contains_user_name_push_rule() -> PatternedPushRule {
97    #[allow(deprecated)]
98    PatternedPushRuleInit {
99        rule_id: PredefinedContentRuleId::ContainsUserName.to_string(),
100        default: true,
101        enabled: true,
102        pattern: user_id().localpart().into(),
103        actions: vec![Action::Notify],
104    }
105    .into()
106}
107
108/// User mention rule that was removed from the spec.
109fn contains_display_name_push_rule() -> ConditionalPushRule {
110    #[allow(deprecated)]
111    ConditionalPushRuleInit {
112        rule_id: PredefinedOverrideRuleId::ContainsDisplayName.to_string(),
113        default: true,
114        enabled: true,
115        conditions: vec![PushCondition::ContainsDisplayName],
116        actions: vec![Action::Notify],
117    }
118    .into()
119}