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"] }
[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" }

View file

@ -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<EduEvent, Error> = data.deserialize().map_err(Into::into);
match data? {
EduEvent::Basic(data) => Ok(get_global_account_data::Response {
let data: Result<EduEvent, Error> = 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)

View file

@ -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),
})
}

View file

@ -62,8 +62,6 @@ impl RoomEdus {
room_id: &RoomId,
since: u64,
) -> 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();
prefix.push(0xff);

View file

@ -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}")]