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    /// Sets a custom push rule
29    SetCustomPushRule { rule: NewPushRule },
30}
31
32fn get_notify_actions(notify: bool) -> Vec<Action> {
33    if notify {
34        vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))]
35    } else {
36        vec![]
37    }
38}
39
40impl Command {
41    /// Tries to create a push rule corresponding to this command
42    pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
43        match self {
44            Self::SetRoomPushRule { room_id, notify } => {
45                // `Room` push rule for this `room_id`
46                let new_rule = NewSimplePushRule::new(room_id.clone(), get_notify_actions(*notify));
47                Ok(NewPushRule::Room(new_rule))
48            }
49
50            Self::SetOverridePushRule { rule_id, room_id, notify } => {
51                // `Override` push rule matching this `room_id`
52                let new_rule = NewConditionalPushRule::new(
53                    rule_id.clone(),
54                    vec![PushCondition::EventMatch {
55                        key: "room_id".to_owned(),
56                        pattern: room_id.to_string(),
57                    }],
58                    get_notify_actions(*notify),
59                );
60                Ok(NewPushRule::Override(new_rule))
61            }
62
63            Self::SetKeywordPushRule { keyword } => {
64                // `Content` push rule matching this keyword
65                let new_rule = NewPatternedPushRule::new(
66                    keyword.clone(),
67                    keyword.clone(),
68                    get_notify_actions(true),
69                );
70                Ok(NewPushRule::Content(new_rule))
71            }
72
73            Self::SetPushRuleEnabled { .. }
74            | Self::DeletePushRule { .. }
75            | Self::SetPushRuleActions { .. } => Err(NotificationSettingsError::InvalidParameter(
76                "cannot create a push rule from this command.".to_owned(),
77            )),
78
79            Self::SetCustomPushRule { rule } => Ok(rule.clone()),
80        }
81    }
82}