feat: call /query/profile over federation when local user asks

next
Timo Kösters 2021-07-20 19:49:47 +02:00
parent 728e176a06
commit e20f55970e
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4
1 changed files with 67 additions and 4 deletions

View File

@ -1,11 +1,14 @@
use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma}; use crate::{database::DatabaseGuard, pdu::PduBuilder, utils, ConduitResult, Error, Ruma};
use ruma::{ use ruma::{
api::client::{ api::{
client::{
error::ErrorKind, error::ErrorKind,
r0::profile::{ r0::profile::{
get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name, get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name,
}, },
}, },
federation::{self, query::get_profile_information::v1::ProfileField},
},
events::EventType, events::EventType,
serde::Raw, serde::Raw,
}; };
@ -120,6 +123,25 @@ pub async fn get_displayname_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<get_display_name::Request<'_>>, body: Ruma<get_display_name::Request<'_>>,
) -> ConduitResult<get_display_name::Response> { ) -> ConduitResult<get_display_name::Response> {
if body.user_id.server_name() != db.globals.server_name() {
let response = db
.sending
.send_federation_request(
&db.globals,
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: &body.user_id,
field: Some(&ProfileField::DisplayName),
},
)
.await?;
return Ok(get_display_name::Response {
displayname: response.displayname,
}
.into());
}
Ok(get_display_name::Response { Ok(get_display_name::Response {
displayname: db.users.displayname(&body.user_id)?, displayname: db.users.displayname(&body.user_id)?,
} }
@ -234,6 +256,26 @@ pub async fn get_avatar_url_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<get_avatar_url::Request<'_>>, body: Ruma<get_avatar_url::Request<'_>>,
) -> ConduitResult<get_avatar_url::Response> { ) -> ConduitResult<get_avatar_url::Response> {
if body.user_id.server_name() != db.globals.server_name() {
let response = db
.sending
.send_federation_request(
&db.globals,
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: &body.user_id,
field: Some(&ProfileField::AvatarUrl),
},
)
.await?;
return Ok(get_avatar_url::Response {
avatar_url: response.avatar_url,
blurhash: response.blurhash,
}
.into());
}
Ok(get_avatar_url::Response { Ok(get_avatar_url::Response {
avatar_url: db.users.avatar_url(&body.user_id)?, avatar_url: db.users.avatar_url(&body.user_id)?,
blurhash: db.users.blurhash(&body.user_id)?, blurhash: db.users.blurhash(&body.user_id)?,
@ -250,6 +292,27 @@ pub async fn get_profile_route(
db: DatabaseGuard, db: DatabaseGuard,
body: Ruma<get_profile::Request<'_>>, body: Ruma<get_profile::Request<'_>>,
) -> ConduitResult<get_profile::Response> { ) -> ConduitResult<get_profile::Response> {
if body.user_id.server_name() != db.globals.server_name() {
let response = db
.sending
.send_federation_request(
&db.globals,
body.user_id.server_name(),
federation::query::get_profile_information::v1::Request {
user_id: &body.user_id,
field: None,
},
)
.await?;
return Ok(get_profile::Response {
displayname: response.displayname,
avatar_url: response.avatar_url,
blurhash: response.blurhash,
}
.into());
}
if !db.users.exists(&body.user_id)? { if !db.users.exists(&body.user_id)? {
// Return 404 if this user doesn't exist // Return 404 if this user doesn't exist
return Err(Error::BadRequest( return Err(Error::BadRequest(