Files
continuwuity/src/api/client/alias.rs
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
2.5 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
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
/// # `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(
State(services): State<crate::State>,
body: Ruma<create_alias::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<create_alias::v3::Response> {
let sender_user = body.identity.sender_user();
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.identity.appservice_info())
2024-07-18 06:37:47 +00:00
.await?;
2024-03-05 19:48:54 -05: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-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
}
/// # `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(
State(services): State<crate::State>,
body: Ruma<delete_alias::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<delete_alias::v3::Response> {
let sender_user = body.identity.sender_user();
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.identity.appservice_info())
2024-07-18 06:37:47 +00:00
.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
}
/// # `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(
State(services): State<crate::State>,
body: Ruma<get_alias::v3::Request>,
2024-07-16 08:05:25 +00:00
) -> Result<get_alias::v3::Response> {
let room_alias = body.body.room_alias;
2024-04-22 01:25:28 -04:00
let Ok((room_id, servers)) = services.rooms.alias.resolve_alias(&room_alias).await else {
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
Ok(get_alias::v3::Response::new(room_id, servers))
}