Upgrade ruma

… and refactor push rule code along the way.
This commit is contained in:
Jonas Platte 2021-04-05 21:25:10 +02:00
parent db7044a950
commit fe744c856f
No known key found for this signature in database
GPG key ID: CC154DE0E30B7C67
13 changed files with 261 additions and 862 deletions

View file

@ -21,7 +21,7 @@ use ruma::{
},
EventType,
},
RoomAliasId, RoomId, RoomVersionId, UserId,
push, RoomAliasId, RoomId, RoomVersionId, UserId,
};
use register::RegistrationKind;
@ -181,7 +181,7 @@ pub async fn register_route(
EventType::PushRules,
&ruma::events::push_rules::PushRulesEvent {
content: ruma::events::push_rules::PushRulesEventContent {
global: crate::push_rules::default_pushrules(&user_id),
global: push::Ruleset::server_default(&user_id),
},
},
&db.globals,

View file

@ -46,7 +46,7 @@ pub async fn create_content_route(
db.flush().await?;
Ok(create_content::Response {
content_uri: mxc,
content_uri: mxc.try_into().expect("Invalid mxc:// URI"),
blurhash: None,
}
.into())

View file

@ -10,10 +10,7 @@ use ruma::{
},
},
events::{push_rules, EventType},
push::{
ConditionalPushRuleInit, ContentPushRule, OverridePushRule, PatternedPushRuleInit,
RoomPushRule, SenderPushRule, SimplePushRuleInit, UnderridePushRule,
},
push::{ConditionalPushRuleInit, PatternedPushRuleInit, SimplePushRuleInit},
};
#[cfg(feature = "conduit_bin")]
@ -67,29 +64,24 @@ pub async fn get_pushrule_route(
let rule = match body.kind {
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.clone().into()),
.get(body.rule_id.as_str())
.map(|rule| rule.clone().into()),
RuleKind::_Custom(_) => None,
};
@ -105,14 +97,15 @@ pub async fn get_pushrule_route(
#[cfg_attr(
feature = "conduit_bin",
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<body>")
put("/_matrix/client/r0/pushrules/<_>/<_>/<_>", data = "<req>")
)]
#[tracing::instrument(skip(db, body))]
#[tracing::instrument(skip(db, req))]
pub async fn set_pushrule_route(
db: State<'_, Database>,
body: Ruma<set_pushrule::Request<'_>>,
req: Ruma<set_pushrule::Request<'_>>,
) -> ConduitResult<set_pushrule::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_user = req.sender_user.as_ref().expect("user is authenticated");
let body = req.body;
if body.scope != "global" {
return Err(Error::BadRequest(
@ -132,107 +125,62 @@ pub async fn set_pushrule_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.override_.remove(&rule);
}
global.override_.insert(OverridePushRule(
global.override_.replace(
ConditionalPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
conditions: body.conditions.clone(),
rule_id: body.rule_id,
conditions: body.conditions,
}
.into(),
));
);
}
RuleKind::Underride => {
if let Some(rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.underride.remove(&rule);
}
global.underride.insert(UnderridePushRule(
global.underride.replace(
ConditionalPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
conditions: body.conditions.clone(),
rule_id: body.rule_id,
conditions: body.conditions,
}
.into(),
));
);
}
RuleKind::Sender => {
if let Some(rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.sender.remove(&rule);
}
global.sender.insert(SenderPushRule(
global.sender.replace(
SimplePushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
rule_id: body.rule_id,
}
.into(),
));
);
}
RuleKind::Room => {
if let Some(rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.room.remove(&rule);
}
global.room.insert(RoomPushRule(
global.room.replace(
SimplePushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
rule_id: body.rule_id,
}
.into(),
));
);
}
RuleKind::Content => {
if let Some(rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.content.remove(&rule);
}
global.content.insert(ContentPushRule(
global.content.replace(
PatternedPushRuleInit {
actions: body.actions.clone(),
actions: body.actions,
default: false,
enabled: true,
rule_id: body.rule_id.clone(),
pattern: body.pattern.clone().unwrap_or_default(),
rule_id: body.rule_id,
pattern: body.pattern.unwrap_or_default(),
}
.into(),
));
);
}
RuleKind::_Custom(_) => {}
}
@ -280,29 +228,24 @@ pub async fn get_pushrule_actions_route(
let actions = match body.kind {
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map(|rule| rule.0.actions.clone()),
.get(body.rule_id.as_str())
.map(|rule| rule.actions.clone()),
RuleKind::_Custom(_) => None,
};
@ -343,63 +286,33 @@ pub async fn set_pushrule_actions_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(mut rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.override_.remove(&rule);
rule.0.actions = body.actions.clone();
global.override_.insert(rule);
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.override_.replace(rule);
}
}
RuleKind::Underride => {
if let Some(mut rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.underride.remove(&rule);
rule.0.actions = body.actions.clone();
global.underride.insert(rule);
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
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.sender.remove(&rule);
rule.0.actions = body.actions.clone();
global.sender.insert(rule);
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
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.room.remove(&rule);
rule.0.actions = body.actions.clone();
global.room.insert(rule);
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
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
global.content.remove(&rule);
rule.0.actions = body.actions.clone();
global.content.insert(rule);
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
rule.actions = body.actions.clone();
global.content.replace(rule);
}
}
RuleKind::_Custom(_) => {}
@ -449,28 +362,28 @@ pub async fn get_pushrule_enabled_route(
RuleKind::Override => global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Underride => global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Sender => global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Room => global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::Content => global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.map_or(false, |rule| rule.0.enabled),
.find(|rule| rule.rule_id == body.rule_id)
.map_or(false, |rule| rule.enabled),
RuleKind::_Custom(_) => false,
};
@ -508,62 +421,37 @@ pub async fn set_pushrule_enabled_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(mut rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.override_.get(body.rule_id.as_str()).cloned() {
global.override_.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.override_.insert(rule);
}
}
RuleKind::Underride => {
if let Some(mut rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.underride.get(body.rule_id.as_str()).cloned() {
global.underride.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.underride.insert(rule);
}
}
RuleKind::Sender => {
if let Some(mut rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.sender.get(body.rule_id.as_str()).cloned() {
global.sender.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.sender.insert(rule);
}
}
RuleKind::Room => {
if let Some(mut rule) = global
.room
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.room.get(body.rule_id.as_str()).cloned() {
global.room.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.room.insert(rule);
}
}
RuleKind::Content => {
if let Some(mut rule) = global
.content
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(mut rule) = global.content.get(body.rule_id.as_str()).cloned() {
global.content.remove(&rule);
rule.0.enabled = body.enabled;
rule.enabled = body.enabled;
global.content.insert(rule);
}
}
@ -612,52 +500,27 @@ pub async fn delete_pushrule_route(
let global = &mut event.content.global;
match body.kind {
RuleKind::Override => {
if let Some(rule) = global
.override_
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.override_.get(body.rule_id.as_str()).cloned() {
global.override_.remove(&rule);
}
}
RuleKind::Underride => {
if let Some(rule) = global
.underride
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.underride.get(body.rule_id.as_str()).cloned() {
global.underride.remove(&rule);
}
}
RuleKind::Sender => {
if let Some(rule) = global
.sender
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
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
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
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
.iter()
.find(|rule| rule.0.rule_id == body.rule_id)
.cloned()
{
if let Some(rule) = global.content.get(body.rule_id.as_str()).cloned() {
global.content.remove(&rule);
}
}

View file

@ -3,10 +3,7 @@ use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Result, Ruma};
use ruma::{
api::client::{
error::ErrorKind,
r0::state::{
get_state_events, get_state_events_for_empty_key, get_state_events_for_key,
send_state_event_for_empty_key, send_state_event_for_key,
},
r0::state::{get_state_events, get_state_events_for_key, send_state_event},
},
events::{
room::history_visibility::{HistoryVisibility, HistoryVisibilityEventContent},
@ -25,8 +22,8 @@ use rocket::{get, put};
#[tracing::instrument(skip(db, body))]
pub async fn send_state_event_for_key_route(
db: State<'_, Database>,
body: Ruma<send_state_event_for_key::Request<'_>>,
) -> ConduitResult<send_state_event_for_key::Response> {
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let content = serde_json::from_str::<serde_json::Value>(
@ -49,7 +46,7 @@ pub async fn send_state_event_for_key_route(
db.flush().await?;
Ok(send_state_event_for_key::Response { event_id }.into())
Ok(send_state_event::Response { event_id }.into())
}
#[cfg_attr(
@ -59,8 +56,8 @@ pub async fn send_state_event_for_key_route(
#[tracing::instrument(skip(db, body))]
pub async fn send_state_event_for_empty_key_route(
db: State<'_, Database>,
body: Ruma<send_state_event_for_empty_key::Request<'_>>,
) -> ConduitResult<send_state_event_for_empty_key::Response> {
body: Ruma<send_state_event::Request<'_>>,
) -> ConduitResult<send_state_event::Response> {
// This just calls send_state_event_for_key_route
let Ruma {
body,
@ -81,7 +78,7 @@ pub async fn send_state_event_for_empty_key_route(
&db,
sender_user
.as_ref()
.expect("no user for send state empty key rout"),
.expect("no user for send state empty key route"),
&body.content,
json,
&body.room_id,
@ -91,7 +88,7 @@ pub async fn send_state_event_for_empty_key_route(
db.flush().await?;
Ok(send_state_event_for_empty_key::Response { event_id }.into())
Ok(send_state_event::Response { event_id }.into())
}
#[cfg_attr(
@ -199,8 +196,8 @@ pub async fn get_state_events_for_key_route(
#[tracing::instrument(skip(db, body))]
pub async fn get_state_events_for_empty_key_route(
db: State<'_, Database>,
body: Ruma<get_state_events_for_empty_key::Request<'_>>,
) -> ConduitResult<get_state_events_for_empty_key::Response> {
body: Ruma<get_state_events_for_key::Request<'_>>,
) -> ConduitResult<get_state_events_for_key::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
#[allow(clippy::blocks_in_if_conditions)]
@ -236,7 +233,7 @@ pub async fn get_state_events_for_empty_key_route(
"State event not found.",
))?;
Ok(get_state_events_for_empty_key::Response {
Ok(get_state_events_for_key::Response {
content: serde_json::value::to_raw_value(&event.content)
.map_err(|_| Error::bad_database("Invalid event content in database"))?,
}