matrix_sdk/notification_settings/
command.rsuse std::fmt::Debug;
use ruma::{
push::{
Action, NewConditionalPushRule, NewPatternedPushRule, NewPushRule, NewSimplePushRule,
PushCondition, RuleKind, Tweak,
},
OwnedRoomId,
};
use crate::NotificationSettingsError;
#[derive(Clone, Debug)]
pub(crate) enum Command {
SetRoomPushRule { room_id: OwnedRoomId, notify: bool },
SetOverridePushRule { rule_id: String, room_id: OwnedRoomId, notify: bool },
SetKeywordPushRule { keyword: String },
SetPushRuleEnabled { kind: RuleKind, rule_id: String, enabled: bool },
DeletePushRule { kind: RuleKind, rule_id: String },
SetPushRuleActions { kind: RuleKind, rule_id: String, actions: Vec<Action> },
}
fn get_notify_actions(notify: bool) -> Vec<Action> {
if notify {
vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))]
} else {
vec![]
}
}
impl Command {
pub(crate) fn to_push_rule(&self) -> Result<NewPushRule, NotificationSettingsError> {
match self {
Self::SetRoomPushRule { room_id, notify } => {
let new_rule = NewSimplePushRule::new(room_id.clone(), get_notify_actions(*notify));
Ok(NewPushRule::Room(new_rule))
}
Self::SetOverridePushRule { rule_id, room_id, notify } => {
let new_rule = NewConditionalPushRule::new(
rule_id.clone(),
vec![PushCondition::EventMatch {
key: "room_id".to_owned(),
pattern: room_id.to_string(),
}],
get_notify_actions(*notify),
);
Ok(NewPushRule::Override(new_rule))
}
Self::SetKeywordPushRule { keyword } => {
let new_rule = NewPatternedPushRule::new(
keyword.clone(),
keyword.clone(),
get_notify_actions(true),
);
Ok(NewPushRule::Content(new_rule))
}
Self::SetPushRuleEnabled { .. }
| Self::DeletePushRule { .. }
| Self::SetPushRuleActions { .. } => Err(NotificationSettingsError::InvalidParameter(
"cannot create a push rule from this command.".to_owned(),
)),
}
}
}