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#[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 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 pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
43 match self {
44 Self::SetRoomPushRule { room_id, notify } => {
45 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 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 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}