From 09561ccea3773f8581b3a4b78134e495a9b8a0b3 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Sun, 26 Jul 2020 15:41:28 +0200 Subject: [PATCH] Upgrade ruma --- Cargo.lock | 50 ++++++++----------- Cargo.toml | 2 +- src/client_server.rs | 96 ++++++++++++++++++------------------ src/database/account_data.rs | 12 ++--- src/database/global_edus.rs | 5 +- src/database/globals.rs | 2 +- src/database/key_backups.rs | 2 +- src/database/rooms.rs | 84 ++++++++++++++----------------- src/database/rooms/edus.rs | 7 ++- src/database/uiaa.rs | 6 +-- src/database/users.rs | 32 ++++++------ src/pdu.rs | 29 ++++++----- src/push_rules.rs | 2 +- src/ruma_wrapper.rs | 5 +- 14 files changed, 154 insertions(+), 180 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d9865b4..ab5b551 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1545,14 +1545,14 @@ dependencies = [ [[package]] name = "ruma" version = "0.1.0" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "ruma-api", "ruma-client-api", "ruma-common", "ruma-events", "ruma-federation-api", - "ruma-identifiers 0.17.0 (git+https://github.com/ruma/ruma?rev=08fbace)", + "ruma-identifiers", "ruma-identifiers-macros", "ruma-signatures", ] @@ -1560,12 +1560,12 @@ dependencies = [ [[package]] name = "ruma-api" version = "0.16.1" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "http", "percent-encoding", "ruma-api-macros", - "ruma-identifiers 0.17.0 (git+https://github.com/ruma/ruma?rev=08fbace)", + "ruma-identifiers", "ruma-serde", "serde", "serde_json", @@ -1575,7 +1575,7 @@ dependencies = [ [[package]] name = "ruma-api-macros" version = "0.16.1" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "proc-macro2", "quote", @@ -1585,14 +1585,14 @@ dependencies = [ [[package]] name = "ruma-client-api" version = "0.9.0" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "http", "js_int", "ruma-api", "ruma-common", "ruma-events", - "ruma-identifiers 0.17.0 (git+https://github.com/ruma/ruma?rev=08fbace)", + "ruma-identifiers", "ruma-serde", "serde", "serde_json", @@ -1602,7 +1602,7 @@ dependencies = [ [[package]] name = "ruma-common" version = "0.2.0" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "js_int", "ruma-serde", @@ -1614,12 +1614,12 @@ dependencies = [ [[package]] name = "ruma-events" version = "0.21.3" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "js_int", "ruma-common", "ruma-events-macros", - "ruma-identifiers 0.17.0 (git+https://github.com/ruma/ruma?rev=08fbace)", + "ruma-identifiers", "ruma-serde", "serde", "serde_json", @@ -1629,7 +1629,7 @@ dependencies = [ [[package]] name = "ruma-events-macros" version = "0.21.3" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "proc-macro2", "quote", @@ -1639,13 +1639,13 @@ dependencies = [ [[package]] name = "ruma-federation-api" version = "0.0.2" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "js_int", "ruma-api", "ruma-common", "ruma-events", - "ruma-identifiers 0.17.0 (git+https://github.com/ruma/ruma?rev=08fbace)", + "ruma-identifiers", "ruma-serde", "serde", "serde_json", @@ -1653,39 +1653,29 @@ dependencies = [ [[package]] name = "ruma-identifiers" -version = "0.17.0" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +version = "0.17.1" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "rand", "serde", "strum", ] -[[package]] -name = "ruma-identifiers" -version = "0.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7046d6ff26cf4f5b8bc77af68527544c61e32cab5810c40035c6491c08da0d3" -dependencies = [ - "serde", - "strum", -] - [[package]] name = "ruma-identifiers-macros" -version = "0.17.0" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +version = "0.17.1" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "proc-macro2", "quote", - "ruma-identifiers 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ruma-identifiers", "syn", ] [[package]] name = "ruma-serde" version = "0.2.2" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "form_urlencoded", "itoa", @@ -1697,7 +1687,7 @@ dependencies = [ [[package]] name = "ruma-signatures" version = "0.6.0-dev.1" -source = "git+https://github.com/ruma/ruma?rev=08fbace#08fbace022f732319b9c5d2b97954b935bf39bf2" +source = "git+https://github.com/ruma/ruma?rev=e047c647ddcb368e7eb1e05ae8823a9494273457#e047c647ddcb368e7eb1e05ae8823a9494273457" dependencies = [ "base64 0.12.3", "ring", diff --git a/Cargo.toml b/Cargo.toml index e0f246e..ec1ee63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ reqwest = "0.10.6" base64 = "0.12.1" thiserror = "1.0.19" image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } -ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "08fbace" } +ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "e047c647ddcb368e7eb1e05ae8823a9494273457" } diff --git a/src/client_server.rs b/src/client_server.rs index 0c085e3..3083ff2 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -64,10 +64,9 @@ use ruma::{ canonical_alias, guest_access, history_visibility, join_rules, member, name, redaction, topic, }, - AnyBasicEvent, AnyEphemeralRoomEvent, AnyEvent, AnySyncEphemeralRoomEvent, EventJson, - EventType, + AnyBasicEvent, AnyEphemeralRoomEvent, AnyEvent, AnySyncEphemeralRoomEvent, EventType, }, - identifiers::{RoomAliasId, RoomId, RoomVersionId, UserId}, + Raw, RoomAliasId, RoomId, RoomVersionId, UserId, }; use serde_json::json; @@ -624,7 +623,7 @@ pub fn set_displayname_route( EventType::RoomMember, serde_json::to_value(ruma::events::room::member::MemberEventContent { displayname: body.displayname.clone(), - ..serde_json::from_value::>( + ..serde_json::from_value::>( db.rooms .room_state_get(&room_id, &EventType::RoomMember, &user_id.to_string())? .ok_or_else(|| { @@ -659,7 +658,7 @@ pub fn set_displayname_route( .try_into() .expect("time is valid"), ), - presence: ruma::events::presence::PresenceState::Online, + presence: ruma::presence::PresenceState::Online, status_msg: None, }, sender: user_id.clone(), @@ -714,7 +713,7 @@ pub fn set_avatar_url_route( EventType::RoomMember, serde_json::to_value(ruma::events::room::member::MemberEventContent { avatar_url: body.avatar_url.clone(), - ..serde_json::from_value::>( + ..serde_json::from_value::>( db.rooms .room_state_get(&room_id, &EventType::RoomMember, &user_id.to_string())? .ok_or_else(|| { @@ -749,7 +748,7 @@ pub fn set_avatar_url_route( .try_into() .expect("time is valid"), ), - presence: ruma::events::presence::PresenceState::Online, + presence: ruma::presence::PresenceState::Online, status_msg: None, }, sender: user_id.clone(), @@ -1605,7 +1604,7 @@ pub fn leave_room_route( ) -> ConduitResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - let mut event = serde_json::from_value::>( + let mut event = serde_json::from_value::>( db.rooms .room_state_get(&body.room_id, &EventType::RoomMember, &user_id.to_string())? .ok_or(Error::BadRequest( @@ -1643,20 +1642,19 @@ pub fn kick_user_route( ) -> ConduitResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - let mut event = - serde_json::from_value::>( - db.rooms - .room_state_get(&body.room_id, &EventType::RoomMember, &user_id.to_string())? - .ok_or(Error::BadRequest( - ErrorKind::BadState, - "Cannot kick member that's not in the room.", - ))? - .content - .clone(), - ) - .map_err(|_| Error::bad_database("Invalid member event in database."))? - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; + let mut event = serde_json::from_value::>( + db.rooms + .room_state_get(&body.room_id, &EventType::RoomMember, &user_id.to_string())? + .ok_or(Error::BadRequest( + ErrorKind::BadState, + "Cannot kick member that's not in the room.", + ))? + .content + .clone(), + ) + .map_err(|_| Error::bad_database("Invalid member event in database."))? + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; event.membership = ruma::events::room::member::MembershipState::Leave; // TODO: reason @@ -1697,7 +1695,7 @@ pub fn ban_user_route( third_party_invite: None, }), |event| { - let mut event = serde_json::from_value::>( + let mut event = serde_json::from_value::>( event.content.clone(), ) .map_err(|_| Error::bad_database("Invalid member event in database."))? @@ -1730,20 +1728,19 @@ pub fn unban_user_route( ) -> ConduitResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - let mut event = - serde_json::from_value::>( - db.rooms - .room_state_get(&body.room_id, &EventType::RoomMember, &user_id.to_string())? - .ok_or(Error::BadRequest( - ErrorKind::BadState, - "Cannot unban a user who is not banned.", - ))? - .content - .clone(), - ) - .map_err(|_| Error::bad_database("Invalid member event in database."))? - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; + let mut event = serde_json::from_value::>( + db.rooms + .room_state_get(&body.room_id, &EventType::RoomMember, &user_id.to_string())? + .ok_or(Error::BadRequest( + ErrorKind::BadState, + "Cannot unban a user who is not banned.", + ))? + .content + .clone(), + ) + .map_err(|_| Error::bad_database("Invalid member event in database."))? + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; event.membership = ruma::events::room::member::MembershipState::Leave; @@ -1902,7 +1899,7 @@ pub async fn get_public_rooms_filtered_route( aliases: Vec::new(), canonical_alias: state.get(&(EventType::RoomCanonicalAlias, "".to_owned())).map_or(Ok::<_, Error>(None), |s| { Ok(serde_json::from_value::< - EventJson, + Raw, >(s.content.clone()) .map_err(|_| Error::bad_database("Invalid canonical alias event in database."))? .deserialize() @@ -1910,7 +1907,7 @@ pub async fn get_public_rooms_filtered_route( .alias) })?, name: state.get(&(EventType::RoomName, "".to_owned())).map_or(Ok::<_, Error>(None), |s| { - Ok(serde_json::from_value::>( + Ok(serde_json::from_value::>( s.content.clone(), ) .map_err(|_| Error::bad_database("Invalid room name event in database."))? @@ -1923,7 +1920,7 @@ pub async fn get_public_rooms_filtered_route( room_id, topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or(Ok::<_, Error>(None), |s| { Ok(Some(serde_json::from_value::< - EventJson, + Raw, >(s.content.clone()) .map_err(|_| Error::bad_database("Invalid room topic event in database."))? .deserialize() @@ -1932,7 +1929,7 @@ pub async fn get_public_rooms_filtered_route( })?, world_readable: state.get(&(EventType::RoomHistoryVisibility, "".to_owned())).map_or(Ok::<_, Error>(false), |s| { Ok(serde_json::from_value::< - EventJson, + Raw, >(s.content.clone()) .map_err(|_| Error::bad_database("Invalid room history visibility event in database."))? .deserialize() @@ -1941,7 +1938,7 @@ pub async fn get_public_rooms_filtered_route( })?, guest_can_join: state.get(&(EventType::RoomGuestAccess, "".to_owned())).map_or(Ok::<_, Error>(false), |s| { Ok(serde_json::from_value::< - EventJson, + Raw, >(s.content.clone()) .map_err(|_| Error::bad_database("Invalid room guest access event in database."))? .deserialize() @@ -1950,7 +1947,7 @@ pub async fn get_public_rooms_filtered_route( })?, avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or( Ok::<_, Error>(None),|s| { Ok(Some(serde_json::from_value::< - EventJson, + Raw, >(s.content.clone()) .map_err(|_| Error::bad_database("Invalid room avatar event in database."))? .deserialize() @@ -2158,7 +2155,7 @@ pub fn create_state_event_for_key_route( if body.event_type == EventType::RoomCanonicalAlias { let canonical_alias = serde_json::from_value::< - EventJson, + Raw, >(content.clone()) .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid canonical alias."))? .deserialize() @@ -2375,9 +2372,9 @@ pub fn sync_route( send_member_count = true; if !joined_since_last_sync && pdu.state_key == Some(user_id.to_string()) { let content = serde_json::from_value::< - EventJson, + Raw, >(pdu.content.clone()) - .expect("EventJson::from_value always works") + .expect("Raw::from_value always works") .deserialize() .map_err(|_| Error::bad_database("Invalid PDU in database."))?; if content.membership == ruma::events::room::member::MembershipState::Join { @@ -2410,7 +2407,7 @@ pub fn sync_route( .filter(|pdu| pdu.kind == EventType::RoomMember) .map(|pdu| { let content = serde_json::from_value::< - EventJson, + Raw, >(pdu.content.clone()) .map_err(|_| Error::bad_database("Invalid member event in database."))? .deserialize() @@ -2418,7 +2415,7 @@ pub fn sync_route( if let Some(state_key) = &pdu.state_key { let current_content = serde_json::from_value::< - EventJson, + Raw, >( members .get(state_key) @@ -2660,7 +2657,8 @@ pub fn sync_route( } // Remove all to-device events the device received *last time* - db.users.remove_to_device_events(user_id, device_id, since)?; + db.users + .remove_to_device_events(user_id, device_id, since)?; Ok(sync_events::Response { next_batch, diff --git a/src/database/account_data.rs b/src/database/account_data.rs index 3b64ba7..8397c12 100644 --- a/src/database/account_data.rs +++ b/src/database/account_data.rs @@ -1,8 +1,8 @@ use crate::{utils, Error, Result}; use ruma::{ api::client::error::ErrorKind, - events::{AnyEvent as EduEvent, EventJson, EventType}, - identifiers::{RoomId, UserId}, + events::{AnyEvent as EduEvent, EventType}, + Raw, RoomId, UserId, }; use std::{collections::HashMap, convert::TryFrom}; @@ -81,7 +81,7 @@ impl AccountData { room_id: Option<&RoomId>, user_id: &UserId, kind: &EventType, - ) -> Result>> { + ) -> Result>> { Ok(self.all(room_id, user_id)?.remove(kind)) } @@ -91,7 +91,7 @@ impl AccountData { room_id: Option<&RoomId>, user_id: &UserId, since: u64, - ) -> Result>> { + ) -> Result>> { let mut userdata = HashMap::new(); let mut prefix = room_id @@ -121,7 +121,7 @@ impl AccountData { .map_err(|_| Error::bad_database("RoomUserData ID in db is invalid."))?, ) .map_err(|_| Error::bad_database("RoomUserData ID in db is invalid."))?, - serde_json::from_slice::>(&v).map_err(|_| { + serde_json::from_slice::>(&v).map_err(|_| { Error::bad_database("Database contains invalid account data.") })?, )) @@ -139,7 +139,7 @@ impl AccountData { &self, room_id: Option<&RoomId>, user_id: &UserId, - ) -> Result>> { + ) -> Result>> { self.changes_since(room_id, user_id, 0) } } diff --git a/src/database/global_edus.rs b/src/database/global_edus.rs index f58c7d6..94f2de8 100644 --- a/src/database/global_edus.rs +++ b/src/database/global_edus.rs @@ -1,5 +1,5 @@ use crate::{Error, Result}; -use ruma::events::EventJson; +use ruma::Raw; pub struct GlobalEdus { //pub globalallid_globalall: sled::Tree, // ToDevice, GlobalAllId = UserId + Count @@ -47,8 +47,7 @@ impl GlobalEdus { pub fn presence_since( &self, since: u64, - ) -> Result>>> - { + ) -> Result>>> { let first_possible_edu = (since + 1).to_be_bytes().to_vec(); // +1 so we don't send the event at since Ok(self diff --git a/src/database/globals.rs b/src/database/globals.rs index d59e1c3..4578605 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -1,7 +1,7 @@ use std::convert::TryInto; use crate::{utils, Error, Result}; -use ruma::identifiers::ServerName; +use ruma::ServerName; pub const COUNTER: &str = "c"; pub struct Globals { diff --git a/src/database/key_backups.rs b/src/database/key_backups.rs index eb31b8d..a506564 100644 --- a/src/database/key_backups.rs +++ b/src/database/key_backups.rs @@ -4,7 +4,7 @@ use ruma::{ error::ErrorKind, r0::backup::{BackupAlgorithm, KeyData, Sessions}, }, - identifiers::{RoomId, UserId}, + {RoomId, UserId}, }; use std::{collections::BTreeMap, convert::TryFrom}; diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 7e6036d..5b9659e 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -12,9 +12,9 @@ use ruma::{ power_levels::{self, PowerLevelsEventContent}, redaction, }, - EventJson, EventType, + EventType, }, - identifiers::{EventId, RoomAliasId, RoomId, UserId}, + EventId, Raw, RoomAliasId, RoomId, UserId, }; use sled::IVec; use std::{ @@ -287,28 +287,24 @@ impl Rooms { }) }, |power_levels| { - Ok( - serde_json::from_value::>( - power_levels.content.clone(), - ) - .expect("EventJson::from_value always works.") - .deserialize() - .map_err(|_| Error::bad_database("Invalid PowerLevels event in db."))?, + Ok(serde_json::from_value::>( + power_levels.content.clone(), ) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| Error::bad_database("Invalid PowerLevels event in db."))?) }, )?; let sender_membership = self .room_state_get(&room_id, &EventType::RoomMember, &sender.to_string())? .map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| { - Ok( - serde_json::from_value::>( - pdu.content.clone(), - ) - .expect("EventJson::from_value always works.") - .deserialize() - .map_err(|_| Error::bad_database("Invalid Member event in db."))? - .membership, + Ok(serde_json::from_value::>( + pdu.content.clone(), ) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| Error::bad_database("Invalid Member event in db."))? + .membership) })?; let sender_power = power_levels.users.get(&sender).map_or_else( @@ -339,24 +335,21 @@ impl Rooms { &target_user_id.to_string(), )? .map_or(Ok::<_, Error>(member::MembershipState::Leave), |pdu| { - Ok( - serde_json::from_value::>( - pdu.content.clone(), - ) - .expect("EventJson::from_value always works.") - .deserialize() - .map_err(|_| Error::bad_database("Invalid Member event in db."))? - .membership, + Ok(serde_json::from_value::>( + pdu.content.clone(), ) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| Error::bad_database("Invalid Member event in db."))? + .membership) })?; - let target_membership = serde_json::from_value::< - EventJson, - >(content.clone()) - .expect("EventJson::from_value always works.") - .deserialize() - .map_err(|_| Error::bad_database("Invalid Member event in db."))? - .membership; + let target_membership = + serde_json::from_value::>(content.clone()) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| Error::bad_database("Invalid Member event in db."))? + .membership; let target_power = power_levels.users.get(&target_user_id).map_or_else( || { @@ -374,9 +367,9 @@ impl Rooms { self.room_state_get(&room_id, &EventType::RoomJoinRules, "")? .map_or(Ok::<_, Error>(join_rules::JoinRule::Public), |pdu| { Ok(serde_json::from_value::< - EventJson, + Raw, >(pdu.content.clone()) - .expect("EventJson::from_value always works.") + .expect("Raw::from_value always works.") .deserialize() .map_err(|_| { Error::bad_database("Database contains invalid JoinRules event") @@ -581,18 +574,17 @@ impl Rooms { EventType::RoomRedaction => { if let Some(redact_id) = &redacts { // TODO: Reason - let _reason = serde_json::from_value::< - EventJson, - >(content) - .expect("EventJson::from_value always works.") - .deserialize() - .map_err(|_| { - Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid redaction event content.", - ) - })? - .reason; + let _reason = + serde_json::from_value::>(content) + .expect("Raw::from_value always works.") + .deserialize() + .map_err(|_| { + Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid redaction event content.", + ) + })? + .reason; self.redact_pdu(&redact_id)?; } diff --git a/src/database/rooms/edus.rs b/src/database/rooms/edus.rs index b96f1b1..22d0166 100644 --- a/src/database/rooms/edus.rs +++ b/src/database/rooms/edus.rs @@ -1,7 +1,7 @@ use crate::{utils, Error, Result}; use ruma::{ - events::{AnyEvent as EduEvent, EventJson, SyncEphemeralRoomEvent}, - identifiers::{RoomId, UserId}, + events::{AnyEvent as EduEvent, SyncEphemeralRoomEvent}, + Raw, RoomId, UserId, }; use std::convert::TryFrom; @@ -61,8 +61,7 @@ impl RoomEdus { &self, room_id: &RoomId, since: u64, - ) -> Result>>> - { + ) -> Result>>> { let mut prefix = room_id.to_string().as_bytes().to_vec(); prefix.push(0xff); diff --git a/src/database/uiaa.rs b/src/database/uiaa.rs index d490499..4366eb2 100644 --- a/src/database/uiaa.rs +++ b/src/database/uiaa.rs @@ -4,7 +4,7 @@ use ruma::{ error::ErrorKind, r0::uiaa::{AuthData, UiaaInfo}, }, - identifiers::{DeviceId, UserId}, + DeviceId, UserId, }; pub struct Uiaa { @@ -149,7 +149,7 @@ impl Uiaa { ) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); if let Some(uiaainfo) = uiaainfo { self.userdeviceid_uiaainfo.insert( @@ -171,7 +171,7 @@ impl Uiaa { ) -> Result { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); let uiaainfo = serde_json::from_slice::( &self diff --git a/src/database/users.rs b/src/database/users.rs index 6e6258f..5030f32 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -8,8 +8,8 @@ use ruma::{ keys::{AlgorithmAndDeviceId, CrossSigningKey, DeviceKeys, KeyAlgorithm, OneTimeKey}, }, }, - events::{AnyToDeviceEvent, EventJson, EventType}, - identifiers::{DeviceId, UserId}, + events::{AnyToDeviceEvent, EventType}, + DeviceId, Raw, UserId, }; use std::{collections::BTreeMap, convert::TryFrom, mem, time::SystemTime}; @@ -177,7 +177,7 @@ impl Users { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); self.userdeviceid_metadata.insert( userdeviceid, @@ -200,7 +200,7 @@ impl Users { pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); // Remove tokens if let Some(old_token) = self.userdeviceid_token.remove(&userdeviceid)? { @@ -246,7 +246,7 @@ impl Users { fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); // All devices have metadata assert!(self.userdeviceid_metadata.get(&userdeviceid)?.is_some()); @@ -273,7 +273,7 @@ impl Users { ) -> Result<()> { let mut key = user_id.to_string().as_bytes().to_vec(); key.push(0xff); - key.extend_from_slice(device_id.as_str().as_bytes()); + key.extend_from_slice(device_id.as_bytes()); // All devices have metadata // Only existing devices should be able to call this. @@ -305,7 +305,7 @@ impl Users { ) -> Result> { let mut prefix = user_id.to_string().as_bytes().to_vec(); prefix.push(0xff); - prefix.extend_from_slice(device_id.as_str().as_bytes()); + prefix.extend_from_slice(device_id.as_bytes()); prefix.push(0xff); prefix.push(b'"'); // Annoying quotation mark prefix.extend_from_slice(key_algorithm.to_string().as_bytes()); @@ -340,7 +340,7 @@ impl Users { ) -> Result> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); let mut counts = BTreeMap::new(); @@ -375,7 +375,7 @@ impl Users { ) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); self.keyid_key.insert( &userdeviceid, @@ -556,7 +556,7 @@ impl Users { ) -> Result> { let mut key = user_id.to_string().as_bytes().to_vec(); key.push(0xff); - key.extend_from_slice(device_id.as_str().as_bytes()); + key.extend_from_slice(device_id.as_bytes()); self.keyid_key.get(key)?.map_or(Ok(None), |bytes| { Ok(Some(serde_json::from_slice(&bytes).map_err(|_| { @@ -643,7 +643,7 @@ impl Users { ) -> Result<()> { let mut key = target_user_id.to_string().as_bytes().to_vec(); key.push(0xff); - key.extend_from_slice(target_device_id.as_str().as_bytes()); + key.extend_from_slice(target_device_id.as_bytes()); key.push(0xff); key.extend_from_slice(&globals.next_count()?.to_be_bytes()); @@ -664,12 +664,12 @@ impl Users { &self, user_id: &UserId, device_id: &DeviceId, - ) -> Result>> { + ) -> Result>> { let mut events = Vec::new(); let mut prefix = user_id.to_string().as_bytes().to_vec(); prefix.push(0xff); - prefix.extend_from_slice(device_id.as_str().as_bytes()); + prefix.extend_from_slice(device_id.as_bytes()); prefix.push(0xff); for value in self.todeviceid_events.scan_prefix(&prefix).values() { @@ -690,7 +690,7 @@ impl Users { ) -> Result<()> { let mut prefix = user_id.to_string().as_bytes().to_vec(); prefix.push(0xff); - prefix.extend_from_slice(device_id.as_ref().as_bytes()); + prefix.extend_from_slice(device_id.as_bytes()); prefix.push(0xff); let mut last = prefix.clone(); @@ -725,7 +725,7 @@ impl Users { ) -> Result<()> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); // Only existing devices should be able to call this. assert!(self.userdeviceid_metadata.get(&userdeviceid)?.is_some()); @@ -748,7 +748,7 @@ impl Users { ) -> Result> { let mut userdeviceid = user_id.to_string().as_bytes().to_vec(); userdeviceid.push(0xff); - userdeviceid.extend_from_slice(device_id.as_str().as_bytes()); + userdeviceid.extend_from_slice(device_id.as_bytes()); self.userdeviceid_metadata .get(&userdeviceid)? diff --git a/src/pdu.rs b/src/pdu.rs index 0db77a1..0cfdb63 100644 --- a/src/pdu.rs +++ b/src/pdu.rs @@ -3,10 +3,9 @@ use js_int::UInt; use ruma::{ events::{ pdu::EventHash, room::member::MemberEventContent, AnyRoomEvent, AnyStateEvent, - AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventJson, EventType, - StateEvent, + AnyStrippedStateEvent, AnySyncRoomEvent, AnySyncStateEvent, EventType, StateEvent, }, - identifiers::{EventId, RoomId, ServerName, UserId}, + EventId, Raw, RoomId, ServerName, UserId, }; use serde::{Deserialize, Serialize}; use serde_json::json; @@ -79,40 +78,40 @@ impl PduEvent { Ok(()) } - pub fn to_sync_room_event(&self) -> EventJson { + pub fn to_sync_room_event(&self) -> Raw { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::(&json) - .map(EventJson::from) + .map(Raw::from) .expect("AnySyncRoomEvent can always be built from a full PDU event") } - pub fn to_room_event(&self) -> EventJson { + pub fn to_room_event(&self) -> Raw { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::(&json) - .map(EventJson::from) + .map(Raw::from) .expect("AnyRoomEvent can always be built from a full PDU event") } - pub fn to_state_event(&self) -> EventJson { + pub fn to_state_event(&self) -> Raw { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::(&json) - .map(EventJson::from) + .map(Raw::from) .expect("AnyStateEvent can always be built from a full PDU event") } - pub fn to_sync_state_event(&self) -> EventJson { + pub fn to_sync_state_event(&self) -> Raw { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::(&json) - .map(EventJson::from) + .map(Raw::from) .expect("AnySyncStateEvent can always be built from a full PDU event") } - pub fn to_stripped_state_event(&self) -> EventJson { + pub fn to_stripped_state_event(&self) -> Raw { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::(&json) - .map(EventJson::from) + .map(Raw::from) .expect("AnyStrippedStateEvent can always be built from a full PDU event") } - pub fn to_member_event(&self) -> EventJson> { + pub fn to_member_event(&self) -> Raw> { let json = serde_json::to_string(&self).expect("PDUs are always valid"); serde_json::from_str::>(&json) - .map(EventJson::from) + .map(Raw::from) .expect("StateEvent can always be built from a full PDU event") } } diff --git a/src/push_rules.rs b/src/push_rules.rs index 5bc0d74..a1f32f4 100644 --- a/src/push_rules.rs +++ b/src/push_rules.rs @@ -1,6 +1,6 @@ use js_int::uint; use ruma::{ - identifiers::UserId, + UserId, push::{ Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule, PatternedPushRuleInit, PushCondition, RoomMemberCountIs, Ruleset, Tweak, diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index cd2d21e..15e50ba 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -7,10 +7,7 @@ use rocket::{ Outcome::*, Request, State, }; -use ruma::{ - api::Endpoint, - identifiers::{DeviceId, UserId}, -}; +use ruma::{api::Endpoint, DeviceId, UserId}; use std::{convert::TryInto, io::Cursor, ops::Deref}; use tokio::io::AsyncReadExt;