Return error instead of panic when first event is not m.room.create

This commit is contained in:
Val Lorentz 2023-12-02 17:51:19 +01:00
parent 520806d413
commit 5a7bb1e8f1
2 changed files with 23 additions and 12 deletions

View file

@ -696,18 +696,27 @@ impl Service {
.collect();
// If there was no create event yet, assume we are creating a room
let room_version_id = self.get_room_version(room_id)?.unwrap_or_else(|| {
if event_type != TimelineEventType::RoomCreate {
panic!("non-create event for room of unknown version");
}
#[derive(Deserialize)]
struct RoomCreate {
room_version: RoomVersionId,
}
let content = serde_json::from_str::<RoomCreate>(content.get())
.expect("Invalid content in RoomCreate pdu.");
content.room_version
});
let room_version_id = self
.get_room_version(room_id)?
.or_else(|| {
if event_type == TimelineEventType::RoomCreate {
#[derive(Deserialize)]
struct RoomCreate {
room_version: RoomVersionId,
}
let content = serde_json::from_str::<RoomCreate>(content.get())
.expect("Invalid content in RoomCreate pdu.");
Some(content.room_version)
} else {
None
}
})
.ok_or_else(|| {
Error::InconsistentRoomState(
"non-create event for room of unknown version",
room_id.to_owned(),
)
})?;
let room_version = RoomVersion::new(&room_version_id).expect("room version is supported");

View file

@ -82,6 +82,8 @@ pub enum Error {
PathError(#[from] axum::extract::rejection::PathRejection),
#[error("from {0}: {1}")]
RedactionError(OwnedServerName, ruma::canonical_json::RedactionError),
#[error("{0} in {1}")]
InconsistentRoomState(&'static str, ruma::OwnedRoomId),
}
impl Error {