From eb56d9253cee2f25b9472805206a4ad132307ff7 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 24 Jan 2024 04:32:14 -0500 Subject: [PATCH] Adding site to GetPersonDetails. Fixes #4373 (#4394) * Adding site to GetPersonDetails. Fixes #4373 * Removing the conditioned site return. --- crates/api_common/src/person.rs | 2 ++ crates/api_common/src/utils.rs | 13 +++++++++++++ crates/apub/src/api/read_community.rs | 13 ++----------- crates/apub/src/api/read_person.rs | 5 ++++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 145e00e7a..8944a57cc 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -1,6 +1,7 @@ use crate::sensitive::Sensitive; use lemmy_db_schema::{ newtypes::{CommentReplyId, CommunityId, LanguageId, PersonId, PersonMentionId}, + source::site::Site, CommentSortType, ListingType, PostListingMode, @@ -178,6 +179,7 @@ pub struct GetPersonDetails { /// A person's details response. pub struct GetPersonDetailsResponse { pub person_view: PersonView, + pub site: Option, pub comments: Vec, pub posts: Vec, pub moderates: Vec, diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 1f72a4fd4..605733efc 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -20,6 +20,7 @@ use lemmy_db_schema::{ person::{Person, PersonUpdateForm}, person_block::PersonBlock, post::{Post, PostRead}, + site::Site, }, traits::Crud, utils::DbPool, @@ -566,6 +567,18 @@ pub fn check_private_instance_and_federation_enabled( } } +/// Read the site for an actor_id. +/// +/// Used for GetCommunityResponse and GetPersonDetails +pub async fn read_site_for_actor( + actor_id: DbUrl, + context: &LemmyContext, +) -> Result, LemmyError> { + let site_id = Site::instance_actor_id_from_url(actor_id.clone().into()); + let site = Site::read_from_apub_id(&mut context.pool(), &site_id.into()).await?; + Ok(site) +} + pub async fn purge_image_posts_for_person( banned_person_id: PersonId, context: &LemmyContext, diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index afa6fb829..a41deb32c 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -4,13 +4,12 @@ use actix_web::web::{Json, Query}; use lemmy_api_common::{ community::{GetCommunity, GetCommunityResponse}, context::LemmyContext, - utils::{check_private_instance, is_mod_or_admin_opt}, + utils::{check_private_instance, is_mod_or_admin_opt, read_site_for_actor}, }; use lemmy_db_schema::source::{ actor_language::CommunityLanguage, community::Community, local_site::LocalSite, - site::Site, }; use lemmy_db_views::structs::LocalUserView; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView}; @@ -64,15 +63,7 @@ pub async fn get_community( .await .with_lemmy_type(LemmyErrorType::CouldntFindCommunity)?; - let site_id = Site::instance_actor_id_from_url(community_view.community.actor_id.clone().into()); - let mut site = Site::read_from_apub_id(&mut context.pool(), &site_id.into()).await?; - // no need to include metadata for local site (its already available through other endpoints). - // this also prevents us from leaking the federation private key. - if let Some(s) = &site { - if s.actor_id.domain() == Some(context.settings().hostname.as_ref()) { - site = None; - } - } + let site = read_site_for_actor(community_view.community.actor_id.clone(), &context).await?; let community_id = community_view.community.id; let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?; diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index b32fc5b5f..9e2de9fef 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -4,7 +4,7 @@ use actix_web::web::{Json, Query}; use lemmy_api_common::{ context::LemmyContext, person::{GetPersonDetails, GetPersonDetailsResponse}, - utils::check_private_instance, + utils::{check_private_instance, read_site_for_actor}, }; use lemmy_db_schema::{ source::{local_site::LocalSite, person::Person}, @@ -89,9 +89,12 @@ pub async fn read_person( let moderates = CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?; + let site = read_site_for_actor(person_view.person.actor_id.clone(), &context).await?; + // Return the jwt Ok(Json(GetPersonDetailsResponse { person_view, + site, moderates, comments, posts,