1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, get_local_user_view_from_jwt_opt, person::*, user_show_nsfw, user_show_bot_accounts};
4 use lemmy_db_queries::{from_opt_str_to_opt_enum, source::person::Person_, SortType};
5 use lemmy_db_schema::source::person::*;
6 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
7 use lemmy_db_views_actor::{
8 community_follower_view::CommunityFollowerView,
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;
25 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
27 let show_nsfw = user_show_nsfw(&local_user_view);
28 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
30 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
35 .unwrap_or_else(|| "admin".to_string());
36 let person_details_id = match data.person_id {
39 let person = blocking(context.pool(), move |conn| {
40 Person::find_by_name(conn, &username)
44 .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
49 let person_id = local_user_view.map(|uv| uv.person.id);
51 // You don't need to return settings for the user, since this comes back with GetSite
53 let person_view = blocking(context.pool(), move |conn| {
54 PersonViewSafe::read(conn, person_details_id)
59 let limit = data.limit;
60 let saved_only = data.saved_only;
61 let community_id = data.community_id;
63 let (posts, comments) = blocking(context.pool(), move |conn| {
64 let mut posts_query = PostQueryBuilder::create(conn)
67 .show_bot_accounts(show_bot_accounts)
68 .saved_only(saved_only)
69 .community_id(community_id)
70 .my_person_id(person_id)
74 let mut comments_query = CommentQueryBuilder::create(conn)
75 .my_person_id(person_id)
76 .show_bot_accounts(show_bot_accounts)
78 .saved_only(saved_only)
79 .community_id(community_id)
83 // If its saved only, you don't care what creator it was
84 // Or, if its not saved, then you only want it for that specific creator
85 if !saved_only.unwrap_or_default() {
86 posts_query = posts_query.creator_id(person_details_id);
87 comments_query = comments_query.creator_id(person_details_id);
90 let posts = posts_query.list()?;
91 let comments = comments_query.list()?;
93 Ok((posts, comments)) as Result<_, LemmyError>
97 let mut follows = vec![];
98 if let Some(pid) = person_id {
99 if pid == person_details_id {
100 follows = blocking(context.pool(), move |conn| {
101 CommunityFollowerView::for_person(conn, person_details_id)
106 let moderates = blocking(context.pool(), move |conn| {
107 CommunityModeratorView::for_person(conn, person_details_id)
112 Ok(GetPersonDetailsResponse {