2020-07-30 16:14:47 +00:00
|
|
|
use super::State;
|
|
|
|
use crate::{ConduitResult, Database, Error, Ruma};
|
|
|
|
use ruma::{
|
|
|
|
api::client::{
|
|
|
|
error::ErrorKind,
|
2021-01-24 15:05:52 +00:00
|
|
|
r0::push::{
|
|
|
|
delete_pushrule, get_pushers, get_pushrule, get_pushrule_actions, get_pushrule_enabled,
|
2021-01-27 02:53:03 +00:00
|
|
|
get_pushrules_all, set_pusher, set_pushrule, set_pushrule_actions,
|
|
|
|
set_pushrule_enabled, RuleKind,
|
2021-01-24 15:05:52 +00:00
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-01-27 02:53:03 +00:00
|
|
|
events::{push_rules, EventType},
|
2021-04-05 19:25:10 +00:00
|
|
|
push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit},
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
2021-01-24 15:05:52 +00:00
|
|
|
use rocket::{delete, get, post, put};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/pushrules", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn get_pushrules_all_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<get_pushrules_all::Request>,
|
|
|
|
) -> ConduitResult<get_pushrules_all::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2020-07-30 16:14:47 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
Ok(get_pushrules_all::Response {
|
|
|
|
global: event.content.global,
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-01-24 15:05:52 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-24 15:05:52 +00:00
|
|
|
pub async fn get_pushrule_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<get_pushrule::Request<'_>>,
|
|
|
|
) -> ConduitResult<get_pushrule::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
let event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = event.content.global;
|
|
|
|
let rule = match body.kind {
|
|
|
|
RuleKind::Override => global
|
|
|
|
.override_
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.clone().into()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Underride => global
|
|
|
|
.underride
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.clone().into()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Sender => global
|
|
|
|
.sender
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.clone().into()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Room => global
|
|
|
|
.room
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.clone().into()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Content => global
|
|
|
|
.content
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.clone().into()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::_Custom(_) => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(rule) = rule {
|
|
|
|
Ok(get_pushrule::Response { rule }.into())
|
|
|
|
} else {
|
2021-01-29 19:19:56 +00:00
|
|
|
Err(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"Push rule not found.",
|
|
|
|
))
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2021-04-05 19:25:10 +00:00
|
|
|
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<req>")
|
2021-01-24 15:05:52 +00:00
|
|
|
)]
|
2021-04-05 19:25:10 +00:00
|
|
|
#[tracing::instrument(skip(db, req))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn set_pushrule_route(
|
|
|
|
db: State<'_, Database>,
|
2021-04-05 19:25:10 +00:00
|
|
|
req: Ruma<set_pushrule::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<set_pushrule::Response> {
|
2021-04-05 19:25:10 +00:00
|
|
|
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
let body = req.body;
|
2021-01-24 15:05:52 +00:00
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
match body.kind {
|
|
|
|
RuleKind::Override => {
|
2021-04-05 19:25:10 +00:00
|
|
|
global.override_.replace(
|
2021-01-24 15:05:52 +00:00
|
|
|
ConditionalPushRuleInit {
|
2021-04-05 19:25:10 +00:00
|
|
|
actions: body.actions,
|
2021-01-24 15:05:52 +00:00
|
|
|
default: false,
|
|
|
|
enabled: true,
|
2021-04-05 19:25:10 +00:00
|
|
|
rule_id: body.rule_id,
|
|
|
|
conditions: body.conditions,
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2021-04-05 19:25:10 +00:00
|
|
|
);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
RuleKind::Underride => {
|
2021-04-05 19:25:10 +00:00
|
|
|
global.underride.replace(
|
2021-01-24 15:05:52 +00:00
|
|
|
ConditionalPushRuleInit {
|
2021-04-05 19:25:10 +00:00
|
|
|
actions: body.actions,
|
2021-01-24 15:05:52 +00:00
|
|
|
default: false,
|
|
|
|
enabled: true,
|
2021-04-05 19:25:10 +00:00
|
|
|
rule_id: body.rule_id,
|
|
|
|
conditions: body.conditions,
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2021-04-05 19:25:10 +00:00
|
|
|
);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
RuleKind::Sender => {
|
2021-04-05 19:25:10 +00:00
|
|
|
global.sender.replace(
|
2021-01-24 15:05:52 +00:00
|
|
|
SimplePushRuleInit {
|
2021-04-05 19:25:10 +00:00
|
|
|
actions: body.actions,
|
2021-01-24 15:05:52 +00:00
|
|
|
default: false,
|
|
|
|
enabled: true,
|
2021-04-05 19:25:10 +00:00
|
|
|
rule_id: body.rule_id,
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2021-04-05 19:25:10 +00:00
|
|
|
);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
RuleKind::Room => {
|
2021-04-05 19:25:10 +00:00
|
|
|
global.room.replace(
|
2021-01-24 15:05:52 +00:00
|
|
|
SimplePushRuleInit {
|
2021-04-05 19:25:10 +00:00
|
|
|
actions: body.actions,
|
2021-01-24 15:05:52 +00:00
|
|
|
default: false,
|
|
|
|
enabled: true,
|
2021-04-05 19:25:10 +00:00
|
|
|
rule_id: body.rule_id,
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2021-04-05 19:25:10 +00:00
|
|
|
);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
RuleKind::Content => {
|
2021-04-05 19:25:10 +00:00
|
|
|
global.content.replace(
|
2021-01-24 15:05:52 +00:00
|
|
|
PatternedPushRuleInit {
|
2021-04-05 19:25:10 +00:00
|
|
|
actions: body.actions,
|
2021-01-24 15:05:52 +00:00
|
|
|
default: false,
|
|
|
|
enabled: true,
|
2021-04-05 19:25:10 +00:00
|
|
|
rule_id: body.rule_id,
|
|
|
|
pattern: body.pattern.unwrap_or_default(),
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
.into(),
|
2021-04-05 19:25:10 +00:00
|
|
|
);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
RuleKind::_Custom(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
None,
|
|
|
|
&sender_user,
|
|
|
|
EventType::PushRules,
|
|
|
|
&event,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(set_pushrule::Response.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
2021-01-24 15:05:52 +00:00
|
|
|
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>/actions", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-24 15:05:52 +00:00
|
|
|
pub async fn get_pushrule_actions_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<get_pushrule_actions::Request<'_>>,
|
|
|
|
) -> ConduitResult<get_pushrule_actions::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
let actions = match body.kind {
|
|
|
|
RuleKind::Override => global
|
|
|
|
.override_
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.actions.clone()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Underride => global
|
|
|
|
.underride
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.actions.clone()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Sender => global
|
|
|
|
.sender
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.actions.clone()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Room => global
|
|
|
|
.room
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.actions.clone()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Content => global
|
|
|
|
.content
|
2021-04-05 19:25:10 +00:00
|
|
|
.get(body.rule_id.as_str())
|
|
|
|
.map(|rule| rule.actions.clone()),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::_Custom(_) => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(get_pushrule_actions::Response {
|
|
|
|
actions: actions.unwrap_or_default(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/actions", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-24 15:05:52 +00:00
|
|
|
pub async fn set_pushrule_actions_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<set_pushrule_actions::Request<'_>>,
|
|
|
|
) -> ConduitResult<set_pushrule_actions::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
match body.kind {
|
|
|
|
RuleKind::Override => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
|
|
|
rule.actions = body.actions.clone();
|
|
|
|
global.override_.replace(rule);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Underride => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
|
|
|
rule.actions = body.actions.clone();
|
|
|
|
global.underride.replace(rule);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Sender => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
|
|
|
rule.actions = body.actions.clone();
|
|
|
|
global.sender.replace(rule);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Room => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
|
|
|
rule.actions = body.actions.clone();
|
|
|
|
global.room.replace(rule);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Content => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
|
|
|
rule.actions = body.actions.clone();
|
|
|
|
global.content.replace(rule);
|
2021-01-24 15:05:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::_Custom(_) => {}
|
|
|
|
};
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
None,
|
|
|
|
&sender_user,
|
|
|
|
EventType::PushRules,
|
|
|
|
&event,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(set_pushrule_actions::Response.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-24 15:05:52 +00:00
|
|
|
pub async fn get_pushrule_enabled_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<get_pushrule_enabled::Request<'_>>,
|
|
|
|
) -> ConduitResult<get_pushrule_enabled::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
let enabled = match body.kind {
|
|
|
|
RuleKind::Override => global
|
|
|
|
.override_
|
|
|
|
.iter()
|
2021-04-05 19:25:10 +00:00
|
|
|
.find(|rule| rule.rule_id == body.rule_id)
|
|
|
|
.map_or(false, |rule| rule.enabled),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Underride => global
|
|
|
|
.underride
|
|
|
|
.iter()
|
2021-04-05 19:25:10 +00:00
|
|
|
.find(|rule| rule.rule_id == body.rule_id)
|
|
|
|
.map_or(false, |rule| rule.enabled),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Sender => global
|
|
|
|
.sender
|
|
|
|
.iter()
|
2021-04-05 19:25:10 +00:00
|
|
|
.find(|rule| rule.rule_id == body.rule_id)
|
|
|
|
.map_or(false, |rule| rule.enabled),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Room => global
|
|
|
|
.room
|
|
|
|
.iter()
|
2021-04-05 19:25:10 +00:00
|
|
|
.find(|rule| rule.rule_id == body.rule_id)
|
|
|
|
.map_or(false, |rule| rule.enabled),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::Content => global
|
|
|
|
.content
|
|
|
|
.iter()
|
2021-04-05 19:25:10 +00:00
|
|
|
.find(|rule| rule.rule_id == body.rule_id)
|
|
|
|
.map_or(false, |rule| rule.enabled),
|
2021-01-24 15:05:52 +00:00
|
|
|
RuleKind::_Custom(_) => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(get_pushrule_enabled::Response { enabled }.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled", data = "<body>")
|
2020-07-30 16:14:47 +00:00
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn set_pushrule_enabled_route(
|
|
|
|
db: State<'_, Database>,
|
2021-01-24 15:05:52 +00:00
|
|
|
body: Ruma<set_pushrule_enabled::Request<'_>>,
|
2020-10-21 19:28:02 +00:00
|
|
|
) -> ConduitResult<set_pushrule_enabled::Response> {
|
2021-01-24 15:05:52 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-24 15:05:52 +00:00
|
|
|
.get::<ruma::events::push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
match body.kind {
|
|
|
|
RuleKind::Override => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.override_.remove(&rule);
|
2021-04-05 19:25:10 +00:00
|
|
|
rule.enabled = body.enabled;
|
2021-01-24 15:05:52 +00:00
|
|
|
global.override_.insert(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Underride => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.underride.remove(&rule);
|
2021-04-05 19:25:10 +00:00
|
|
|
rule.enabled = body.enabled;
|
2021-01-24 15:05:52 +00:00
|
|
|
global.underride.insert(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Sender => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.sender.remove(&rule);
|
2021-04-05 19:25:10 +00:00
|
|
|
rule.enabled = body.enabled;
|
2021-01-24 15:05:52 +00:00
|
|
|
global.sender.insert(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Room => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.room.remove(&rule);
|
2021-04-05 19:25:10 +00:00
|
|
|
rule.enabled = body.enabled;
|
2021-01-24 15:05:52 +00:00
|
|
|
global.room.insert(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Content => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.content.remove(&rule);
|
2021-04-05 19:25:10 +00:00
|
|
|
rule.enabled = body.enabled;
|
2021-01-24 15:05:52 +00:00
|
|
|
global.content.insert(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::_Custom(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
None,
|
|
|
|
&sender_user,
|
|
|
|
EventType::PushRules,
|
|
|
|
&event,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
2020-10-21 19:28:02 +00:00
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(set_pushrule_enabled::Response.into())
|
|
|
|
}
|
|
|
|
|
2021-01-24 15:05:52 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
delete("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-24 15:05:52 +00:00
|
|
|
pub async fn delete_pushrule_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<delete_pushrule::Request<'_>>,
|
|
|
|
) -> ConduitResult<delete_pushrule::Response> {
|
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
|
|
|
if body.scope != "global" {
|
|
|
|
return Err(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Scopes other than 'global' are not supported.",
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut event = db
|
|
|
|
.account_data
|
2021-01-27 02:53:03 +00:00
|
|
|
.get::<push_rules::PushRulesEvent>(None, &sender_user, EventType::PushRules)?
|
2021-01-24 15:05:52 +00:00
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::NotFound,
|
|
|
|
"PushRules event not found.",
|
|
|
|
))?;
|
|
|
|
|
|
|
|
let global = &mut event.content.global;
|
|
|
|
match body.kind {
|
|
|
|
RuleKind::Override => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.override_.remove(&rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Underride => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.underride.remove(&rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Sender => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.sender.remove(&rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Room => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.room.remove(&rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::Content => {
|
2021-04-05 19:25:10 +00:00
|
|
|
if let Some(rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
2021-01-24 15:05:52 +00:00
|
|
|
global.content.remove(&rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RuleKind::_Custom(_) => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
db.account_data.update(
|
|
|
|
None,
|
|
|
|
&sender_user,
|
|
|
|
EventType::PushRules,
|
|
|
|
&event,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
|
|
|
Ok(delete_pushrule::Response.into())
|
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/pushers", data = "<body>")
|
|
|
|
)]
|
2021-03-15 08:48:19 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-27 02:54:35 +00:00
|
|
|
pub async fn get_pushers_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<get_pushers::Request>,
|
|
|
|
) -> ConduitResult<get_pushers::Response> {
|
2021-03-16 17:00:26 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2021-01-27 02:54:35 +00:00
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(get_pushers::Response {
|
2021-03-22 13:04:11 +00:00
|
|
|
pushers: db.pusher.get_pushers(sender_user)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
.into())
|
|
|
|
}
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/pushers/set", data = "<body>")
|
|
|
|
)]
|
2021-03-15 08:48:19 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-01-27 02:54:35 +00:00
|
|
|
pub async fn set_pushers_route(
|
|
|
|
db: State<'_, Database>,
|
|
|
|
body: Ruma<set_pusher::Request>,
|
|
|
|
) -> ConduitResult<set_pusher::Response> {
|
2021-03-16 17:00:26 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2021-01-27 02:54:35 +00:00
|
|
|
let pusher = body.pusher.clone();
|
|
|
|
|
2021-03-16 17:00:26 +00:00
|
|
|
db.pusher.set_pusher(sender_user, pusher)?;
|
2021-01-27 02:54:35 +00:00
|
|
|
|
2020-10-21 19:28:02 +00:00
|
|
|
db.flush().await?;
|
|
|
|
|
2021-01-27 02:54:35 +00:00
|
|
|
Ok(set_pusher::Response::default().into())
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|