2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2026-02-16 04:21:54 +00:00
|
|
|
use conduwuit::{Err, Result};
|
|
|
|
|
use ruma::api::client::alias::{create_alias, delete_alias, get_alias};
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2024-07-22 07:43:51 +00:00
|
|
|
use crate::Ruma;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-01-16 21:22:47 -05:00
|
|
|
/// # `PUT /_matrix/client/v3/directory/room/{roomAlias}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
|
/// Creates a new room alias on this server.
|
2024-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn create_alias_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
|
body: Ruma<create_alias::v3::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<create_alias::v3::Response> {
|
2025-04-26 23:06:43 +00:00
|
|
|
let sender_user = body.sender_user();
|
2025-06-28 22:42:31 +01:00
|
|
|
if services.users.is_suspended(sender_user).await? {
|
|
|
|
|
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
|
|
|
|
|
}
|
2024-06-12 01:42:39 -04:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.alias
|
|
|
|
|
.appservice_checks(&body.room_alias, &body.appservice_info)
|
|
|
|
|
.await?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-01 00:01:49 -04:00
|
|
|
// this isn't apart of alias_checks or delete alias route because we should
|
|
|
|
|
// allow removing forbidden room aliases
|
2024-07-16 08:05:25 +00:00
|
|
|
if services
|
2024-03-25 17:05:11 -04:00
|
|
|
.globals
|
|
|
|
|
.forbidden_alias_names()
|
|
|
|
|
.is_match(body.room_alias.alias())
|
|
|
|
|
{
|
2024-08-08 17:18:30 +00:00
|
|
|
return Err!(Request(Forbidden("Room alias is forbidden.")));
|
2024-02-08 19:11:48 -05:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
if services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
|
.alias
|
2024-08-08 17:18:30 +00:00
|
|
|
.resolve_local_alias(&body.room_alias)
|
|
|
|
|
.await
|
|
|
|
|
.is_ok()
|
2024-03-25 17:05:11 -04:00
|
|
|
{
|
2024-08-08 17:18:30 +00:00
|
|
|
return Err!(Conflict("Alias already exists."));
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
|
.alias
|
2024-06-12 01:42:39 -04:00
|
|
|
.set_alias(&body.room_alias, &body.room_id, sender_user)?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_alias::v3::Response::new())
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-16 21:22:47 -05:00
|
|
|
/// # `DELETE /_matrix/client/v3/directory/room/{roomAlias}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
|
/// Deletes a room alias from this server.
|
|
|
|
|
///
|
|
|
|
|
/// - TODO: Update canonical alias event
|
2024-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn delete_alias_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
|
body: Ruma<delete_alias::v3::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<delete_alias::v3::Response> {
|
2025-04-26 23:06:43 +00:00
|
|
|
let sender_user = body.sender_user();
|
2025-06-28 22:42:31 +01:00
|
|
|
if services.users.is_suspended(sender_user).await? {
|
|
|
|
|
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
|
|
|
|
|
}
|
2024-06-12 01:42:39 -04:00
|
|
|
|
2024-07-18 06:37:47 +00:00
|
|
|
services
|
|
|
|
|
.rooms
|
|
|
|
|
.alias
|
|
|
|
|
.appservice_checks(&body.room_alias, &body.appservice_info)
|
|
|
|
|
.await?;
|
2024-06-12 01:42:39 -04:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
|
.alias
|
2024-06-12 01:42:39 -04:00
|
|
|
.remove_alias(&body.room_alias, sender_user)
|
|
|
|
|
.await?;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2021-08-31 19:14:37 +02:00
|
|
|
// TODO: update alt_aliases?
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(delete_alias::v3::Response::new())
|
2020-07-30 18:14:47 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 14:10:04 -05:00
|
|
|
/// # `GET /_matrix/client/v3/directory/room/{roomAlias}`
|
2021-08-31 19:14:37 +02:00
|
|
|
///
|
|
|
|
|
/// Resolve an alias locally or over federation.
|
2024-07-16 08:05:25 +00:00
|
|
|
pub(crate) async fn get_alias_route(
|
2024-12-15 00:05:47 -05:00
|
|
|
State(services): State<crate::State>,
|
|
|
|
|
body: Ruma<get_alias::v3::Request>,
|
2024-07-16 08:05:25 +00:00
|
|
|
) -> Result<get_alias::v3::Response> {
|
2024-06-23 02:55:00 +00:00
|
|
|
let room_alias = body.body.room_alias;
|
2024-04-22 01:25:28 -04:00
|
|
|
|
2026-02-16 04:21:54 +00:00
|
|
|
let Ok((room_id, servers)) = services.rooms.alias.resolve_alias(&room_alias).await else {
|
2025-03-10 12:29:54 -04:00
|
|
|
return Err!(Request(NotFound("Room with alias not found.")));
|
2020-12-08 10:33:44 +01:00
|
|
|
};
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-01 00:01:49 -04:00
|
|
|
Ok(get_alias::v3::Response::new(room_id, servers))
|
|
|
|
|
}
|