From 84dcb885a7fb28a1472656a8e036199381c0fd19 Mon Sep 17 00:00:00 2001 From: Devin R Date: Wed, 24 Jun 2020 17:03:33 -0400 Subject: [PATCH] Return correct Errors and replace panic, fix misc review issues Remove EventJson todo comments, clean up Cargo.toml commented ruma deps. --- Cargo.toml | 14 ++----------- src/client_server.rs | 40 ++++++++++++++++---------------------- src/database/globals.rs | 2 +- src/database/rooms/edus.rs | 2 -- src/error.rs | 5 ----- 5 files changed, 20 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 55308b5..f15c403 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,16 +29,6 @@ thiserror = "1.0.19" image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } [dependencies.ruma] -git = "https://github.com/ruma/ruma" -# branch = "matrix-sdk2" -#path = "../ruma/ruma" +git = "https://github.com/DevinR528/ruma" +branch = "key-sign" features = ["rand", "client-api", "federation-api"] - -# These are required only until ruma-events and ruma-federation-api are merged into ruma/ruma -# [patch.crates-io] -# ruma-common = { git = "https://github.com/ruma/ruma", rev = "baa87104569b45dc07a9a7a16d3c7592ab8f4d6b" } -# ruma-serde = { git = "https://github.com/ruma/ruma", rev = "baa87104569b45dc07a9a7a16d3c7592ab8f4d6b" } -# ruma-identifiers = { git = "https://github.com/ruma/ruma", rev = "baa87104569b45dc07a9a7a16d3c7592ab8f4d6b" } -#ruma-common = { path = "../ruma/ruma-common" } -#ruma-serde = { path = "../ruma/ruma-serde" } -#ruma-identifiers = { path = "../ruma/ruma-identifiers" } diff --git a/src/client_server.rs b/src/client_server.rs index 15ced16..d3ff62c 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -602,15 +602,17 @@ pub fn get_global_account_data_route( )? .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; - // TODO clearly this is not ideal... - // NOTE: EventJson is no longer needed as all the enums and event structs impl ser/de - let data: Result = data.deserialize().map_err(Into::into); - match data? { - EduEvent::Basic(data) => Ok(get_global_account_data::Response { + let data: Result = data + .deserialize() + .map_err(|_| Error::bad_database("Deserialization of account data failed")); + + if let EduEvent::Basic(data) = data? { + Ok(get_global_account_data::Response { account_data: EventJson::from(data), } - .into()), - _ => panic!("timo what do i do here"), + .into()) + } else { + Err(Error::bad_database("Encountered a non account data event.")) } } @@ -2523,7 +2525,6 @@ pub fn sync_route( .filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| { if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { - // TODO we could get rid of EventJson? Some(EventJson::from(ev)) } else { None @@ -2554,8 +2555,8 @@ pub fn sync_route( .account_data .changes_since(Some(&room_id), &user_id, since)? .into_iter() - .flat_map(|(_, v)| { - if let Some(EduEvent::Basic(account_event)) = v.deserialize().ok() { + .filter_map(|(_, v)| { + if let Ok(EduEvent::Basic(account_event)) = v.deserialize() { Some(EventJson::from(account_event)) } else { None @@ -2618,7 +2619,6 @@ pub fn sync_route( .filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| { if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { - // TODO we could get rid of EventJson? Some(EventJson::from(ev)) } else { None @@ -2710,8 +2710,8 @@ pub fn sync_route( .account_data .changes_since(None, &user_id, since)? .into_iter() - .flat_map(|(_, v)| { - if let Some(EduEvent::Basic(account_event)) = v.deserialize().ok() { + .filter_map(|(_, v)| { + if let Ok(EduEvent::Basic(account_event)) = v.deserialize() { Some(EventJson::from(account_event)) } else { None @@ -2859,13 +2859,12 @@ pub fn get_message_events_route( .clone() .parse() .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?; + let limit = body + .limit + .try_into() + .map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?; match body.dir { get_message_events::Direction::Forward => { - let limit = body - .limit - .try_into() - .map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?; - let events_after = db .rooms .pdus_after(&user_id, &body.room_id, from) @@ -2897,11 +2896,6 @@ pub fn get_message_events_route( .into()) } get_message_events::Direction::Backward => { - let limit = body - .limit - .try_into() - .map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?; - let events_before = db .rooms .pdus_until(&user_id, &body.room_id, from) diff --git a/src/database/globals.rs b/src/database/globals.rs index 00e0982..b04eebb 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -32,7 +32,7 @@ impl Globals { .unwrap_or("localhost") .to_owned(), ) - .map_err(|_| Error::bad_database("Invalid server name"))?, + .map_err(|_| Error::BadConfig("Invalid server name"))?, registration_disabled: config.get_bool("registration_disabled").unwrap_or(false), }) } diff --git a/src/database/rooms/edus.rs b/src/database/rooms/edus.rs index c352a01..de138b6 100644 --- a/src/database/rooms/edus.rs +++ b/src/database/rooms/edus.rs @@ -62,8 +62,6 @@ impl RoomEdus { room_id: &RoomId, since: u64, ) -> Result>>> { - // TODO is this ^^^^^^^ - // only ever a read receipt could we just return EphemeralRoomEvent here? let mut prefix = room_id.to_string().as_bytes().to_vec(); prefix.push(0xff); diff --git a/src/error.rs b/src/error.rs index e1af15a..7305073 100644 --- a/src/error.rs +++ b/src/error.rs @@ -25,11 +25,6 @@ pub enum Error { #[from] source: image::error::ImageError, }, - #[error("Could not deserialize json")] - SerdeError { - #[from] - source: serde_json::Error, - }, #[error("{0}")] BadConfig(&'static str), #[error("{0}")]