From a9a18fc5f07c55f2f59289df5ddccdfebeab7793 Mon Sep 17 00:00:00 2001 From: Ginger Date: Wed, 22 Apr 2026 10:41:01 -0400 Subject: [PATCH] fix: Re-add support for custom room IDs --- src/api/client/room/create.rs | 22 ++++++++++++++++------ src/api/router/args.rs | 6 +++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/api/client/room/create.rs b/src/api/client/room/create.rs index 1eb5e432c..6cb907871 100644 --- a/src/api/client/room/create.rs +++ b/src/api/client/room/create.rs @@ -9,8 +9,8 @@ use conduwuit_service::{Services, appservice::RegistrationInfo}; use futures::FutureExt; use ruma::{ - CanonicalJsonObject, Int, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId, OwnedRoomId, - OwnedUserId, RoomAliasId, RoomId, RoomVersionId, UserId, + CanonicalJsonObject, CanonicalJsonValue, Int, MilliSecondsSinceUnixEpoch, OwnedRoomAliasId, + OwnedRoomId, OwnedUserId, RoomAliasId, RoomId, RoomVersionId, UserId, api::client::room::{self, create_room}, assign, events::{ @@ -87,7 +87,19 @@ pub(crate) async fn create_room_route( let room_version_rules = room_version.rules().unwrap(); let room_id: Option = match room_version_rules.room_id_format { - | RoomIdFormatVersion::V1 => Some(RoomId::new_v1(services.globals.server_name())), + | RoomIdFormatVersion::V1 => { + // Check for custom room ID field + if let Some(CanonicalJsonValue::String(room_id)) = + body.json_body.as_ref().unwrap().get("room_id") + { + Some( + RoomId::parse(room_id) + .map_err(|_| err!(Request(BadJson("Malformed custom room ID"))))?, + ) + } else { + Some(RoomId::new_v1(services.globals.server_name())) + } + }, | _ => None, }; @@ -235,10 +247,8 @@ pub(crate) async fn create_room_route( .json_body .as_ref() .unwrap() - .as_object() - .unwrap() .get("origin_server_ts") - .and_then(ruma::CanonicalJsonValue::as_integer) + .and_then(CanonicalJsonValue::as_integer) .map(Into::into) .and_then(|value: i64| value.try_into().ok()) .map(MilliSecondsSinceUnixEpoch); diff --git a/src/api/router/args.rs b/src/api/router/args.rs index 26927504b..c7a57b918 100644 --- a/src/api/router/args.rs +++ b/src/api/router/args.rs @@ -7,7 +7,7 @@ }; use conduwuit::{Error, Result, err}; use ruma::{ - CanonicalJsonValue, DeviceId, OwnedDeviceId, OwnedServerName, OwnedUserId, ServerName, + CanonicalJsonObject, DeviceId, OwnedDeviceId, OwnedServerName, OwnedUserId, ServerName, UserId, api::IncomingRequest, }; use serde::Deserialize; @@ -47,7 +47,7 @@ pub(crate) struct Args { /// Parsed JSON content. /// None when body is not a valid string - pub(crate) json_body: Option, + pub(crate) json_body: Option, } impl Args @@ -127,7 +127,7 @@ async fn from_request( }; // Make a JSON copy of the body for use in handlers - let json_body = serde_json::from_slice::(&body).ok(); + let json_body = serde_json::from_slice::(&body).ok(); // Extract the query parameters and path let Path(path): Path> = parts.extract().await?;