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, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, ApubObject, 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_follower_view::CommunityFollowerView,
10 community_moderator_view::CommunityModeratorView,
11 person_view::PersonViewSafe,
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::LemmyContext;
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for GetPersonDetails {
18 type Response = GetPersonDetailsResponse;
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<GetPersonDetailsResponse, LemmyError> {
25 let data: &GetPersonDetails = self;
26 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).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());
45 let actor_id = build_actor_id_from_shortname(EndpointType::Person, &name)?;
47 let person = blocking(context.pool(), move |conn| {
48 Person::read_from_apub_id(conn, &actor_id)
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 mut follows = vec![];
107 if let Some(pid) = person_id {
108 if pid == person_details_id {
109 follows = blocking(context.pool(), move |conn| {
110 CommunityFollowerView::for_person(conn, person_details_id)
115 let moderates = blocking(context.pool(), move |conn| {
116 CommunityModeratorView::for_person(conn, person_details_id)
121 Ok(GetPersonDetailsResponse {