matrix_sdk/notification_settings/
command.rs1use 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#[derive(Clone, Debug)]
15pub(crate) enum Command {
16 SetRoomPushRule { room_id: OwnedRoomId, notify: bool },
18 SetOverridePushRule { rule_id: String, room_id: OwnedRoomId, notify: bool },
20 SetKeywordPushRule { keyword: String },
22 SetPushRuleEnabled { kind: RuleKind, rule_id: String, enabled: bool },
24 DeletePushRule { kind: RuleKind, rule_id: String },
26 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 pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
41 match self {
42 Self::SetRoomPushRule { room_id, notify } => {
43 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 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 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}