matrix_sdk_test/notification_settings/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use ruma::{
    push::{
        Action, NewConditionalPushRule, NewPushRule, NewSimplePushRule, PushCondition, RuleKind,
        Ruleset, Tweak,
    },
    RoomId, UserId,
};

pub fn get_server_default_ruleset() -> Ruleset {
    let user_id = UserId::parse("@user:matrix.org").unwrap();
    Ruleset::server_default(&user_id)
}

/// Build a new ruleset based on the server's default ruleset, by inserting a
/// list of rules
pub fn build_ruleset(rule_list: Vec<(RuleKind, &RoomId, bool)>) -> Ruleset {
    let mut ruleset = get_server_default_ruleset();
    for (kind, room_id, notify) in rule_list {
        let actions = if notify {
            vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))]
        } else {
            vec![]
        };
        match kind {
            RuleKind::Room => {
                let new_rule = NewSimplePushRule::new(room_id.to_owned(), actions);
                ruleset.insert(NewPushRule::Room(new_rule), None, None).unwrap();
            }
            RuleKind::Override | RuleKind::Underride => {
                let new_rule = NewConditionalPushRule::new(
                    room_id.into(),
                    vec![PushCondition::EventMatch {
                        key: "room_id".to_owned(),
                        pattern: room_id.to_string(),
                    }],
                    actions,
                );
                let new_push_rule = match kind {
                    RuleKind::Override => NewPushRule::Override(new_rule),
                    _ => NewPushRule::Underride(new_rule),
                };
                ruleset.insert(new_push_rule, None, None).unwrap();
            }
            _ => {}
        }
    }

    ruleset
}