1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 get_local_user_view_from_jwt_opt,
7 user_show_bot_accounts,
10 use lemmy_db_queries::{from_opt_str_to_opt_enum, source::person::Person_, SortType};
11 use lemmy_db_schema::source::person::*;
12 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
13 use lemmy_db_views_actor::{
14 community_follower_view::CommunityFollowerView,
15 community_moderator_view::CommunityModeratorView,
16 person_view::PersonViewSafe,
18 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
19 use lemmy_websocket::LemmyContext;
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for GetPersonDetails {
23 type Response = GetPersonDetailsResponse;
27 context: &Data<LemmyContext>,
28 _websocket_id: Option<ConnectionId>,
29 ) -> Result<GetPersonDetailsResponse, LemmyError> {
30 let data: &GetPersonDetails = &self;
31 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
33 let show_nsfw = user_show_nsfw(&local_user_view);
34 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
36 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
41 .unwrap_or_else(|| "admin".to_string());
42 let person_details_id = match data.person_id {
45 let person = blocking(context.pool(), move |conn| {
46 Person::find_by_name(conn, &username)
50 .map_err(|_| ApiError::err("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)
65 let limit = data.limit;
66 let saved_only = data.saved_only;
67 let community_id = data.community_id;
69 let (posts, comments) = blocking(context.pool(), move |conn| {
70 let mut posts_query = PostQueryBuilder::create(conn)
73 .show_bot_accounts(show_bot_accounts)
74 .saved_only(saved_only)
75 .community_id(community_id)
76 .my_person_id(person_id)
80 let mut comments_query = CommentQueryBuilder::create(conn)
81 .my_person_id(person_id)
82 .show_bot_accounts(show_bot_accounts)
84 .saved_only(saved_only)
85 .community_id(community_id)
89 // If its saved only, you don't care what creator it was
90 // Or, if its not saved, then you only want it for that specific creator
91 if !saved_only.unwrap_or_default() {
92 posts_query = posts_query.creator_id(person_details_id);
93 comments_query = comments_query.creator_id(person_details_id);
96 let posts = posts_query.list()?;
97 let comments = comments_query.list()?;
99 Ok((posts, comments)) as Result<_, LemmyError>
103 let mut follows = vec![];
104 if let Some(pid) = person_id {
105 if pid == person_details_id {
106 follows = blocking(context.pool(), move |conn| {
107 CommunityFollowerView::for_person(conn, person_details_id)
112 let moderates = blocking(context.pool(), move |conn| {
113 CommunityModeratorView::for_person(conn, person_details_id)
118 Ok(GetPersonDetailsResponse {