250 lines
8.4 KiB
Rust

//! Common types for the [push notifications module][push]
//!
//! [push]: https://matrix.org/docs/spec/client_server/r0.6.1#id89
use ruma_common::StringEnum;
use serde::{Deserialize, Serialize};
mod action;
mod any_push_rule;
mod condition;
pub use self::{
action::{Action, Tweak},
any_push_rule::{AnyPushRule, MissingConditionsError, MissingPatternError},
condition::{ComparisonOperator, PushCondition, RoomMemberCountIs},
};
/// A push ruleset scopes a set of rules according to some criteria.
///
/// For example, some rules may only be applied for messages from a particular sender, a particular
/// room, or by default. The push ruleset contains the entire set of scopes and rules.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Ruleset {
/// These rules configure behavior for (unencrypted) messages that match certain patterns.
pub content: Vec<PatternedPushRule>,
/// These user-configured rules are given the highest priority.
///
/// This field is named `override_` instead of `override` because the latter is a reserved
/// keyword in Rust.
#[serde(rename = "override")]
pub override_: Vec<ConditionalPushRule>,
/// These rules change the behavior of all messages for a given room.
pub room: Vec<PushRule>,
/// These rules configure notification behavior for messages from a specific Matrix user ID.
pub sender: Vec<PushRule>,
/// These rules are identical to override rules, but have a lower priority than `content`,
/// `room` and `sender` rules.
pub underride: Vec<ConditionalPushRule>,
}
impl Ruleset {
/// Creates an empty `Ruleset`.
pub fn new() -> Self {
Default::default()
}
}
/// A push rule is a single rule that states under what conditions an event should be passed onto a
/// push gateway and how the notification should be presented.
///
/// These rules are stored on the user's homeserver. They are manually configured by the user, who
/// can create and view them via the Client/Server API.
///
/// To create an instance of this type, first create a `PushRuleInit` and convert it via
/// `PushRule::from` / `.into()`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PushRule {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
}
/// Initial set of fields of `PushRule`.
///
/// This struct will not be updated even if additional fields are added to `PushRule` in a new
/// (non-breaking) release of the Matrix specification.
#[derive(Debug)]
pub struct PushRuleInit {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
}
impl From<PushRuleInit> for PushRule {
fn from(init: PushRuleInit) -> Self {
let PushRuleInit { actions, default, enabled, rule_id } = init;
Self { actions, default, enabled, rule_id }
}
}
/// Like `PushRule`, but with an additional `conditions` field.
///
/// Only applicable to underride and override rules.
///
/// To create an instance of this type, first create a `ConditionalPushRuleInit` and convert it via
/// `ConditionalPushRule::from` / `.into()`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct ConditionalPushRule {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
/// The conditions that must hold true for an event in order for a rule to be applied to an
/// event.
///
/// A rule with no conditions always matches.
pub conditions: Vec<PushCondition>,
}
/// Initial set of fields of `ConditionalPushRule`.
///
/// This struct will not be updated even if additional fields are added to `ConditionalPushRule` in
/// a new (non-breaking) release of the Matrix specification.
#[derive(Debug)]
pub struct ConditionalPushRuleInit {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
/// The conditions that must hold true for an event in order for a rule to be applied to an
/// event.
///
/// A rule with no conditions always matches.
pub conditions: Vec<PushCondition>,
}
impl From<ConditionalPushRuleInit> for ConditionalPushRule {
fn from(init: ConditionalPushRuleInit) -> Self {
let ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions } = init;
Self { actions, default, enabled, rule_id, conditions }
}
}
/// Like `PushRule`, but with an additional `pattern` field.
///
/// Only applicable to content rules.
///
/// To create an instance of this type, first create a `PatternedPushRuleInit` and convert it via
/// `PatternedPushRule::from` / `.into()`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PatternedPushRule {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
/// The glob-style pattern to match against.
pub pattern: String,
}
/// Initial set of fields of `PatterenedPushRule`.
///
/// This struct will not be updated even if additional fields are added to `PatterenedPushRule` in a
/// new (non-breaking) release of the Matrix specification.
#[derive(Debug)]
pub struct PatternedPushRuleInit {
/// Actions to determine if and how a notification is delivered for events matching this rule.
pub actions: Vec<Action>,
/// Whether this is a default rule, or has been set explicitly.
pub default: bool,
/// Whether the push rule is enabled or not.
pub enabled: bool,
/// The ID of this rule.
pub rule_id: String,
/// The glob-style pattern to match against.
pub pattern: String,
}
impl From<PatternedPushRuleInit> for PatternedPushRule {
fn from(init: PatternedPushRuleInit) -> Self {
let PatternedPushRuleInit { actions, default, enabled, rule_id, pattern } = init;
Self { actions, default, enabled, rule_id, pattern }
}
}
/// Information for the pusher implementation itself.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PusherData {
/// Required if the pusher's kind is http. The URL to use to send notifications to.
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
/// The format to use when sending notifications to the Push Gateway.
#[serde(skip_serializing_if = "Option::is_none")]
pub format: Option<PushFormat>,
}
impl PusherData {
/// Creates an empty `PusherData`.
pub fn new() -> Self {
Default::default()
}
}
/// A special format that the homeserver should use when sending notifications to a Push Gateway.
/// Currently, only "event_id_only" is supported as of [Push Gateway API r0.1.1][spec].
///
/// [spec]: https://matrix.org/docs/spec/push_gateway/r0.1.1#homeserver-behaviour
#[derive(Clone, Debug, PartialEq, Eq, StringEnum)]
#[ruma_enum(rename_all = "snake_case")]
pub enum PushFormat {
/// Require the homeserver to only send a reduced set of fields in the push.
EventIdOnly,
#[doc(hidden)]
_Custom(String),
}