1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{GetPersonDetails, GetPersonDetailsResponse},
5 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::person::ApubPerson};
8 use lemmy_db_schema::{source::person::Person, utils::post_to_comment_sort_type};
9 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
10 use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe};
11 use lemmy_utils::{error::LemmyError, ConnectionId};
12 use lemmy_websocket::LemmyContext;
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetPersonDetails {
16 type Response = GetPersonDetailsResponse;
18 #[tracing::instrument(skip(self, context, _websocket_id))]
21 context: &Data<LemmyContext>,
22 _websocket_id: Option<ConnectionId>,
23 ) -> Result<GetPersonDetailsResponse, LemmyError> {
24 let data: &GetPersonDetails = self;
26 // Check to make sure a person name or an id is given
27 if data.username.is_none() && data.person_id.is_none() {
28 return Err(LemmyError::from_message("no_id_given"));
32 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
35 check_private_instance(&local_user_view, context.pool()).await?;
37 let show_bot_accounts = local_user_view
39 .map(|t| t.local_user.show_bot_accounts);
41 let person_details_id = match data.person_id {
44 if let Some(username) = &data.username {
45 resolve_actor_identifier::<ApubPerson, Person>(username, context)
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 = blocking(context.pool(), move |conn| {
60 PersonViewSafe::read(conn, person_details_id)
66 let limit = data.limit;
67 let saved_only = data.saved_only;
68 let community_id = data.community_id;
70 let (posts, comments) = blocking(context.pool(), move |conn| {
71 let posts_query = PostQuery::builder()
74 .saved_only(saved_only)
75 .community_id(community_id)
79 let person_id = local_user_view.map(|uv| uv.person.id);
80 let comments_query = CommentQuery::builder()
82 .my_person_id(person_id)
83 .show_bot_accounts(show_bot_accounts)
84 .sort(sort.map(post_to_comment_sort_type))
85 .saved_only(saved_only)
86 .community_id(community_id)
90 // If its saved only, you don't care what creator it was
91 // Or, if its not saved, then you only want it for that specific creator
92 let (posts, comments) = if !saved_only.unwrap_or(false) {
95 .creator_id(Some(person_details_id))
99 .creator_id(Some(person_details_id))
104 (posts_query.build().list()?, comments_query.build().list()?)
107 Ok((posts, comments)) as Result<_, LemmyError>
111 let moderates = blocking(context.pool(), move |conn| {
112 CommunityModeratorView::for_person(conn, person_details_id)
117 Ok(GetPersonDetailsResponse {