2024-07-16 08:05:25 +00:00
|
|
|
use axum::extract::State;
|
2024-08-08 17:18:30 +00:00
|
|
|
use conduit::{debug, Err, Result};
|
|
|
|
use futures::StreamExt;
|
2023-06-28 17:41:55 +02:00
|
|
|
use rand::seq::SliceRandom;
|
2020-08-20 12:12:02 -04:00
|
|
|
use ruma::{
|
2024-08-08 17:18:30 +00:00
|
|
|
api::client::alias::{create_alias, delete_alias, get_alias},
|
2024-06-23 02:55:00 +00:00
|
|
|
OwnedServerName, RoomAliasId, RoomId,
|
2020-08-14 11:34:15 +02:00
|
|
|
};
|
2024-07-22 07:43:51 +00:00
|
|
|
use service::Services;
|
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(
|
|
|
|
State(services): State<crate::State>, body: Ruma<create_alias::v3::Request>,
|
|
|
|
) -> Result<create_alias::v3::Response> {
|
2024-06-12 01:42:39 -04:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
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(
|
|
|
|
State(services): State<crate::State>, body: Ruma<delete_alias::v3::Request>,
|
|
|
|
) -> Result<delete_alias::v3::Response> {
|
2024-06-12 01:42:39 -04:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
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(
|
|
|
|
State(services): State<crate::State>, body: Ruma<get_alias::v3::Request>,
|
|
|
|
) -> Result<get_alias::v3::Response> {
|
2024-06-23 02:55:00 +00:00
|
|
|
let room_alias = body.body.room_alias;
|
|
|
|
let servers = None;
|
2024-04-22 01:25:28 -04:00
|
|
|
|
2024-07-16 08:05:25 +00:00
|
|
|
let Ok((room_id, pre_servers)) = services
|
2024-06-23 02:55:00 +00:00
|
|
|
.rooms
|
|
|
|
.alias
|
|
|
|
.resolve_alias(&room_alias, servers.as_ref())
|
|
|
|
.await
|
|
|
|
else {
|
2024-08-08 17:18:30 +00: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-08-08 17:18:30 +00:00
|
|
|
let servers = room_available_servers(&services, &room_id, &room_alias, &pre_servers).await;
|
2024-06-23 02:55:00 +00:00
|
|
|
debug!(?room_alias, ?room_id, "available servers: {servers:?}");
|
2024-05-01 00:01:49 -04:00
|
|
|
|
|
|
|
Ok(get_alias::v3::Response::new(room_id, servers))
|
|
|
|
}
|
|
|
|
|
2024-08-08 17:18:30 +00:00
|
|
|
async fn room_available_servers(
|
2024-07-16 08:05:25 +00:00
|
|
|
services: &Services, room_id: &RoomId, room_alias: &RoomAliasId, pre_servers: &Option<Vec<OwnedServerName>>,
|
2024-05-01 00:01:49 -04:00
|
|
|
) -> Vec<OwnedServerName> {
|
2024-01-13 14:10:04 -05:00
|
|
|
// find active servers in room state cache to suggest
|
2024-07-16 08:05:25 +00:00
|
|
|
let mut servers: Vec<OwnedServerName> = services
|
2024-03-25 17:05:11 -04:00
|
|
|
.rooms
|
|
|
|
.state_cache
|
2024-05-01 00:01:49 -04:00
|
|
|
.room_servers(room_id)
|
2024-08-08 17:18:30 +00:00
|
|
|
.map(ToOwned::to_owned)
|
|
|
|
.collect()
|
|
|
|
.await;
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-01 00:01:49 -04:00
|
|
|
// push any servers we want in the list already (e.g. responded remote alias
|
|
|
|
// servers, room alias server itself)
|
|
|
|
if let Some(pre_servers) = pre_servers {
|
|
|
|
servers.extend(pre_servers.clone());
|
|
|
|
};
|
|
|
|
|
2024-03-27 14:44:59 -04:00
|
|
|
servers.sort_unstable();
|
|
|
|
servers.dedup();
|
|
|
|
|
|
|
|
// shuffle list of servers randomly after sort and dedupe
|
|
|
|
servers.shuffle(&mut rand::thread_rng());
|
|
|
|
|
2024-05-01 00:01:49 -04:00
|
|
|
// insert our server as the very first choice if in list, else check if we can
|
|
|
|
// prefer the room alias server first
|
2024-03-25 17:05:11 -04:00
|
|
|
if let Some(server_index) = servers
|
2024-03-27 14:47:17 -04:00
|
|
|
.iter()
|
2024-07-22 07:43:51 +00:00
|
|
|
.position(|server_name| services.globals.server_is_ours(server_name))
|
2024-01-13 14:10:04 -05:00
|
|
|
{
|
2024-06-01 00:22:50 -04:00
|
|
|
servers.swap_remove(server_index);
|
2024-07-16 08:05:25 +00:00
|
|
|
servers.insert(0, services.globals.server_name().to_owned());
|
2024-05-01 00:01:49 -04:00
|
|
|
} else if let Some(alias_server_index) = servers
|
|
|
|
.iter()
|
|
|
|
.position(|server| server == room_alias.server_name())
|
|
|
|
{
|
2024-06-01 00:22:50 -04:00
|
|
|
servers.swap_remove(alias_server_index);
|
2024-05-01 00:01:49 -04:00
|
|
|
servers.insert(0, room_alias.server_name().into());
|
2024-01-13 14:10:04 -05:00
|
|
|
}
|
2024-03-05 19:48:54 -05:00
|
|
|
|
2024-05-01 00:01:49 -04:00
|
|
|
servers
|
|
|
|
}
|