2024-08-01 10:58:27 +00:00
|
|
|
use conduit::{debug_warn, err, pdu::gen_event_id_canonical_json, Err, Result};
|
|
|
|
|
use ruma::{CanonicalJsonObject, OwnedEventId, OwnedRoomId, RoomId};
|
2024-05-09 15:59:08 -07:00
|
|
|
use serde_json::value::RawValue as RawJsonValue;
|
|
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
impl super::Service {
|
|
|
|
|
pub fn parse_incoming_pdu(&self, pdu: &RawJsonValue) -> Result<(OwnedEventId, CanonicalJsonObject, OwnedRoomId)> {
|
|
|
|
|
let value: CanonicalJsonObject = serde_json::from_str(pdu.get()).map_err(|e| {
|
2024-08-01 10:58:27 +00:00
|
|
|
debug_warn!("Error parsing incoming event {pdu:#?}");
|
|
|
|
|
err!(BadServerResponse("Error parsing incoming event {e:?}"))
|
2024-07-18 06:37:47 +00:00
|
|
|
})?;
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
let room_id: OwnedRoomId = value
|
|
|
|
|
.get("room_id")
|
|
|
|
|
.and_then(|id| RoomId::parse(id.as_str()?).ok())
|
2024-08-01 10:58:27 +00:00
|
|
|
.ok_or(err!(Request(InvalidParam("Invalid room id in pdu"))))?;
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
let Ok(room_version_id) = self.services.state.get_room_version(&room_id) else {
|
|
|
|
|
return Err!("Server is not in room {room_id}");
|
|
|
|
|
};
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
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.")));
|
|
|
|
|
};
|
2024-05-09 15:59:08 -07:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
Ok((event_id, value, room_id))
|
|
|
|
|
}
|
2024-05-09 15:59:08 -07:00
|
|
|
}
|