Files
continuwuity/src/api/server/query.rs
T

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

94 lines
2.3 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
2026-04-13 12:24:15 -04:00
use conduwuit::{Err, Result, err};
2024-08-08 17:18:30 +00:00
use futures::StreamExt;
2024-06-05 04:32:58 +00:00
use rand::seq::SliceRandom;
use ruma::{
OwnedServerName,
2026-04-13 12:24:15 -04:00
api::federation::query::{get_profile_information, get_room_information},
2024-06-05 04:32:58 +00:00
};
use crate::Ruma;
2024-06-05 04:32:58 +00:00
/// # `GET /_matrix/federation/v1/query/directory`
///
/// Resolve a room alias to a room id.
pub(crate) async fn get_room_information_route(
State(services): State<crate::State>,
body: Ruma<get_room_information::v1::Request>,
2024-06-05 04:32:58 +00:00
) -> Result<get_room_information::v1::Response> {
2024-07-16 08:05:25 +00:00
let room_id = services
2024-06-05 04:32:58 +00:00
.rooms
.alias
2024-08-08 17:18:30 +00:00
.resolve_local_alias(&body.room_alias)
.await
.map_err(|_| err!(Request(NotFound("Room alias not found."))))?;
2024-06-05 04:32:58 +00:00
2024-07-16 08:05:25 +00:00
let mut servers: Vec<OwnedServerName> = services
2024-06-05 04:32:58 +00:00
.rooms
.state_cache
.room_servers(&room_id)
2024-08-08 17:18:30 +00:00
.collect()
.await;
2024-06-05 04:32:58 +00:00
servers.sort_unstable();
servers.dedup();
2026-02-20 22:57:45 +00:00
servers.shuffle(&mut rand::rng());
2024-06-05 04:32:58 +00:00
// insert our server as the very first choice if in list
if let Some(server_index) = servers
.iter()
2024-07-16 08:05:25 +00:00
.position(|server| server == services.globals.server_name())
2024-06-05 04:32:58 +00:00
{
servers.swap_remove(server_index);
2024-07-16 08:05:25 +00:00
servers.insert(0, services.globals.server_name().to_owned());
2024-06-05 04:32:58 +00:00
}
2026-04-13 12:24:15 -04:00
Ok(get_room_information::v1::Response::new(room_id, servers))
2024-06-05 04:32:58 +00:00
}
/// # `GET /_matrix/federation/v1/query/profile`
///
///
/// Gets information on a profile.
pub(crate) async fn get_profile_information_route(
State(services): State<crate::State>,
body: Ruma<get_profile_information::v1::Request>,
2024-06-05 04:32:58 +00:00
) -> Result<get_profile_information::v1::Response> {
if !services
.server
.config
.allow_inbound_profile_lookup_federation_requests
{
2026-04-13 12:24:15 -04:00
return Err!(Request(Forbidden(
"Profile lookup over federation is not allowed on this homeserver."
)));
2024-06-05 04:32:58 +00:00
}
2024-07-22 07:43:51 +00:00
if !services.globals.server_is_ours(body.user_id.server_name()) {
2026-04-13 12:24:15 -04:00
return Err!(Request(InvalidParam("User does not belong to this server.")));
2024-06-05 04:32:58 +00:00
}
2026-04-13 12:24:15 -04:00
let response = if let Some(field) = &body.field {
let mut response = get_profile_information::v1::Response::new();
2024-06-05 04:32:58 +00:00
if let Some(value) = services
.users
.get_local_profile_field(&body.user_id, field.to_owned())
.await
2026-04-13 12:24:15 -04:00
{
response.set(value.field_name().as_str().to_owned(), value.value().into_owned());
}
2024-06-05 04:32:58 +00:00
2026-04-13 12:24:15 -04:00
response
} else {
services
.users
.get_local_profile(&body.user_id)
2026-04-13 12:24:15 -04:00
.await
.into_iter()
.collect()
};
Ok(response)
2024-06-05 04:32:58 +00:00
}