1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
5 fetcher::webfinger::webfinger_resolve,
6 objects::person::ApubPerson,
9 use lemmy_apub_lib::object_id::ObjectId;
10 use lemmy_db_schema::{from_opt_str_to_opt_enum, SortType};
11 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
12 use lemmy_db_views_actor::{
13 community_moderator_view::CommunityModeratorView,
14 person_view::PersonViewSafe,
16 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetPersonDetails {
21 type Response = GetPersonDetailsResponse;
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<GetPersonDetailsResponse, LemmyError> {
28 let data: &GetPersonDetails = self;
30 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
32 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
33 let show_bot_accounts = local_user_view
35 .map(|t| t.local_user.show_bot_accounts);
36 let show_read_posts = local_user_view
38 .map(|t| t.local_user.show_read_posts);
40 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
42 let person_details_id = match data.person_id {
48 .unwrap_or_else(|| "admin".to_string());
50 webfinger_resolve::<ApubPerson>(&name, EndpointType::Person, context, &mut 0).await?;
52 let person = ObjectId::<ApubPerson>::new(actor_id)
53 .dereference(context, &mut 0)
56 .map_err(|e| ApiError::err("couldnt_find_that_username_or_email", e))?
61 let person_id = local_user_view.map(|uv| uv.person.id);
63 // You don't need to return settings for the user, since this comes back with GetSite
65 let person_view = blocking(context.pool(), move |conn| {
66 PersonViewSafe::read(conn, person_details_id)
71 let limit = data.limit;
72 let saved_only = data.saved_only;
73 let community_id = data.community_id;
75 let (posts, comments) = blocking(context.pool(), move |conn| {
76 let mut posts_query = PostQueryBuilder::create(conn)
79 .show_bot_accounts(show_bot_accounts)
80 .show_read_posts(show_read_posts)
81 .saved_only(saved_only)
82 .community_id(community_id)
83 .my_person_id(person_id)
87 let mut comments_query = CommentQueryBuilder::create(conn)
88 .my_person_id(person_id)
89 .show_bot_accounts(show_bot_accounts)
91 .saved_only(saved_only)
92 .community_id(community_id)
96 // If its saved only, you don't care what creator it was
97 // Or, if its not saved, then you only want it for that specific creator
98 if !saved_only.unwrap_or(false) {
99 posts_query = posts_query.creator_id(person_details_id);
100 comments_query = comments_query.creator_id(person_details_id);
103 let posts = posts_query.list()?;
104 let comments = comments_query.list()?;
106 Ok((posts, comments)) as Result<_, LemmyError>
110 let moderates = blocking(context.pool(), move |conn| {
111 CommunityModeratorView::for_person(conn, person_details_id)
116 Ok(GetPersonDetailsResponse {