matrix_sdk_test/notification_settings/
mod.rs

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