use std::{error::Error, fmt};
pub use ruma_common::push::RuleKind;
use ruma_common::{
push::{
Action, AnyPushRule, AnyPushRuleRef, ConditionalPushRule, ConditionalPushRuleInit,
HttpPusherData, PatternedPushRule, PatternedPushRuleInit, PushCondition, SimplePushRule,
SimplePushRuleInit,
},
serde::JsonObject,
};
use serde::{Deserialize, Serialize};
pub mod delete_pushrule;
pub mod get_notifications;
pub mod get_pushers;
pub mod get_pushrule;
pub mod get_pushrule_actions;
pub mod get_pushrule_enabled;
pub mod get_pushrules_all;
pub mod get_pushrules_global_scope;
mod pusher_serde;
pub mod set_pusher;
pub mod set_pushrule;
pub mod set_pushrule_actions;
pub mod set_pushrule_enabled;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PushRule {
pub actions: Vec<Action>,
pub default: bool,
pub enabled: bool,
pub rule_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub conditions: Option<Vec<PushCondition>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pattern: Option<String>,
}
impl<T> From<SimplePushRule<T>> for PushRule
where
T: Into<String>,
{
fn from(push_rule: SimplePushRule<T>) -> Self {
let SimplePushRule { actions, default, enabled, rule_id, .. } = push_rule;
let rule_id = rule_id.into();
Self { actions, default, enabled, rule_id, conditions: None, pattern: None }
}
}
impl From<PatternedPushRule> for PushRule {
fn from(push_rule: PatternedPushRule) -> Self {
let PatternedPushRule { actions, default, enabled, rule_id, pattern, .. } = push_rule;
Self { actions, default, enabled, rule_id, conditions: None, pattern: Some(pattern) }
}
}
impl From<ConditionalPushRule> for PushRule {
fn from(push_rule: ConditionalPushRule) -> Self {
let ConditionalPushRule { actions, default, enabled, rule_id, conditions, .. } = push_rule;
Self { actions, default, enabled, rule_id, conditions: Some(conditions), pattern: None }
}
}
impl<T> From<SimplePushRuleInit<T>> for PushRule
where
T: Into<String>,
{
fn from(init: SimplePushRuleInit<T>) -> Self {
let SimplePushRuleInit { actions, default, enabled, rule_id } = init;
let rule_id = rule_id.into();
Self { actions, default, enabled, rule_id, pattern: None, conditions: None }
}
}
impl From<ConditionalPushRuleInit> for PushRule {
fn from(init: ConditionalPushRuleInit) -> Self {
let ConditionalPushRuleInit { actions, default, enabled, rule_id, conditions } = init;
Self { actions, default, enabled, rule_id, pattern: None, conditions: Some(conditions) }
}
}
impl From<PatternedPushRuleInit> for PushRule {
fn from(init: PatternedPushRuleInit) -> Self {
let PatternedPushRuleInit { actions, default, enabled, rule_id, pattern } = init;
Self { actions, default, enabled, rule_id, pattern: Some(pattern), conditions: None }
}
}
impl From<AnyPushRule> for PushRule {
fn from(push_rule: AnyPushRule) -> Self {
#[allow(unreachable_patterns)]
match push_rule {
AnyPushRule::Override(r) => r.into(),
AnyPushRule::Content(r) => r.into(),
AnyPushRule::Room(r) => r.into(),
AnyPushRule::Sender(r) => r.into(),
AnyPushRule::Underride(r) => r.into(),
_ => unreachable!(),
}
}
}
impl<'a> From<AnyPushRuleRef<'a>> for PushRule {
fn from(push_rule: AnyPushRuleRef<'a>) -> Self {
push_rule.to_owned().into()
}
}
impl<T> TryFrom<PushRule> for SimplePushRule<T>
where
T: TryFrom<String>,
{
type Error = <T as TryFrom<String>>::Error;
fn try_from(push_rule: PushRule) -> Result<Self, Self::Error> {
let PushRule { actions, default, enabled, rule_id, .. } = push_rule;
let rule_id = T::try_from(rule_id)?;
Ok(SimplePushRuleInit { actions, default, enabled, rule_id }.into())
}
}
#[derive(Debug)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct MissingPatternError;
impl fmt::Display for MissingPatternError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Push rule does not have a pattern.")
}
}
impl Error for MissingPatternError {}
impl TryFrom<PushRule> for PatternedPushRule {
type Error = MissingPatternError;
fn try_from(push_rule: PushRule) -> Result<Self, Self::Error> {
if let PushRule { actions, default, enabled, rule_id, pattern: Some(pattern), .. } =
push_rule
{
Ok(PatternedPushRuleInit { actions, default, enabled, rule_id, pattern }.into())
} else {
Err(MissingPatternError)
}
}
}
impl From<PushRule> for ConditionalPushRule {
fn from(push_rule: PushRule) -> Self {
let PushRule { actions, default, enabled, rule_id, conditions, .. } = push_rule;
ConditionalPushRuleInit {
actions,
default,
enabled,
rule_id,
conditions: conditions.unwrap_or_default(),
}
.into()
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum PusherKind {
Http(HttpPusherData),
Email(EmailPusherData),
#[doc(hidden)]
_Custom(CustomPusherData),
}
#[derive(Clone, Debug, Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct Pusher {
#[serde(flatten)]
pub ids: PusherIds,
#[serde(flatten)]
pub kind: PusherKind,
pub app_display_name: String,
pub device_display_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile_tag: Option<String>,
pub lang: String,
}
#[derive(Debug)]
#[allow(clippy::exhaustive_structs)]
pub struct PusherInit {
pub ids: PusherIds,
pub kind: PusherKind,
pub app_display_name: String,
pub device_display_name: String,
pub profile_tag: Option<String>,
pub lang: String,
}
impl From<PusherInit> for Pusher {
fn from(init: PusherInit) -> Self {
let PusherInit { ids, kind, app_display_name, device_display_name, profile_tag, lang } =
init;
Self { ids, kind, app_display_name, device_display_name, profile_tag, lang }
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct PusherIds {
pub pushkey: String,
pub app_id: String,
}
impl PusherIds {
pub fn new(pushkey: String, app_id: String) -> Self {
Self { pushkey, app_id }
}
}
#[derive(Clone, Debug, Default)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct EmailPusherData;
impl EmailPusherData {
pub fn new() -> Self {
Self::default()
}
}
#[doc(hidden)]
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct CustomPusherData {
kind: String,
data: JsonObject,
}