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.

105 lines
2.7 KiB
Rust
Raw Normal View History

2024-07-16 08:05:25 +00:00
use axum::extract::State;
2024-07-22 07:43:51 +00:00
use conduit::{Error, Result};
2024-06-05 04:32:58 +00:00
use get_profile_information::v1::ProfileField;
use rand::seq::SliceRandom;
use ruma::{
api::{
client::error::ErrorKind,
federation::query::{get_profile_information, get_room_information},
},
OwnedServerName,
};
2024-07-22 07:43:51 +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(
2024-07-16 08:05:25 +00:00
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
.resolve_local_alias(&body.room_alias)?
.ok_or_else(|| Error::BadRequest(ErrorKind::NotFound, "Room alias not found."))?;
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)
.filter_map(Result::ok)
.collect();
servers.sort_unstable();
servers.dedup();
servers.shuffle(&mut rand::thread_rng());
// 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
}
Ok(get_room_information::v1::Response {
room_id,
servers,
})
}
/// # `GET /_matrix/federation/v1/query/profile`
///
///
/// Gets information on a profile.
pub(crate) async fn get_profile_information_route(
2024-07-16 08:05:25 +00:00
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> {
2024-07-16 08:05:25 +00:00
if !services.globals.allow_profile_lookup_federation_requests() {
2024-06-05 04:32:58 +00:00
return Err(Error::BadRequest(
ErrorKind::forbidden(),
"Profile lookup over federation is not allowed on this homeserver.",
));
}
2024-07-22 07:43:51 +00:00
if !services.globals.server_is_ours(body.user_id.server_name()) {
2024-06-05 04:32:58 +00:00
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"User does not belong to this server.",
));
}
let mut displayname = None;
let mut avatar_url = None;
let mut blurhash = None;
let mut tz = None;
2024-06-05 04:32:58 +00:00
match &body.field {
Some(ProfileField::DisplayName) => {
2024-07-16 08:05:25 +00:00
displayname = services.users.displayname(&body.user_id)?;
2024-06-05 04:32:58 +00:00
},
Some(ProfileField::AvatarUrl) => {
2024-07-16 08:05:25 +00:00
avatar_url = services.users.avatar_url(&body.user_id)?;
blurhash = services.users.blurhash(&body.user_id)?;
2024-06-05 04:32:58 +00:00
},
// TODO: what to do with custom
Some(_) => {},
None => {
2024-07-16 08:05:25 +00:00
displayname = services.users.displayname(&body.user_id)?;
avatar_url = services.users.avatar_url(&body.user_id)?;
blurhash = services.users.blurhash(&body.user_id)?;
tz = services.users.timezone(&body.user_id)?;
2024-06-05 04:32:58 +00:00
},
}
Ok(get_profile_information::v1::Response {
displayname,
avatar_url,
blurhash,
tz,
2024-06-05 04:32:58 +00:00
})
}