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;
9 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
10 use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe};
11 use lemmy_utils::{ConnectionId, LemmyError};
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 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
29 check_private_instance(&local_user_view, context.pool()).await?;
31 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
32 let show_bot_accounts = local_user_view
34 .map(|t| t.local_user.show_bot_accounts);
35 let show_read_posts = local_user_view
37 .map(|t| t.local_user.show_read_posts);
39 let person_details_id = match data.person_id {
42 if let Some(username) = &data.username {
43 resolve_actor_identifier::<ApubPerson, Person>(username, context)
45 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
48 return Err(LemmyError::from_message(
49 "couldnt_find_that_username_or_email",
55 let person_id = local_user_view.map(|uv| uv.person.id);
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 mut posts_query = PostQueryBuilder::create(conn)
74 .show_bot_accounts(show_bot_accounts)
75 .show_read_posts(show_read_posts)
76 .saved_only(saved_only)
77 .community_id(community_id)
78 .my_person_id(person_id)
82 let mut comments_query = CommentQueryBuilder::create(conn)
83 .my_person_id(person_id)
84 .show_bot_accounts(show_bot_accounts)
86 .saved_only(saved_only)
87 .community_id(community_id)
91 // If its saved only, you don't care what creator it was
92 // Or, if its not saved, then you only want it for that specific creator
93 if !saved_only.unwrap_or(false) {
94 posts_query = posts_query.creator_id(person_details_id);
95 comments_query = comments_query.creator_id(person_details_id);
98 let posts = posts_query.list()?;
99 let comments = comments_query.list()?;
101 Ok((posts, comments)) as Result<_, LemmyError>
105 let moderates = blocking(context.pool(), move |conn| {
106 CommunityModeratorView::for_person(conn, person_details_id)
111 Ok(GetPersonDetailsResponse {