From 05356d7d4ec7b508feecfdbaf5b37b04c295fa6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 4 Nov 2022 18:41:47 +0100 Subject: [PATCH] push: Add method to change the actions of push rules in a Ruleset --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/push.rs | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 9ff6896d..32a29344 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -39,6 +39,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 diff --git a/crates/ruma-common/src/push.rs b/crates/ruma-common/src/push.rs index 58eb647b..08ee1986 100644 --- a/crates/ruma-common/src/push.rs +++ b/crates/ruma-common/src/push.rs @@ -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 can't be found. + pub fn set_actions( + &mut self, + kind: RuleKind, + rule_id: impl AsRef, + actions: Vec, + ) -> 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