mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
refactor: Fix errors in api/server/query.rs
This commit is contained in:
+28
-21
@@ -96,26 +96,7 @@ async fn fetch_full_profile(
|
|||||||
) -> Option<BTreeMap<String, Value>> {
|
) -> Option<BTreeMap<String, Value>> {
|
||||||
// If the user exists locally, fetch their local profile
|
// If the user exists locally, fetch their local profile
|
||||||
if services.users.exists(user_id).await {
|
if services.users.exists(user_id).await {
|
||||||
let mut profile = BTreeMap::new();
|
return Some(get_local_profile(services, user_id).await);
|
||||||
|
|
||||||
// Get displayname and avatar_url independently because `all_profile_keys`
|
|
||||||
// doesn't include them
|
|
||||||
for field in [ProfileFieldName::AvatarUrl, ProfileFieldName::DisplayName] {
|
|
||||||
let key = field.as_str().to_owned();
|
|
||||||
|
|
||||||
if let Some(value) = get_local_profile_field(services, user_id, field).await {
|
|
||||||
profile.insert(key, value.value().into_owned());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert all other profile fields
|
|
||||||
let mut all_fields = services.users.all_profile_keys(user_id);
|
|
||||||
|
|
||||||
while let Some((key, value)) = all_fields.next().await {
|
|
||||||
profile.insert(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Some(profile);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise ask their homeserver
|
// Otherwise ask their homeserver
|
||||||
@@ -188,7 +169,33 @@ async fn fetch_profile_field(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_local_profile_field(
|
pub(crate) async fn get_local_profile(
|
||||||
|
services: &Services,
|
||||||
|
user_id: &UserId,
|
||||||
|
) -> BTreeMap<String, Value> {
|
||||||
|
let mut profile = BTreeMap::new();
|
||||||
|
|
||||||
|
// Get displayname and avatar_url independently because `all_profile_keys`
|
||||||
|
// doesn't include them
|
||||||
|
for field in [ProfileFieldName::AvatarUrl, ProfileFieldName::DisplayName] {
|
||||||
|
let key = field.as_str().to_owned();
|
||||||
|
|
||||||
|
if let Some(value) = get_local_profile_field(services, user_id, field).await {
|
||||||
|
profile.insert(key, value.value().into_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert all other profile fields
|
||||||
|
let mut all_fields = services.users.all_profile_keys(user_id);
|
||||||
|
|
||||||
|
while let Some((key, value)) = all_fields.next().await {
|
||||||
|
profile.insert(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn get_local_profile_field(
|
||||||
services: &Services,
|
services: &Services,
|
||||||
user_id: &UserId,
|
user_id: &UserId,
|
||||||
field: ProfileFieldName,
|
field: ProfileFieldName,
|
||||||
|
|||||||
+27
-57
@@ -1,19 +1,16 @@
|
|||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use conduwuit::{Error, Result, err};
|
use conduwuit::{Err, Result, err};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use get_profile_information::v1::ProfileField;
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use ruma::{
|
use ruma::{
|
||||||
OwnedServerName,
|
OwnedServerName,
|
||||||
api::{
|
api::federation::query::{get_profile_information, get_room_information},
|
||||||
client::error::ErrorKind,
|
|
||||||
federation::query::{get_profile_information, get_room_information},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::Ruma;
|
use crate::{
|
||||||
|
Ruma,
|
||||||
|
client::{get_local_profile, get_local_profile_field},
|
||||||
|
};
|
||||||
|
|
||||||
/// # `GET /_matrix/federation/v1/query/directory`
|
/// # `GET /_matrix/federation/v1/query/directory`
|
||||||
///
|
///
|
||||||
@@ -33,7 +30,6 @@ pub(crate) async fn get_room_information_route(
|
|||||||
.rooms
|
.rooms
|
||||||
.state_cache
|
.state_cache
|
||||||
.room_servers(&room_id)
|
.room_servers(&room_id)
|
||||||
.map(ToOwned::to_owned)
|
|
||||||
.collect()
|
.collect()
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@@ -51,7 +47,7 @@ pub(crate) async fn get_room_information_route(
|
|||||||
servers.insert(0, services.globals.server_name().to_owned());
|
servers.insert(0, services.globals.server_name().to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(get_room_information::v1::Response { room_id, servers })
|
Ok(get_room_information::v1::Response::new(room_id, servers))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// # `GET /_matrix/federation/v1/query/profile`
|
/// # `GET /_matrix/federation/v1/query/profile`
|
||||||
@@ -67,57 +63,31 @@ pub(crate) async fn get_profile_information_route(
|
|||||||
.config
|
.config
|
||||||
.allow_inbound_profile_lookup_federation_requests
|
.allow_inbound_profile_lookup_federation_requests
|
||||||
{
|
{
|
||||||
return Err(Error::BadRequest(
|
return Err!(Request(Forbidden(
|
||||||
ErrorKind::forbidden(),
|
"Profile lookup over federation is not allowed on this homeserver."
|
||||||
"Profile lookup over federation is not allowed on this homeserver.",
|
)));
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !services.globals.server_is_ours(body.user_id.server_name()) {
|
if !services.globals.server_is_ours(body.user_id.server_name()) {
|
||||||
return Err(Error::BadRequest(
|
return Err!(Request(InvalidParam("User does not belong to this server.")));
|
||||||
ErrorKind::InvalidParam,
|
|
||||||
"User does not belong to this server.",
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut displayname = None;
|
let response = if let Some(field) = &body.field {
|
||||||
let mut avatar_url = None;
|
let mut response = get_profile_information::v1::Response::new();
|
||||||
let mut blurhash = None;
|
|
||||||
let mut custom_profile_fields = BTreeMap::new();
|
|
||||||
|
|
||||||
match &body.field {
|
if let Some(value) =
|
||||||
| Some(ProfileField::DisplayName) => {
|
get_local_profile_field(&services, &body.user_id, field.to_owned()).await
|
||||||
displayname = services.users.displayname(&body.user_id).await.ok();
|
{
|
||||||
},
|
response.set(value.field_name().as_str().to_owned(), value.value().into_owned());
|
||||||
| Some(ProfileField::AvatarUrl) => {
|
}
|
||||||
avatar_url = services.users.avatar_url(&body.user_id).await.ok();
|
|
||||||
blurhash = services.users.blurhash(&body.user_id).await.ok();
|
|
||||||
},
|
|
||||||
| Some(custom_field) => {
|
|
||||||
if let Ok(value) = services
|
|
||||||
.users
|
|
||||||
.profile_key(&body.user_id, custom_field.as_str())
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
custom_profile_fields.insert(custom_field.to_string(), value);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
| None => {
|
|
||||||
displayname = services.users.displayname(&body.user_id).await.ok();
|
|
||||||
avatar_url = services.users.avatar_url(&body.user_id).await.ok();
|
|
||||||
blurhash = services.users.blurhash(&body.user_id).await.ok();
|
|
||||||
custom_profile_fields = services
|
|
||||||
.users
|
|
||||||
.all_profile_keys(&body.user_id)
|
|
||||||
.collect()
|
|
||||||
.await;
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(get_profile_information::v1::Response {
|
response
|
||||||
displayname,
|
} else {
|
||||||
avatar_url,
|
get_local_profile(&services, &body.user_id)
|
||||||
blurhash,
|
.await
|
||||||
custom_profile_fields,
|
.into_iter()
|
||||||
})
|
.collect()
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user