Skip to content

Commit

Permalink
push: Add method to change the actions of push rules in a Ruleset
Browse files Browse the repository at this point in the history
  • Loading branch information
zecakeh committed Nov 4, 2022
1 parent d9ee8c8 commit 6225726
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/ruma-common/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Improvements:
* `Ruleset::get` to access a push rule
* `Ruleset::insert` to add or update user push rules
* `Ruleset::set_enabled` to change the enabled state of push rules
* `Ruleset::set_actions` to change the actions of push rules

# 0.10.5

Expand Down
44 changes: 44 additions & 0 deletions crates/ruma-common/src/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,50 @@ impl Ruleset {
Ok(())
}

/// Set the actions of the rule from the given kind and with the given `rule_id` in the rule
/// set.
///
/// Returns an error if the rule wasn't found.
pub fn set_actions(
&mut self,
kind: RuleKind,
rule_id: impl AsRef<str>,
actions: Vec<Action>,
) -> Result<(), RuleNotFoundError> {
let rule_id = rule_id.as_ref();

match kind {
RuleKind::Override => {
let mut rule = self.override_.get(rule_id).ok_or(RuleNotFoundError)?.clone();
rule.actions = actions;
self.override_.replace(rule);
}
RuleKind::Underride => {
let mut rule = self.underride.get(rule_id).ok_or(RuleNotFoundError)?.clone();
rule.actions = actions;
self.underride.replace(rule);
}
RuleKind::Sender => {
let mut rule = self.sender.get(rule_id).ok_or(RuleNotFoundError)?.clone();
rule.actions = actions;
self.sender.replace(rule);
}
RuleKind::Room => {
let mut rule = self.room.get(rule_id).ok_or(RuleNotFoundError)?.clone();
rule.actions = actions;
self.room.replace(rule);
}
RuleKind::Content => {
let mut rule = self.content.get(rule_id).ok_or(RuleNotFoundError)?.clone();
rule.actions = actions;
self.content.replace(rule);
}
RuleKind::_Custom(_) => return Err(RuleNotFoundError),
}

Ok(())
}

/// Get the first push rule that applies to this event, if any.
///
/// # Arguments
Expand Down

0 comments on commit 6225726

Please sign in to comment.