1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*};
4 use lemmy_apub::{build_actor_id_from_shortname, fetcher::object_id::ObjectId, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, SortType};
6 use lemmy_db_schema::source::person::*;
7 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
8 use lemmy_db_views_actor::{
9 community_moderator_view::CommunityModeratorView,
10 person_view::PersonViewSafe,
12 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
13 use lemmy_websocket::LemmyContext;
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for GetPersonDetails {
17 type Response = GetPersonDetailsResponse;
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, context.pool(), context.secret()).await?;
28 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
29 let show_bot_accounts = local_user_view
31 .map(|t| t.local_user.show_bot_accounts);
32 let show_read_posts = local_user_view
34 .map(|t| t.local_user.show_read_posts);
36 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
38 let person_details_id = match data.person_id {
44 .unwrap_or_else(|| "admin".to_string());
46 build_actor_id_from_shortname(EndpointType::Person, &name, &context.settings())?;
48 let person = ObjectId::<Person>::new(actor_id)
49 .dereference(context, &mut 0)
52 .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
57 let person_id = local_user_view.map(|uv| uv.person.id);
59 // You don't need to return settings for the user, since this comes back with GetSite
61 let person_view = blocking(context.pool(), move |conn| {
62 PersonViewSafe::read(conn, person_details_id)
67 let limit = data.limit;
68 let saved_only = data.saved_only;
69 let community_id = data.community_id;
71 let (posts, comments) = blocking(context.pool(), move |conn| {
72 let mut posts_query = PostQueryBuilder::create(conn)
75 .show_bot_accounts(show_bot_accounts)
76 .show_read_posts(show_read_posts)
77 .saved_only(saved_only)
78 .community_id(community_id)
79 .my_person_id(person_id)
83 let mut comments_query = CommentQueryBuilder::create(conn)
84 .my_person_id(person_id)
85 .show_bot_accounts(show_bot_accounts)
87 .saved_only(saved_only)
88 .community_id(community_id)
92 // If its saved only, you don't care what creator it was
93 // Or, if its not saved, then you only want it for that specific creator
94 if !saved_only.unwrap_or(false) {
95 posts_query = posts_query.creator_id(person_details_id);
96 comments_query = comments_query.creator_id(person_details_id);
99 let posts = posts_query.list()?;
100 let comments = comments_query.list()?;
102 Ok((posts, comments)) as Result<_, LemmyError>
106 let moderates = blocking(context.pool(), move |conn| {
107 CommunityModeratorView::for_person(conn, person_details_id)
112 Ok(GetPersonDetailsResponse {