add custom room ID support using room_id field

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-01-24 12:21:18 -05:00 committed by June
parent 2980af6490
commit 382347353e

View file

@ -23,11 +23,12 @@ use ruma::{
}, },
int, int,
serde::JsonObject, serde::JsonObject,
CanonicalJsonObject, OwnedRoomAliasId, RoomAliasId, RoomId, RoomVersionId, CanonicalJsonObject, CanonicalJsonValue, OwnedRoomAliasId, OwnedRoomId, RoomAliasId, RoomId,
RoomVersionId,
}; };
use serde_json::{json, value::to_raw_value}; use serde_json::{json, value::to_raw_value};
use std::{cmp::max, collections::BTreeMap, sync::Arc}; use std::{cmp::max, collections::BTreeMap, sync::Arc};
use tracing::{info, warn}; use tracing::{error, info, warn};
/// # `POST /_matrix/client/v3/createRoom` /// # `POST /_matrix/client/v3/createRoom`
/// ///
@ -62,6 +63,34 @@ pub async fn create_room_route(
)); ));
} }
let room_id: OwnedRoomId;
// checks if the user specified an explicit (custom) room_id to be created with in request body.
// falls back to normal generated room ID if not specified.
if let Some(CanonicalJsonValue::Object(json_body)) = &body.json_body {
match json_body.get("room_id") {
Some(custom_room_id) => {
room_id = RoomId::parse(format!(
"!{}:{}",
custom_room_id,
services().globals.server_name()
))
.map_err(|e| {
info!(
"User attempted to create room with custom room ID but failed parsing: {}",
e
);
Error::BadRequest(
ErrorKind::InvalidParam,
"Custom room ID could not be parsed",
)
})?;
}
None => room_id = RoomId::new(services().globals.server_name()),
}
} else {
room_id = RoomId::new(services().globals.server_name())
}
services().rooms.short.get_or_create_shortroomid(&room_id)?; services().rooms.short.get_or_create_shortroomid(&room_id)?;