push: Add method to remove a user-defined push rule from a Ruleset
This commit is contained in:
parent
a78d2a7dcf
commit
ba296ec466
@ -7,7 +7,9 @@ Breaking changes:
|
|||||||
|
|
||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
- Add method to update the server-default push rules in a `Ruleset`
|
- Add convenience methods for `push::Ruleset`:
|
||||||
|
- To update the server-default push rules
|
||||||
|
- To remove a user-defined push rule
|
||||||
|
|
||||||
# 0.11.3
|
# 0.11.3
|
||||||
|
|
||||||
|
@ -311,6 +311,47 @@ impl Ruleset {
|
|||||||
pub fn get_actions<T>(&self, event: &Raw<T>, context: &PushConditionRoomCtx) -> &[Action] {
|
pub fn get_actions<T>(&self, event: &Raw<T>, context: &PushConditionRoomCtx) -> &[Action] {
|
||||||
self.get_match(event, context).map(|rule| rule.actions()).unwrap_or(&[])
|
self.get_match(event, context).map(|rule| rule.actions()).unwrap_or(&[])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes a user-defined rule in the rule set.
|
||||||
|
///
|
||||||
|
/// Returns an error if the parameters are invalid.
|
||||||
|
pub fn remove(
|
||||||
|
&mut self,
|
||||||
|
kind: RuleKind,
|
||||||
|
rule_id: impl AsRef<str>,
|
||||||
|
) -> Result<(), RemovePushRuleError> {
|
||||||
|
let rule_id = rule_id.as_ref();
|
||||||
|
|
||||||
|
if let Some(rule) = self.get(kind.clone(), rule_id) {
|
||||||
|
if rule.is_server_default() {
|
||||||
|
return Err(RemovePushRuleError::ServerDefault);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(RemovePushRuleError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
|
match kind {
|
||||||
|
RuleKind::Override => {
|
||||||
|
self.override_.shift_remove(rule_id);
|
||||||
|
}
|
||||||
|
RuleKind::Underride => {
|
||||||
|
self.underride.shift_remove(rule_id);
|
||||||
|
}
|
||||||
|
RuleKind::Sender => {
|
||||||
|
self.sender.shift_remove(rule_id);
|
||||||
|
}
|
||||||
|
RuleKind::Room => {
|
||||||
|
self.room.shift_remove(rule_id);
|
||||||
|
}
|
||||||
|
RuleKind::Content => {
|
||||||
|
self.content.shift_remove(rule_id);
|
||||||
|
}
|
||||||
|
// This has been handled in the `self.get` call earlier.
|
||||||
|
RuleKind::_Custom(_) => unreachable!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A push rule is a single rule that states under what conditions an event should be passed onto a
|
/// A push rule is a single rule that states under what conditions an event should be passed onto a
|
||||||
@ -901,6 +942,19 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The error type returned when trying to remove a user-defined push rule from a `Ruleset`.
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum RemovePushRuleError {
|
||||||
|
/// The rule is a server-default rules and they can't be removed.
|
||||||
|
#[error("server-default rules cannot be removed")]
|
||||||
|
ServerDefault,
|
||||||
|
|
||||||
|
/// The rule was not found.
|
||||||
|
#[error("rule not found")]
|
||||||
|
NotFound,
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
@ -53,6 +53,11 @@ impl AnyPushRule {
|
|||||||
self.as_ref().rule_id()
|
self.as_ref().rule_id()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether the push rule is a server-default rule.
|
||||||
|
pub fn is_server_default(&self) -> bool {
|
||||||
|
self.as_ref().is_server_default()
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if the push rule applies to the event.
|
/// Check if the push rule applies to the event.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
@ -168,6 +173,17 @@ impl<'a> AnyPushRuleRef<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether the push rule is a server-default rule.
|
||||||
|
pub fn is_server_default(self) -> bool {
|
||||||
|
match self {
|
||||||
|
Self::Override(rule) => rule.default,
|
||||||
|
Self::Underride(rule) => rule.default,
|
||||||
|
Self::Content(rule) => rule.default,
|
||||||
|
Self::Room(rule) => rule.default,
|
||||||
|
Self::Sender(rule) => rule.default,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Check if the push rule applies to the event.
|
/// Check if the push rule applies to the event.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
|
Loading…
x
Reference in New Issue
Block a user