From 9a3866afffa2591b6623ed928a36fa9ccd4feca0 Mon Sep 17 00:00:00 2001 From: strawberry Date: Tue, 16 Jan 2024 21:22:47 -0500 Subject: [PATCH] return `M_INVALID_PARAM` instead of `M_BAD_JSON` for bad room aliases Signed-off-by: strawberry --- src/api/client_server/alias.rs | 39 +++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/api/client_server/alias.rs b/src/api/client_server/alias.rs index 1c3ff231..b0f5a289 100644 --- a/src/api/client_server/alias.rs +++ b/src/api/client_server/alias.rs @@ -13,7 +13,7 @@ use ruma::{ OwnedRoomAliasId, OwnedServerName, }; -/// # `PUT /_matrix/client/r0/directory/room/{roomAlias}` +/// # `PUT /_matrix/client/v3/directory/room/{roomAlias}` /// /// Creates a new room alias on this server. pub async fn create_alias_route( @@ -35,15 +35,22 @@ pub async fn create_alias_route( return Err(Error::Conflict("Alias already exists.")); } - services() + if services() .rooms .alias - .set_alias(&body.room_alias, &body.room_id)?; + .set_alias(&body.room_alias, &body.room_id) + .is_err() + { + return Err(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid room alias. Alias must be in the form of '#localpart:server_name'", + )); + }; Ok(create_alias::v3::Response::new()) } -/// # `DELETE /_matrix/client/r0/directory/room/{roomAlias}` +/// # `DELETE /_matrix/client/v3/directory/room/{roomAlias}` /// /// Deletes a room alias from this server. /// @@ -59,7 +66,29 @@ pub async fn delete_alias_route( )); } - services().rooms.alias.remove_alias(&body.room_alias)?; + if services() + .rooms + .alias + .resolve_local_alias(&body.room_alias)? + .is_none() + { + return Err(Error::BadRequest( + ErrorKind::NotFound, + "Alias does not exist.", + )); + } + + if services() + .rooms + .alias + .remove_alias(&body.room_alias) + .is_err() + { + return Err(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid room alias. Alias must be in the form of '#localpart:server_name'", + )); + }; // TODO: update alt_aliases?