mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-06-27 18:14:51 +02:00
combine service/users data w/ mod unit split sliding sync related out of service/users instrument database entry points remove increment crap from database interface de-wrap all database get() calls de-wrap all database insert() calls de-wrap all database remove() calls refactor database interface for async streaming add query key serializer for database implement Debug for result handle add query deserializer for database add deserialization trait for option handle start a stream utils suite de-wrap/asyncify/type-query count_one_time_keys() de-wrap/asyncify users count add admin query users command suite de-wrap/asyncify users exists de-wrap/partially asyncify user filter related asyncify/de-wrap users device/keys related asyncify/de-wrap user auth/misc related asyncify/de-wrap users blurhash asyncify/de-wrap account_data get; merge Data into Service partial asyncify/de-wrap uiaa; merge Data into Service partially asyncify/de-wrap transaction_ids get; merge Data into Service partially asyncify/de-wrap key_backups; merge Data into Service asyncify/de-wrap pusher service getters; merge Data into Service asyncify/de-wrap rooms alias getters/some iterators asyncify/de-wrap rooms directory getters/iterator partially asyncify/de-wrap rooms lazy-loading partially asyncify/de-wrap rooms metadata asyncify/dewrap rooms outlier asyncify/dewrap rooms pdu_metadata dewrap/partially asyncify rooms read receipt de-wrap rooms search service de-wrap/partially asyncify rooms user service partial de-wrap rooms state_compressor de-wrap rooms state_cache de-wrap room state et al de-wrap rooms timeline service additional users device/keys related de-wrap/asyncify sender asyncify services refactor database to TryFuture/TryStream refactor services for TryFuture/TryStream asyncify api handlers additional asyncification for admin module abstract stream related; support reverse streams additional stream conversions asyncify state-res related Signed-off-by: Jason Volk <jason@zemos.net>
147 lines
3.9 KiB
Rust
147 lines
3.9 KiB
Rust
use axum::extract::State;
|
|
use conduit::{debug, Err, Result};
|
|
use futures::StreamExt;
|
|
use rand::seq::SliceRandom;
|
|
use ruma::{
|
|
api::client::alias::{create_alias, delete_alias, get_alias},
|
|
OwnedServerName, RoomAliasId, RoomId,
|
|
};
|
|
use service::Services;
|
|
|
|
use crate::Ruma;
|
|
|
|
/// # `PUT /_matrix/client/v3/directory/room/{roomAlias}`
|
|
///
|
|
/// Creates a new room alias on this server.
|
|
pub(crate) async fn create_alias_route(
|
|
State(services): State<crate::State>, body: Ruma<create_alias::v3::Request>,
|
|
) -> Result<create_alias::v3::Response> {
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
services
|
|
.rooms
|
|
.alias
|
|
.appservice_checks(&body.room_alias, &body.appservice_info)
|
|
.await?;
|
|
|
|
// this isn't apart of alias_checks or delete alias route because we should
|
|
// allow removing forbidden room aliases
|
|
if services
|
|
.globals
|
|
.forbidden_alias_names()
|
|
.is_match(body.room_alias.alias())
|
|
{
|
|
return Err!(Request(Forbidden("Room alias is forbidden.")));
|
|
}
|
|
|
|
if services
|
|
.rooms
|
|
.alias
|
|
.resolve_local_alias(&body.room_alias)
|
|
.await
|
|
.is_ok()
|
|
{
|
|
return Err!(Conflict("Alias already exists."));
|
|
}
|
|
|
|
services
|
|
.rooms
|
|
.alias
|
|
.set_alias(&body.room_alias, &body.room_id, sender_user)?;
|
|
|
|
Ok(create_alias::v3::Response::new())
|
|
}
|
|
|
|
/// # `DELETE /_matrix/client/v3/directory/room/{roomAlias}`
|
|
///
|
|
/// Deletes a room alias from this server.
|
|
///
|
|
/// - TODO: Update canonical alias event
|
|
pub(crate) async fn delete_alias_route(
|
|
State(services): State<crate::State>, body: Ruma<delete_alias::v3::Request>,
|
|
) -> Result<delete_alias::v3::Response> {
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
services
|
|
.rooms
|
|
.alias
|
|
.appservice_checks(&body.room_alias, &body.appservice_info)
|
|
.await?;
|
|
|
|
services
|
|
.rooms
|
|
.alias
|
|
.remove_alias(&body.room_alias, sender_user)
|
|
.await?;
|
|
|
|
// TODO: update alt_aliases?
|
|
|
|
Ok(delete_alias::v3::Response::new())
|
|
}
|
|
|
|
/// # `GET /_matrix/client/v3/directory/room/{roomAlias}`
|
|
///
|
|
/// Resolve an alias locally or over federation.
|
|
pub(crate) async fn get_alias_route(
|
|
State(services): State<crate::State>, body: Ruma<get_alias::v3::Request>,
|
|
) -> Result<get_alias::v3::Response> {
|
|
let room_alias = body.body.room_alias;
|
|
let servers = None;
|
|
|
|
let Ok((room_id, pre_servers)) = services
|
|
.rooms
|
|
.alias
|
|
.resolve_alias(&room_alias, servers.as_ref())
|
|
.await
|
|
else {
|
|
return Err!(Request(NotFound("Room with alias not found.")));
|
|
};
|
|
|
|
let servers = room_available_servers(&services, &room_id, &room_alias, &pre_servers).await;
|
|
debug!(?room_alias, ?room_id, "available servers: {servers:?}");
|
|
|
|
Ok(get_alias::v3::Response::new(room_id, servers))
|
|
}
|
|
|
|
async fn room_available_servers(
|
|
services: &Services, room_id: &RoomId, room_alias: &RoomAliasId, pre_servers: &Option<Vec<OwnedServerName>>,
|
|
) -> Vec<OwnedServerName> {
|
|
// find active servers in room state cache to suggest
|
|
let mut servers: Vec<OwnedServerName> = services
|
|
.rooms
|
|
.state_cache
|
|
.room_servers(room_id)
|
|
.map(ToOwned::to_owned)
|
|
.collect()
|
|
.await;
|
|
|
|
// 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());
|
|
};
|
|
|
|
servers.sort_unstable();
|
|
servers.dedup();
|
|
|
|
// shuffle list of servers randomly after sort and dedupe
|
|
servers.shuffle(&mut rand::thread_rng());
|
|
|
|
// insert our server as the very first choice if in list, else check if we can
|
|
// prefer the room alias server first
|
|
if let Some(server_index) = servers
|
|
.iter()
|
|
.position(|server_name| services.globals.server_is_ours(server_name))
|
|
{
|
|
servers.swap_remove(server_index);
|
|
servers.insert(0, services.globals.server_name().to_owned());
|
|
} else if let Some(alias_server_index) = servers
|
|
.iter()
|
|
.position(|server| server == room_alias.server_name())
|
|
{
|
|
servers.swap_remove(alias_server_index);
|
|
servers.insert(0, room_alias.server_name().into());
|
|
}
|
|
|
|
servers
|
|
}
|