Simplify deserialization and deconstruction for Responses

next
Devin R 2020-07-21 16:26:01 -04:00
parent 33bc666859
commit 84d47da392
2 changed files with 18 additions and 31 deletions

View File

@ -594,27 +594,18 @@ pub fn get_global_account_data_route(
) -> ConduitResult<get_global_account_data::Response> { ) -> ConduitResult<get_global_account_data::Response> {
let user_id = body.user_id.as_ref().expect("user is authenticated"); let user_id = body.user_id.as_ref().expect("user is authenticated");
let data = db db.account_data
.account_data
.get( .get(
None, None,
user_id, user_id,
&EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), &EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"),
)? )?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; .and_then(|ev| {
serde_json::from_str(ev.json().get())
let data: AnyEvent = data .map(|data| get_global_account_data::Response { account_data: data }.into())
.deserialize() .ok()
.map_err(|_| Error::bad_database("Deserialization of account data failed"))?; })
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))
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."))
}
} }
#[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "<body>")] #[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "<body>")]
@ -2546,14 +2537,12 @@ 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()
.map(|(_, v)| { .filter_map(|(_, v)| {
if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { serde_json::from_str(v.json().get())
Ok(EventJson::from(account_event)) .map_err(|_| Error::bad_database("Invalid account event in database."))
} else { .ok()
Err(Error::bad_database("found invalid event"))
}
}) })
.collect::<Result<Vec<_>, _>>()?, .collect::<Vec<_>>(),
}, },
summary: sync_events::RoomSummary { summary: sync_events::RoomSummary {
heroes, heroes,
@ -2690,14 +2679,12 @@ pub fn sync_route(
.account_data .account_data
.changes_since(None, &user_id, since)? .changes_since(None, &user_id, since)?
.into_iter() .into_iter()
.map(|(_, v)| { .filter_map(|(_, v)| {
if let Ok(AnyEvent::Basic(account_event)) = v.deserialize() { serde_json::from_str(v.json().get())
Ok(EventJson::from(account_event)) .map_err(|_| Error::bad_database("Invalid account event in database."))
} else { .ok()
Err(Error::bad_database("found invalid event"))
}
}) })
.collect::<Result<Vec<_>, _>>()?, .collect::<Vec<_>>(),
}, },
device_lists: sync_events::DeviceLists { device_lists: sync_events::DeviceLists {
changed: if since != 0 { changed: if since != 0 {

View File

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