Use the ruma methods for managing rulesets
This commit is contained in:
parent
f53ecaa97d
commit
4635644e21
1 changed files with 80 additions and 236 deletions
|
@ -5,11 +5,11 @@ use ruma::{
|
||||||
push::{
|
push::{
|
||||||
delete_pushrule, get_pushers, get_pushrule, get_pushrule_actions, get_pushrule_enabled,
|
delete_pushrule, get_pushers, get_pushrule, get_pushrule_actions, get_pushrule_enabled,
|
||||||
get_pushrules_all, set_pusher, set_pushrule, set_pushrule_actions,
|
get_pushrules_all, set_pusher, set_pushrule, set_pushrule_actions,
|
||||||
set_pushrule_enabled, RuleKind, RuleScope,
|
set_pushrule_enabled, RuleScope,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
events::{push_rules::PushRulesEvent, GlobalAccountDataEventType},
|
events::{push_rules::PushRulesEvent, GlobalAccountDataEventType},
|
||||||
push::{ConditionalPushRuleInit, NewPushRule, PatternedPushRuleInit, SimplePushRuleInit},
|
push::{InsertPushRuleError, RemovePushRuleError},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// # `GET /_matrix/client/r0/pushrules`
|
/// # `GET /_matrix/client/r0/pushrules`
|
||||||
|
@ -65,30 +65,10 @@ pub async fn get_pushrule_route(
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?
|
||||||
.content;
|
.content;
|
||||||
|
|
||||||
let global = account_data.global;
|
let rule = account_data
|
||||||
let rule = match body.kind {
|
.global
|
||||||
RuleKind::Override => global
|
.get(body.kind.clone(), &body.rule_id)
|
||||||
.override_
|
.map(Into::into);
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.clone().into()),
|
|
||||||
RuleKind::Underride => global
|
|
||||||
.underride
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.clone().into()),
|
|
||||||
RuleKind::Sender => global
|
|
||||||
.sender
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.clone().into()),
|
|
||||||
RuleKind::Room => global
|
|
||||||
.room
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.clone().into()),
|
|
||||||
RuleKind::Content => global
|
|
||||||
.content
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.clone().into()),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(rule) = rule {
|
if let Some(rule) = rule {
|
||||||
Ok(get_pushrule::v3::Response { rule })
|
Ok(get_pushrule::v3::Response { rule })
|
||||||
|
@ -131,66 +111,36 @@ pub async fn set_pushrule_route(
|
||||||
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
||||||
|
|
||||||
let global = &mut account_data.content.global;
|
if let Err(error) = account_data.content.global.insert(
|
||||||
match body.rule {
|
body.rule.clone(),
|
||||||
NewPushRule::Override(rule) => {
|
body.after.as_deref(),
|
||||||
global.override_.replace(
|
body.before.as_deref(),
|
||||||
ConditionalPushRuleInit {
|
) {
|
||||||
actions: rule.actions,
|
let err = match error {
|
||||||
default: false,
|
InsertPushRuleError::ServerDefaultRuleId => Error::BadRequest(
|
||||||
enabled: true,
|
ErrorKind::InvalidParam,
|
||||||
rule_id: rule.rule_id,
|
"Rule IDs starting with a dot are reserved for server-default rules.",
|
||||||
conditions: rule.conditions,
|
),
|
||||||
}
|
InsertPushRuleError::InvalidRuleId => Error::BadRequest(
|
||||||
.into(),
|
ErrorKind::InvalidParam,
|
||||||
);
|
"Rule ID containing invalid characters.",
|
||||||
}
|
),
|
||||||
NewPushRule::Underride(rule) => {
|
InsertPushRuleError::RelativeToServerDefaultRule => Error::BadRequest(
|
||||||
global.underride.replace(
|
ErrorKind::InvalidParam,
|
||||||
ConditionalPushRuleInit {
|
"Can't place a push rule relatively to a server-default rule.",
|
||||||
actions: rule.actions,
|
),
|
||||||
default: false,
|
InsertPushRuleError::UnknownRuleId => Error::BadRequest(
|
||||||
enabled: true,
|
ErrorKind::NotFound,
|
||||||
rule_id: rule.rule_id,
|
"The before or after rule could not be found.",
|
||||||
conditions: rule.conditions,
|
),
|
||||||
}
|
InsertPushRuleError::BeforeHigherThanAfter => Error::BadRequest(
|
||||||
.into(),
|
ErrorKind::InvalidParam,
|
||||||
);
|
"The before rule has a higher priority than the after rule.",
|
||||||
}
|
),
|
||||||
NewPushRule::Sender(rule) => {
|
_ => Error::BadRequest(ErrorKind::InvalidParam, "Invalid data."),
|
||||||
global.sender.replace(
|
};
|
||||||
SimplePushRuleInit {
|
|
||||||
actions: rule.actions,
|
return Err(err);
|
||||||
default: false,
|
|
||||||
enabled: true,
|
|
||||||
rule_id: rule.rule_id,
|
|
||||||
}
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
NewPushRule::Room(rule) => {
|
|
||||||
global.room.replace(
|
|
||||||
SimplePushRuleInit {
|
|
||||||
actions: rule.actions,
|
|
||||||
default: false,
|
|
||||||
enabled: true,
|
|
||||||
rule_id: rule.rule_id,
|
|
||||||
}
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
NewPushRule::Content(rule) => {
|
|
||||||
global.content.replace(
|
|
||||||
PatternedPushRuleInit {
|
|
||||||
actions: rule.actions,
|
|
||||||
default: false,
|
|
||||||
enabled: true,
|
|
||||||
rule_id: rule.rule_id,
|
|
||||||
pattern: rule.pattern,
|
|
||||||
}
|
|
||||||
.into(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
services().account_data.update(
|
services().account_data.update(
|
||||||
|
@ -235,29 +185,9 @@ pub async fn get_pushrule_actions_route(
|
||||||
.content;
|
.content;
|
||||||
|
|
||||||
let global = account_data.global;
|
let global = account_data.global;
|
||||||
let actions = match body.kind {
|
let actions = global
|
||||||
RuleKind::Override => global
|
.get(body.kind.clone(), &body.rule_id)
|
||||||
.override_
|
.map(|rule| rule.actions().to_owned());
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.actions.clone()),
|
|
||||||
RuleKind::Underride => global
|
|
||||||
.underride
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.actions.clone()),
|
|
||||||
RuleKind::Sender => global
|
|
||||||
.sender
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.actions.clone()),
|
|
||||||
RuleKind::Room => global
|
|
||||||
.room
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.actions.clone()),
|
|
||||||
RuleKind::Content => global
|
|
||||||
.content
|
|
||||||
.get(body.rule_id.as_str())
|
|
||||||
.map(|rule| rule.actions.clone()),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(get_pushrule_actions::v3::Response {
|
Ok(get_pushrule_actions::v3::Response {
|
||||||
actions: actions.unwrap_or_default(),
|
actions: actions.unwrap_or_default(),
|
||||||
|
@ -294,40 +224,17 @@ pub async fn set_pushrule_actions_route(
|
||||||
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
||||||
|
|
||||||
let global = &mut account_data.content.global;
|
if account_data
|
||||||
match body.kind {
|
.content
|
||||||
RuleKind::Override => {
|
.global
|
||||||
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
.set_actions(body.kind.clone(), &body.rule_id, body.actions.clone())
|
||||||
rule.actions = body.actions.clone();
|
.is_err()
|
||||||
global.override_.replace(rule);
|
{
|
||||||
|
return Err(Error::BadRequest(
|
||||||
|
ErrorKind::NotFound,
|
||||||
|
"Push rule not found.",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
RuleKind::Underride => {
|
|
||||||
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
|
||||||
rule.actions = body.actions.clone();
|
|
||||||
global.underride.replace(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Sender => {
|
|
||||||
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
|
||||||
rule.actions = body.actions.clone();
|
|
||||||
global.sender.replace(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Room => {
|
|
||||||
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
|
||||||
rule.actions = body.actions.clone();
|
|
||||||
global.room.replace(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Content => {
|
|
||||||
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
|
||||||
rule.actions = body.actions.clone();
|
|
||||||
global.content.replace(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
services().account_data.update(
|
services().account_data.update(
|
||||||
None,
|
None,
|
||||||
|
@ -370,34 +277,10 @@ pub async fn get_pushrule_enabled_route(
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
||||||
|
|
||||||
let global = account_data.content.global;
|
let global = account_data.content.global;
|
||||||
let enabled = match body.kind {
|
let enabled = global
|
||||||
RuleKind::Override => global
|
.get(body.kind.clone(), &body.rule_id)
|
||||||
.override_
|
.map(|r| r.enabled())
|
||||||
.iter()
|
.unwrap_or_default();
|
||||||
.find(|rule| rule.rule_id == body.rule_id)
|
|
||||||
.map_or(false, |rule| rule.enabled),
|
|
||||||
RuleKind::Underride => global
|
|
||||||
.underride
|
|
||||||
.iter()
|
|
||||||
.find(|rule| rule.rule_id == body.rule_id)
|
|
||||||
.map_or(false, |rule| rule.enabled),
|
|
||||||
RuleKind::Sender => global
|
|
||||||
.sender
|
|
||||||
.iter()
|
|
||||||
.find(|rule| rule.rule_id == body.rule_id)
|
|
||||||
.map_or(false, |rule| rule.enabled),
|
|
||||||
RuleKind::Room => global
|
|
||||||
.room
|
|
||||||
.iter()
|
|
||||||
.find(|rule| rule.rule_id == body.rule_id)
|
|
||||||
.map_or(false, |rule| rule.enabled),
|
|
||||||
RuleKind::Content => global
|
|
||||||
.content
|
|
||||||
.iter()
|
|
||||||
.find(|rule| rule.rule_id == body.rule_id)
|
|
||||||
.map_or(false, |rule| rule.enabled),
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(get_pushrule_enabled::v3::Response { enabled })
|
Ok(get_pushrule_enabled::v3::Response { enabled })
|
||||||
}
|
}
|
||||||
|
@ -432,44 +315,16 @@ pub async fn set_pushrule_enabled_route(
|
||||||
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
||||||
|
|
||||||
let global = &mut account_data.content.global;
|
if account_data
|
||||||
match body.kind {
|
.content
|
||||||
RuleKind::Override => {
|
.global
|
||||||
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
.set_enabled(body.kind.clone(), &body.rule_id, body.enabled)
|
||||||
global.override_.remove(&rule);
|
.is_err()
|
||||||
rule.enabled = body.enabled;
|
{
|
||||||
global.override_.insert(rule);
|
return Err(Error::BadRequest(
|
||||||
}
|
ErrorKind::NotFound,
|
||||||
}
|
"Push rule not found.",
|
||||||
RuleKind::Underride => {
|
));
|
||||||
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.underride.remove(&rule);
|
|
||||||
rule.enabled = body.enabled;
|
|
||||||
global.underride.insert(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Sender => {
|
|
||||||
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.sender.remove(&rule);
|
|
||||||
rule.enabled = body.enabled;
|
|
||||||
global.sender.insert(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Room => {
|
|
||||||
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.room.remove(&rule);
|
|
||||||
rule.enabled = body.enabled;
|
|
||||||
global.room.insert(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Content => {
|
|
||||||
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.content.remove(&rule);
|
|
||||||
rule.enabled = body.enabled;
|
|
||||||
global.content.insert(rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
services().account_data.update(
|
services().account_data.update(
|
||||||
|
@ -512,34 +367,23 @@ pub async fn delete_pushrule_route(
|
||||||
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
let mut account_data = serde_json::from_str::<PushRulesEvent>(event.get())
|
||||||
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
.map_err(|_| Error::bad_database("Invalid account data event in db."))?;
|
||||||
|
|
||||||
let global = &mut account_data.content.global;
|
if let Err(error) = account_data
|
||||||
match body.kind {
|
.content
|
||||||
RuleKind::Override => {
|
.global
|
||||||
if let Some(rule) = global.override_.get(body.rule_id.as_str()).cloned() {
|
.remove(body.kind.clone(), &body.rule_id)
|
||||||
global.override_.remove(&rule);
|
{
|
||||||
|
let err = match error {
|
||||||
|
RemovePushRuleError::ServerDefault => Error::BadRequest(
|
||||||
|
ErrorKind::InvalidParam,
|
||||||
|
"Cannot delete a server-default pushrule.",
|
||||||
|
),
|
||||||
|
RemovePushRuleError::NotFound => {
|
||||||
|
Error::BadRequest(ErrorKind::NotFound, "Push rule not found.")
|
||||||
}
|
}
|
||||||
}
|
_ => Error::BadRequest(ErrorKind::InvalidParam, "Invalid data."),
|
||||||
RuleKind::Underride => {
|
};
|
||||||
if let Some(rule) = global.underride.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.underride.remove(&rule);
|
return Err(err);
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Sender => {
|
|
||||||
if let Some(rule) = global.sender.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.sender.remove(&rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Room => {
|
|
||||||
if let Some(rule) = global.room.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.room.remove(&rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
RuleKind::Content => {
|
|
||||||
if let Some(rule) = global.content.get(body.rule_id.as_str()).cloned() {
|
|
||||||
global.content.remove(&rule);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
services().account_data.update(
|
services().account_data.update(
|
||||||
|
|
Loading…
Add table
Reference in a new issue