From 6e84d317b2f186130943c0cd6ccf21b416e84fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Thu, 22 Apr 2021 11:26:20 +0200 Subject: [PATCH] improvement: update ruma --- Cargo.lock | 60 +++++++++++++++---------------- Cargo.toml | 4 +-- src/client_server/message.rs | 4 +-- src/client_server/session.rs | 7 +++- src/client_server/state.rs | 70 ++++++++++++++---------------------- src/database/pusher.rs | 22 ++++++------ src/ruma_wrapper.rs | 7 ++-- src/server_server.rs | 57 +++++++++++++---------------- 8 files changed, 106 insertions(+), 125 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 84001dcc..c13e7d6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1545,8 +1545,8 @@ dependencies = [ [[package]] name = "ruma" -version = "0.0.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.0.3" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "assign", "js_int", @@ -1565,8 +1565,8 @@ dependencies = [ [[package]] name = "ruma-api" -version = "0.17.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.17.0-alpha.4" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "http", "percent-encoding", @@ -1580,8 +1580,8 @@ dependencies = [ [[package]] name = "ruma-api-macros" -version = "0.17.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.17.0-alpha.4" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1591,8 +1591,8 @@ dependencies = [ [[package]] name = "ruma-appservice-api" -version = "0.2.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.2.0-alpha.3" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "ruma-api", "ruma-common", @@ -1605,8 +1605,8 @@ dependencies = [ [[package]] name = "ruma-client-api" -version = "0.10.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.10.0-alpha.3" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "assign", "http", @@ -1624,8 +1624,8 @@ dependencies = [ [[package]] name = "ruma-common" -version = "0.4.0" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.5.0" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "indexmap", "js_int", @@ -1640,8 +1640,8 @@ dependencies = [ [[package]] name = "ruma-events" -version = "0.22.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.22.0-alpha.3" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "js_int", "ruma-common", @@ -1654,8 +1654,8 @@ dependencies = [ [[package]] name = "ruma-events-macros" -version = "0.22.0-alpha.2" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.22.0-alpha.3" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1665,8 +1665,8 @@ dependencies = [ [[package]] name = "ruma-federation-api" -version = "0.1.0-alpha.1" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.1.0-alpha.2" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "js_int", "ruma-api", @@ -1681,7 +1681,7 @@ dependencies = [ [[package]] name = "ruma-identifiers" version = "0.19.0" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "paste", "rand", @@ -1695,7 +1695,7 @@ dependencies = [ [[package]] name = "ruma-identifiers-macros" version = "0.19.0" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "proc-macro2", "quote", @@ -1705,13 +1705,13 @@ dependencies = [ [[package]] name = "ruma-identifiers-validation" -version = "0.2.3" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.3.0" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" [[package]] name = "ruma-identity-service-api" -version = "0.0.1" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.1.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "ruma-api", "ruma-common", @@ -1723,8 +1723,8 @@ dependencies = [ [[package]] name = "ruma-push-gateway-api" -version = "0.0.1" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +version = "0.1.0-alpha.1" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "js_int", "ruma-api", @@ -1739,7 +1739,7 @@ dependencies = [ [[package]] name = "ruma-serde" version = "0.3.1" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "form_urlencoded", "itoa", @@ -1752,7 +1752,7 @@ dependencies = [ [[package]] name = "ruma-serde-macros" version = "0.3.1" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1763,7 +1763,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.7.0" -source = "git+https://github.com/ruma/ruma?rev=c1693569f15920e408aa6a26b7f3cc7fc6693a63#c1693569f15920e408aa6a26b7f3cc7fc6693a63" +source = "git+https://github.com/ruma/ruma?rev=4f16b9357c15d649075393a723f23cf560251754#4f16b9357c15d649075393a723f23cf560251754" dependencies = [ "base64 0.13.0", "ring", @@ -2053,7 +2053,7 @@ checksum = "3015a7d0a5fd5105c91c3710d42f9ccf0abfb287d62206484dcc67f9569a6483" [[package]] name = "state-res" version = "0.1.0" -source = "git+https://github.com/ruma/state-res?rev=4516d73e8c7495330619bfb5b42c3bbf704293d8#4516d73e8c7495330619bfb5b42c3bbf704293d8" +source = "git+https://github.com/ruma/state-res?rev=aa53d07f51ffb7258f5c1e499bddffd4c630f7df#aa53d07f51ffb7258f5c1e499bddffd4c630f7df" dependencies = [ "itertools 0.10.0", "log", diff --git a/Cargo.toml b/Cargo.toml index fa495dfb..4cf2ed4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,12 @@ rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "93e62c86e #rocket = { git = "https://github.com/timokoesters/Rocket.git", branch = "empty_parameters", default-features = false, features = ["tls"] } # Used for matrix spec type definitions and helpers -ruma = { git = "https://github.com/ruma/ruma", rev = "c1693569f15920e408aa6a26b7f3cc7fc6693a63", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] } +ruma = { git = "https://github.com/ruma/ruma", rev = "4f16b9357c15d649075393a723f23cf560251754", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { git = "https://github.com/timokoesters/ruma", rev = "220d5b4a76b3b781f7f8297fbe6b14473b04214b", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] } #ruma = { path = "../ruma/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "unstable-pre-spec", "unstable-exhaustive-types"] } # Used when doing state resolution -state-res = { git = "https://github.com/ruma/state-res", rev = "4516d73e8c7495330619bfb5b42c3bbf704293d8", features = ["unstable-pre-spec"] } +state-res = { git = "https://github.com/ruma/state-res", rev = "aa53d07f51ffb7258f5c1e499bddffd4c630f7df", features = ["unstable-pre-spec"] } #state-res = { path = "../state-res", features = ["unstable-pre-spec"] } # Used for long polling and federation sender, should be the same as rocket::tokio diff --git a/src/client_server/message.rs b/src/client_server/message.rs index 04f27def..ecd2665f 100644 --- a/src/client_server/message.rs +++ b/src/client_server/message.rs @@ -5,7 +5,7 @@ use ruma::{ error::ErrorKind, r0::message::{get_message_events, send_message_event}, }, - events::EventContent, + events::EventType, EventId, }; use std::{ @@ -55,7 +55,7 @@ pub async fn send_message_event_route( let event_id = db.rooms.build_and_append_pdu( PduBuilder { - event_type: body.content.event_type().into(), + event_type: EventType::from(&body.event_type), content: serde_json::from_str( body.json_body .as_ref() diff --git a/src/client_server/session.rs b/src/client_server/session.rs index cb6442d1..3718003a 100644 --- a/src/client_server/session.rs +++ b/src/client_server/session.rs @@ -26,7 +26,12 @@ use rocket::{get, post}; #[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/login"))] #[tracing::instrument] pub async fn get_login_types_route() -> ConduitResult { - Ok(get_login_types::Response::new(vec![get_login_types::LoginType::Password]).into()) + Ok( + get_login_types::Response::new(vec![get_login_types::LoginType::Password( + Default::default(), + )]) + .into(), + ) } /// # `POST /_matrix/client/r0/login` diff --git a/src/client_server/state.rs b/src/client_server/state.rs index 88cce031..68e0c7f1 100644 --- a/src/client_server/state.rs +++ b/src/client_server/state.rs @@ -6,9 +6,13 @@ use ruma::{ r0::state::{get_state_events, get_state_events_for_key, send_state_event}, }, events::{ - room::history_visibility::{HistoryVisibility, HistoryVisibilityEventContent}, - AnyStateEventContent, EventContent, EventType, + room::{ + canonical_alias::CanonicalAliasEventContent, + history_visibility::{HistoryVisibility, HistoryVisibilityEventContent}, + }, + AnyStateEventContent, EventType, }, + serde::Raw, EventId, RoomId, UserId, }; @@ -26,21 +30,13 @@ pub async fn send_state_event_for_key_route( ) -> ConduitResult { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - let content = serde_json::from_str::( - body.json_body - .as_ref() - .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? - .get(), - ) - .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; - let event_id = send_state_event_for_key_helper( &db, sender_user, - &body.content, - content, &body.room_id, - Some(body.state_key.to_owned()), + EventType::from(&body.event_type), + &body.body.body, // Yes, I hate it too + body.state_key.to_owned(), ) .await?; @@ -58,31 +54,15 @@ pub async fn send_state_event_for_empty_key_route( db: State<'_, Database>, body: Ruma>, ) -> ConduitResult { - // This just calls send_state_event_for_key_route - let Ruma { - body, - sender_user, - json_body, - .. - } = body; - - let json = serde_json::from_str::( - json_body - .as_ref() - .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? - .get(), - ) - .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; + let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event_id = send_state_event_for_key_helper( &db, - sender_user - .as_ref() - .expect("no user for send state empty key route"), - &body.content, - json, + sender_user, &body.room_id, - Some("".into()), + EventType::from(&body.event_type), + &body.body.body, + body.state_key.to_owned(), ) .await?; @@ -183,7 +163,7 @@ pub async fn get_state_events_for_key_route( ))?; Ok(get_state_events_for_key::Response { - content: serde_json::value::to_raw_value(&event.content) + content: serde_json::from_value(event.content) .map_err(|_| Error::bad_database("Invalid event content in database"))?, } .into()) @@ -234,7 +214,7 @@ pub async fn get_state_events_for_empty_key_route( ))?; Ok(get_state_events_for_key::Response { - content: serde_json::value::to_raw_value(&event.content) + content: serde_json::from_value(event.content) .map_err(|_| Error::bad_database("Invalid event content in database"))?, } .into()) @@ -243,17 +223,19 @@ pub async fn get_state_events_for_empty_key_route( pub async fn send_state_event_for_key_helper( db: &Database, sender: &UserId, - content: &AnyStateEventContent, - json: serde_json::Value, room_id: &RoomId, - state_key: Option, + event_type: EventType, + json: &Raw, + state_key: String, ) -> Result { let sender_user = sender; - if let AnyStateEventContent::RoomCanonicalAlias(canonical_alias) = content { + if let Ok(canonical_alias) = + serde_json::from_str::(json.json().get()) + { let mut aliases = canonical_alias.alt_aliases.clone(); - if let Some(alias) = canonical_alias.alias.clone() { + if let Some(alias) = canonical_alias.alias { aliases.push(alias); } @@ -276,10 +258,10 @@ pub async fn send_state_event_for_key_helper( let event_id = db.rooms.build_and_append_pdu( PduBuilder { - event_type: content.event_type().into(), - content: json, + event_type, + content: serde_json::from_str(json.json().get()).expect("content is valid json"), unsigned: None, - state_key, + state_key: Some(state_key), redacts: None, }, &sender_user, diff --git a/src/database/pusher.rs b/src/database/pusher.rs index 40b829fb..28da2364 100644 --- a/src/database/pusher.rs +++ b/src/database/pusher.rs @@ -2,7 +2,7 @@ use crate::{Database, Error, PduEvent, Result}; use log::{error, info, warn}; use ruma::{ api::{ - client::r0::push::{Pusher, PusherKind}, + client::r0::push::{get_pushers, set_pusher, PusherKind}, push_gateway::send_event_notification::{ self, v1::{Device, Notification, NotificationCounts, NotificationPriority}, @@ -30,7 +30,7 @@ impl PushData { }) } - pub fn set_pusher(&self, sender: &UserId, pusher: Pusher) -> Result<()> { + pub fn set_pusher(&self, sender: &UserId, pusher: set_pusher::Pusher) -> Result<()> { let mut key = sender.as_bytes().to_vec(); key.push(0xff); key.extend_from_slice(pusher.pushkey.as_bytes()); @@ -52,7 +52,7 @@ impl PushData { Ok(()) } - pub fn get_pusher(&self, senderkey: &[u8]) -> Result> { + pub fn get_pusher(&self, senderkey: &[u8]) -> Result> { self.senderkey_pusher .get(senderkey)? .map(|push| { @@ -62,7 +62,7 @@ impl PushData { .transpose() } - pub fn get_pushers(&self, sender: &UserId) -> Result> { + pub fn get_pushers(&self, sender: &UserId) -> Result> { let mut prefix = sender.as_bytes().to_vec(); prefix.push(0xff); @@ -164,7 +164,7 @@ where pub async fn send_push_notice( user: &UserId, unread: UInt, - pusher: &Pusher, + pusher: &get_pushers::Pusher, ruleset: Ruleset, pdu: &PduEvent, db: &Database, @@ -205,7 +205,7 @@ pub fn get_actions<'a>( ruleset: &'a Ruleset, pdu: &PduEvent, db: &Database, -) -> Result> { +) -> Result<&'a [Action]> { let power_levels: PowerLevelsEventContent = db .rooms .room_state_get(&pdu.room_id, &EventType::RoomPowerLevels, "")? @@ -228,20 +228,18 @@ pub fn get_actions<'a>( notification_power_levels: power_levels.notifications, }; - Ok(ruleset - .get_actions(&pdu.to_sync_room_event(), &ctx) - .map(Clone::clone)) + Ok(ruleset.get_actions(&pdu.to_sync_room_event(), &ctx)) } async fn send_notice( unread: UInt, - pusher: &Pusher, + pusher: &get_pushers::Pusher, tweaks: Vec, event: &PduEvent, db: &Database, ) -> Result<()> { // TODO: email - if pusher.kind == Some(PusherKind::Email) { + if pusher.kind == PusherKind::Email { return Ok(()); } @@ -250,7 +248,7 @@ async fn send_notice( // 1. if "event_id_only" is the only format kind it seems we should never add more info // 2. can pusher/devices have conflicting formats let event_id_only = pusher.data.format == Some(PushFormat::EventIdOnly); - let url = if let Some(url) = pusher.data.url.as_ref() { + let url = if let Some(url) = &pusher.data.url { url } else { error!("Http Pusher must have URL specified."); diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 5685ac6b..5c518b33 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -4,7 +4,6 @@ use ruma::{ identifiers::{DeviceId, UserId}, Outgoing, }; -use std::collections::BTreeMap; use std::ops::Deref; #[cfg(feature = "conduit_bin")] @@ -27,6 +26,7 @@ use { signatures::CanonicalJsonValue, ServerName, }, + std::collections::BTreeMap, std::convert::TryFrom, std::io::Cursor, }; @@ -265,7 +265,10 @@ where match ruma::signatures::verify_json(&pub_key_map, &request_map) { Ok(()) => (None, None, false), Err(e) => { - warn!("Failed to verify json request: {}: {:?} {:?}", e, pub_key_map, request_map); + warn!( + "Failed to verify json request: {}: {:?} {:?}", + e, pub_key_map, request_map + ); // Forbidden return Failure((Status::raw(580), ())); diff --git a/src/server_server.rs b/src/server_server.rs index 90b5099f..8ff962ae 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -21,7 +21,7 @@ use ruma::{ create_join_event_template, }, query::{get_profile_information, get_room_information}, - transactions::send_transaction_message, + transactions::{edu::Edu, send_transaction_message}, }, IncomingResponse, OutgoingRequest, OutgoingResponse, }, @@ -585,39 +585,32 @@ pub async fn send_transaction_message_route<'a>( return Err(Error::bad_config("Federation is disabled.")); } - for edu in &body.edus { - match serde_json::from_str::(edu.json().get()) { - Ok(edu) => match edu.edu_type.as_str() { - "m.typing" => { - if let Some(typing) = edu.content.get("typing") { - if typing.as_bool().unwrap_or_default() { - db.rooms.edus.typing_add( - &UserId::try_from(edu.content["user_id"].as_str().unwrap()) - .unwrap(), - &RoomId::try_from(edu.content["room_id"].as_str().unwrap()) - .unwrap(), - 3000 + utils::millis_since_unix_epoch(), - &db.globals, - )?; - } else { - db.rooms.edus.typing_remove( - &UserId::try_from(edu.content["user_id"].as_str().unwrap()) - .unwrap(), - &RoomId::try_from(edu.content["room_id"].as_str().unwrap()) - .unwrap(), - &db.globals, - )?; - } - } + for edu in body + .edus + .iter() + .map(|edu| serde_json::from_str::(edu.json().get())) + .filter_map(|r| r.ok()) + { + match edu { + Edu::Presence(_) => {} + Edu::Receipt(_) => {} + Edu::Typing(typing) => { + if typing.typing { + db.rooms.edus.typing_add( + &typing.user_id, + &typing.room_id, + 3000 + utils::millis_since_unix_epoch(), + &db.globals, + )?; + } else { + db.rooms + .edus + .typing_remove(&typing.user_id, &typing.room_id, &db.globals)?; } - "m.presence" => {} - "m.receipt" => {} - "m.device_list_update" => {} - _ => {} - }, - Err(_err) => { - continue; } + Edu::DeviceListUpdate(_) => {} + Edu::DirectToDevice(_) => {} + Edu::_Custom(_) => {} } }