diff --git a/src/client_server.rs b/src/client_server.rs index 48c03c3..208e360 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -594,27 +594,18 @@ pub fn get_global_account_data_route( ) -> ConduitResult { let user_id = body.user_id.as_ref().expect("user is authenticated"); - let data = db - .account_data + db.account_data .get( None, user_id, &EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), )? - .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; - - let data: AnyEvent = data - .deserialize() - .map_err(|_| Error::bad_database("Deserialization of account data failed"))?; - - if let AnyEvent::Basic(data) = data { - Ok(get_global_account_data::Response { - account_data: data.into(), - } - .into()) - } else { - Err(Error::bad_database("Encountered a non account data event.")) - } + .and_then(|ev| { + serde_json::from_str(ev.json().get()) + .map(|data| get_global_account_data::Response { account_data: data }.into()) + .ok() + }) + .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found.")) } #[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] @@ -2546,14 +2537,12 @@ pub fn sync_route( .account_data .changes_since(Some(&room_id), &user_id, since)? .into_iter() - .map(|(_, v)| { - if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { - Ok(EventJson::from(account_event)) - } else { - Err(Error::bad_database("found invalid event")) - } + .filter_map(|(_, v)| { + serde_json::from_str(v.json().get()) + .map_err(|_| Error::bad_database("Invalid account event in database.")) + .ok() }) - .collect::, _>>()?, + .collect::>(), }, summary: sync_events::RoomSummary { heroes, @@ -2690,14 +2679,12 @@ pub fn sync_route( .account_data .changes_since(None, &user_id, since)? .into_iter() - .map(|(_, v)| { - if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { - Ok(EventJson::from(account_event)) - } else { - Err(Error::bad_database("found invalid event")) - } + .filter_map(|(_, v)| { + serde_json::from_str(v.json().get()) + .map_err(|_| Error::bad_database("Invalid account event in database.")) + .ok() }) - .collect::, _>>()?, + .collect::>(), }, device_lists: sync_events::DeviceLists { changed: if since != 0 { diff --git a/src/database/globals.rs b/src/database/globals.rs index 1654636..d59e1c3 100644 --- a/src/database/globals.rs +++ b/src/database/globals.rs @@ -31,7 +31,7 @@ impl Globals { .unwrap_or("localhost") .to_string() .try_into() - .map_err(|_| Error::bad_database("Private or public keys are invalid."))?, + .map_err(|_| Error::BadConfig("Invalid server name found."))?, registration_disabled: config.get_bool("registration_disabled").unwrap_or(false), }) }