1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{GetPersonDetails, GetPersonDetailsResponse},
5 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::person::ApubPerson};
9 source::{local_site::LocalSite, person::Person},
10 utils::post_to_comment_sort_type,
12 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
13 use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::LemmyContext;
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetPersonDetails {
19 type Response = GetPersonDetailsResponse;
21 #[tracing::instrument(skip(self, context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<GetPersonDetailsResponse, LemmyError> {
27 let data: &GetPersonDetails = self;
29 // Check to make sure a person name or an id is given
30 if data.username.is_none() && data.person_id.is_none() {
31 return Err(LemmyError::from_message("no_id_given"));
35 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
37 let local_site = LocalSite::read(context.pool()).await?;
39 check_private_instance(&local_user_view, &local_site)?;
41 let person_details_id = match data.person_id {
44 if let Some(username) = &data.username {
45 resolve_actor_identifier::<ApubPerson, Person>(username, context, true)
47 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
50 return Err(LemmyError::from_message(
51 "couldnt_find_that_username_or_email",
57 // You don't need to return settings for the user, since this comes back with GetSite
59 let person_view = PersonViewSafe::read(context.pool(), person_details_id).await?;
63 let limit = data.limit;
64 let saved_only = data.saved_only;
65 let community_id = data.community_id;
66 let local_user = local_user_view.map(|l| l.local_user);
67 let local_user_clone = local_user.clone();
69 let posts_query = PostQuery::builder()
72 .saved_only(saved_only)
73 .local_user(local_user.as_ref())
74 .community_id(community_id)
78 // If its saved only, you don't care what creator it was
79 // Or, if its not saved, then you only want it for that specific creator
80 let posts = if !saved_only.unwrap_or(false) {
82 .creator_id(Some(person_details_id))
86 posts_query.build().list()
90 let comments_query = CommentQuery::builder()
92 .local_user(local_user_clone.as_ref())
93 .sort(sort.map(post_to_comment_sort_type))
94 .saved_only(saved_only)
95 .show_deleted_and_removed(Some(false))
96 .community_id(community_id)
100 // If its saved only, you don't care what creator it was
101 // Or, if its not saved, then you only want it for that specific creator
102 let comments = if !saved_only.unwrap_or(false) {
104 .creator_id(Some(person_details_id))
108 comments_query.build().list()
112 let moderates = CommunityModeratorView::for_person(context.pool(), person_details_id).await?;
115 Ok(GetPersonDetailsResponse {