matrix_sdk/notification_settings/
command.rs

1use std::fmt::Debug;
2
3use ruma::{
4    push::{
5        Action, NewConditionalPushRule, NewPatternedPushRule, NewPushRule, NewSimplePushRule,
6        PushCondition, RuleKind, Tweak,
7    },
8    OwnedRoomId,
9};
10
11use crate::NotificationSettingsError;
12
13/// Enum describing the commands required to modify the owner's account data.
14#[derive(Clone, Debug)]
15pub(crate) enum Command {
16    /// Set a new `Room` push rule
17    SetRoomPushRule { room_id: OwnedRoomId, notify: bool },
18    /// Set a new `Override` push rule matching a `RoomId`
19    SetOverridePushRule { rule_id: String, room_id: OwnedRoomId, notify: bool },
20    /// Set a new push rule for a keyword.
21    SetKeywordPushRule { keyword: String },
22    /// Set whether a push rule is enabled
23    SetPushRuleEnabled { kind: RuleKind, rule_id: String, enabled: bool },
24    /// Delete a push rule
25    DeletePushRule { kind: RuleKind, rule_id: String },
26    /// Set a list of actions
27    SetPushRuleActions { kind: RuleKind, rule_id: String, actions: Vec<Action> },
28}
29
30fn get_notify_actions(notify: bool) -> Vec<Action> {
31    if notify {
32        vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))]
33    } else {
34        vec![]
35    }
36}
37
38impl Command {
39    /// Tries to create a push rule corresponding to this command
40    pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
41        match self {
42            Self::SetRoomPushRule { room_id, notify } => {
43                // `Room` push rule for this `room_id`
44                let new_rule = NewSimplePushRule::new(room_id.clone(), get_notify_actions(*notify));
45                Ok(NewPushRule::Room(new_rule))
46            }
47
48            Self::SetOverridePushRule { rule_id, room_id, notify } => {
49                // `Override` push rule matching this `room_id`
50                let new_rule = NewConditionalPushRule::new(
51                    rule_id.clone(),
52                    vec![PushCondition::EventMatch {
53                        key: "room_id".to_owned(),
54                        pattern: room_id.to_string(),
55                    }],
56                    get_notify_actions(*notify),
57                );
58                Ok(NewPushRule::Override(new_rule))
59            }
60
61            Self::SetKeywordPushRule { keyword } => {
62                // `Content` push rule matching this keyword
63                let new_rule = NewPatternedPushRule::new(
64                    keyword.clone(),
65                    keyword.clone(),
66                    get_notify_actions(true),
67                );
68                Ok(NewPushRule::Content(new_rule))
69            }
70
71            Self::SetPushRuleEnabled { .. }
72            | Self::DeletePushRule { .. }
73            | Self::SetPushRuleActions { .. } => Err(NotificationSettingsError::InvalidParameter(
74                "cannot create a push rule from this command.".to_owned(),
75            )),
76        }
77    }
78}