misc cleanup

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-03 10:03:31 +00:00 committed by strawberry
parent 57e0a5f65d
commit f503ed918c
7 changed files with 49 additions and 71 deletions

View file

@ -198,7 +198,6 @@ impl Service {
Ok(None) => debug!("Command successful with no response"),
Ok(Some(output)) | Err(output) => self
.handle_response(output)
.boxed()
.await
.unwrap_or_else(default_log),
}
@ -277,6 +276,7 @@ impl Service {
};
self.respond_to_room(content, &pdu.room_id, response_sender)
.boxed()
.await
}

View file

@ -1,29 +1,29 @@
use conduit::{debug_warn, err, pdu::gen_event_id_canonical_json, Err, Result};
use ruma::{CanonicalJsonObject, OwnedEventId, OwnedRoomId, RoomId};
use conduit::{err, pdu::gen_event_id_canonical_json, result::FlatOk, Result};
use ruma::{CanonicalJsonObject, CanonicalJsonValue, OwnedEventId, OwnedRoomId, RoomId};
use serde_json::value::RawValue as RawJsonValue;
impl super::Service {
pub async fn parse_incoming_pdu(
&self, pdu: &RawJsonValue,
) -> Result<(OwnedEventId, CanonicalJsonObject, OwnedRoomId)> {
let value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
debug_warn!("Error parsing incoming event {pdu:#?}");
err!(BadServerResponse("Error parsing incoming event {e:?}"))
})?;
let value = serde_json::from_str::<CanonicalJsonObject>(pdu.get())
.map_err(|e| err!(BadServerResponse(debug_warn!("Error parsing incoming event {e:?}"))))?;
let room_id: OwnedRoomId = value
.get("room_id")
.and_then(|id| RoomId::parse(id.as_str()?).ok())
.ok_or_else(|| err!(Request(InvalidParam("Invalid room id in pdu"))))?;
.and_then(CanonicalJsonValue::as_str)
.map(RoomId::parse)
.flat_ok_or(err!(Request(InvalidParam("Invalid room_id in pdu"))))?;
let Ok(room_version_id) = self.services.state.get_room_version(&room_id).await else {
return Err!("Server is not in room {room_id}");
};
let room_version_id = self
.services
.state
.get_room_version(&room_id)
.await
.map_err(|_| err!("Server is not in room {room_id}"))?;
let Ok((event_id, value)) = gen_event_id_canonical_json(pdu, &room_version_id) else {
// Event could not be converted to canonical json
return Err!(Request(InvalidParam("Could not convert event to canonical json.")));
};
let (event_id, value) = gen_event_id_canonical_json(pdu, &room_version_id)
.map_err(|e| err!(Request(InvalidParam("Could not convert event to canonical json: {e}"))))?;
Ok((event_id, value, room_id))
}

View file

@ -661,8 +661,7 @@ impl Service {
.await
.or_else(|_| {
if event_type == TimelineEventType::RoomCreate {
let content = serde_json::from_str::<RoomCreateEventContent>(content.get())
.expect("Invalid content in RoomCreate pdu.");
let content: RoomCreateEventContent = serde_json::from_str(content.get())?;
Ok(content.room_version)
} else {
Err(Error::InconsistentRoomState(