Skip to main content

palpo_core/push/
push_rule.rs

1//! Common types for the [push notifications module][push].
2//!
3//! [push]: https://spec.matrix.org/latest/client-server-api/#push-notifications
4//!
5//! ## Understanding the types of this module
6//!
7//! Push rules are grouped in `RuleSet`s, and are grouped in five kinds (for
8//! more details about the different kind of rules, see the `Ruleset` documentation,
9//! or the specification). These five kinds are, by order of priority:
10//!
11//! - override rules
12//! - content rules
13//! - room rules
14//! - sender rules
15//! - underride rules
16
17use salvo::prelude::*;
18use serde::{Deserialize, Serialize};
19
20use crate::push::Action;
21use crate::push::NewConditionalPushRule;
22use crate::push::NewPatternedPushRule;
23use crate::push::NewSimplePushRule;
24use crate::push::PushCondition;
25use crate::{OwnedRoomId, OwnedUserId, PrivOwnedStr, serde::StringEnum};
26
27/// The kinds of push rules that are available.
28#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
29#[derive(ToSchema, Clone, PartialEq, Eq, PartialOrd, Ord, StringEnum)]
30#[palpo_enum(rename_all = "snake_case")]
31#[non_exhaustive]
32pub enum RuleKind {
33    /// User-configured rules that override all other kinds.
34    Override,
35
36    /// Lowest priority user-defined rules.
37    Underride,
38
39    /// Sender-specific rules.
40    Sender,
41
42    /// Room-specific rules.
43    Room,
44
45    /// Content-specific rules.
46    Content,
47
48    #[doc(hidden)]
49    #[salvo(schema(value_type = String))]
50    _Custom(PrivOwnedStr),
51}
52
53/// A push rule to update or create.
54#[derive(ToSchema, Deserialize, Clone, Debug)]
55pub enum NewPushRule {
56    /// Rules that override all other kinds.
57    Override(NewConditionalPushRule),
58
59    /// Content-specific rules.
60    Content(NewPatternedPushRule),
61
62    /// Room-specific rules.
63    Room(NewSimplePushRule<OwnedRoomId>),
64
65    /// Sender-specific rules.
66    Sender(NewSimplePushRule<OwnedUserId>),
67
68    /// Lowest priority rules.
69    Underride(NewConditionalPushRule),
70}
71
72impl NewPushRule {
73    /// The kind of this `NewPushRule`.
74    pub fn kind(&self) -> RuleKind {
75        match self {
76            NewPushRule::Override(_) => RuleKind::Override,
77            NewPushRule::Content(_) => RuleKind::Content,
78            NewPushRule::Room(_) => RuleKind::Room,
79            NewPushRule::Sender(_) => RuleKind::Sender,
80            NewPushRule::Underride(_) => RuleKind::Underride,
81        }
82    }
83
84    /// The ID of this `NewPushRule`.
85    pub fn rule_id(&self) -> &str {
86        match self {
87            NewPushRule::Override(r) => &r.rule_id,
88            NewPushRule::Content(r) => &r.rule_id,
89            NewPushRule::Room(r) => r.rule_id.as_ref(),
90            NewPushRule::Sender(r) => r.rule_id.as_ref(),
91            NewPushRule::Underride(r) => &r.rule_id,
92        }
93    }
94}
95
96/// The scope of a push rule.
97#[doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/doc/string_enum.md"))]
98#[derive(ToSchema, Clone, PartialEq, Eq, StringEnum)]
99#[palpo_enum(rename_all = "lowercase")]
100#[non_exhaustive]
101pub enum RuleScope {
102    /// The global rules.
103    Global,
104
105    #[doc(hidden)]
106    #[salvo(schema(skip))]
107    _Custom(PrivOwnedStr),
108}
109
110/// Like `SimplePushRule`, but may represent any kind of push rule thanks to `pattern` and
111/// `conditions` being optional.
112///
113/// To create an instance of this type, use one of its `From` implementations.
114#[derive(ToSchema, Clone, Debug, Serialize, Deserialize)]
115pub struct PushRule {
116    /// The actions to perform when this rule is matched.
117    pub actions: Vec<Action>,
118
119    /// Whether this is a default rule, or has been set explicitly.
120    pub default: bool,
121
122    /// Whether the push rule is enabled or not.
123    pub enabled: bool,
124
125    /// The ID of this rule.
126    pub rule_id: String,
127
128    /// The conditions that must hold true for an event in order for a rule to be applied to an
129    /// event.
130    ///
131    /// A rule with no conditions always matches. Only applicable to underride and override rules.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub conditions: Option<Vec<PushCondition>>,
134
135    /// The glob-style pattern to match against.
136    ///
137    /// Only applicable to content rules.
138    #[serde(skip_serializing_if = "Option::is_none")]
139    pub pattern: Option<String>,
140}
141
142#[derive(ToParameters, Deserialize, Debug)]
143pub struct ScopeKindRuleReqArgs {
144    /// The scope to fetch rules from.
145    #[salvo(parameter(parameter_in = Path))]
146    pub scope: RuleScope,
147
148    /// The kind of rule.
149    #[salvo(parameter(parameter_in = Path))]
150    pub kind: RuleKind,
151
152    /// The identifier for the rule.
153    #[salvo(parameter(parameter_in = Path))]
154    pub rule_id: String,
155}