Return correct Errors and replace panic, fix misc review issues

Remove EventJson todo comments, clean up Cargo.toml commented ruma deps.
This commit is contained in:
Devin R 2020-06-24 17:03:33 -04:00
parent ddc7598870
commit 84dcb885a7
5 changed files with 20 additions and 43 deletions

View file

@ -29,16 +29,6 @@ thiserror = "1.0.19"
image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] }
[dependencies.ruma] [dependencies.ruma]
git = "https://github.com/ruma/ruma" git = "https://github.com/DevinR528/ruma"
# branch = "matrix-sdk2" branch = "key-sign"
#path = "../ruma/ruma"
features = ["rand", "client-api", "federation-api"] 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" }

View file

@ -602,15 +602,17 @@ pub fn get_global_account_data_route(
)? )?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?;
// TODO clearly this is not ideal... let data: Result<EduEvent, Error> = data
// NOTE: EventJson is no longer needed as all the enums and event structs impl ser/de .deserialize()
let data: Result<EduEvent, Error> = data.deserialize().map_err(Into::into); .map_err(|_| Error::bad_database("Deserialization of account data failed"));
match data? {
EduEvent::Basic(data) => Ok(get_global_account_data::Response { if let EduEvent::Basic(data) = data? {
Ok(get_global_account_data::Response {
account_data: EventJson::from(data), account_data: EventJson::from(data),
} }
.into()), .into())
_ => panic!("timo what do i do here"), } 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| r.ok()) // Filter out buggy events
.filter_map(|r| { .filter_map(|r| {
if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() {
// TODO we could get rid of EventJson?
Some(EventJson::from(ev)) Some(EventJson::from(ev))
} else { } else {
None None
@ -2554,8 +2555,8 @@ pub fn sync_route(
.account_data .account_data
.changes_since(Some(&room_id), &user_id, since)? .changes_since(Some(&room_id), &user_id, since)?
.into_iter() .into_iter()
.flat_map(|(_, v)| { .filter_map(|(_, v)| {
if let Some(EduEvent::Basic(account_event)) = v.deserialize().ok() { if let Ok(EduEvent::Basic(account_event)) = v.deserialize() {
Some(EventJson::from(account_event)) Some(EventJson::from(account_event))
} else { } else {
None None
@ -2618,7 +2619,6 @@ pub fn sync_route(
.filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| r.ok()) // Filter out buggy events
.filter_map(|r| { .filter_map(|r| {
if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() { if let Ok(EduEvent::Ephemeral(ev)) = r.deserialize() {
// TODO we could get rid of EventJson?
Some(EventJson::from(ev)) Some(EventJson::from(ev))
} else { } else {
None None
@ -2710,8 +2710,8 @@ pub fn sync_route(
.account_data .account_data
.changes_since(None, &user_id, since)? .changes_since(None, &user_id, since)?
.into_iter() .into_iter()
.flat_map(|(_, v)| { .filter_map(|(_, v)| {
if let Some(EduEvent::Basic(account_event)) = v.deserialize().ok() { if let Ok(EduEvent::Basic(account_event)) = v.deserialize() {
Some(EventJson::from(account_event)) Some(EventJson::from(account_event))
} else { } else {
None None
@ -2859,13 +2859,12 @@ pub fn get_message_events_route(
.clone() .clone()
.parse() .parse()
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?; .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 { match body.dir {
get_message_events::Direction::Forward => { 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 let events_after = db
.rooms .rooms
.pdus_after(&user_id, &body.room_id, from) .pdus_after(&user_id, &body.room_id, from)
@ -2897,11 +2896,6 @@ pub fn get_message_events_route(
.into()) .into())
} }
get_message_events::Direction::Backward => { 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 let events_before = db
.rooms .rooms
.pdus_until(&user_id, &body.room_id, from) .pdus_until(&user_id, &body.room_id, from)

View file

@ -32,7 +32,7 @@ impl Globals {
.unwrap_or("localhost") .unwrap_or("localhost")
.to_owned(), .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), registration_disabled: config.get_bool("registration_disabled").unwrap_or(false),
}) })
} }

View file

@ -62,8 +62,6 @@ impl RoomEdus {
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Result<impl Iterator<Item = Result<EventJson<EduEvent>>>> { ) -> Result<impl Iterator<Item = Result<EventJson<EduEvent>>>> {
// 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(); let mut prefix = room_id.to_string().as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);

View file

@ -25,11 +25,6 @@ pub enum Error {
#[from] #[from]
source: image::error::ImageError, source: image::error::ImageError,
}, },
#[error("Could not deserialize json")]
SerdeError {
#[from]
source: serde_json::Error,
},
#[error("{0}")] #[error("{0}")]
BadConfig(&'static str), BadConfig(&'static str),
#[error("{0}")] #[error("{0}")]