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_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 = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
28 let show_bot_accounts = local_user_view
30 .map(|t| t.local_user.show_bot_accounts);
31 let show_read_posts = local_user_view
33 .map(|t| t.local_user.show_read_posts);
35 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
40 .unwrap_or_else(|| "admin".to_string());
41 let person_details_id = match data.person_id {
44 let person = blocking(context.pool(), move |conn| {
45 Person::find_by_name(conn, &username)
49 .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
54 let person_id = local_user_view.map(|uv| uv.person.id);
56 // You don't need to return settings for the user, since this comes back with GetSite
58 let person_view = blocking(context.pool(), move |conn| {
59 PersonViewSafe::read(conn, person_details_id)
64 let limit = data.limit;
65 let saved_only = data.saved_only;
66 let community_id = data.community_id;
68 let (posts, comments) = blocking(context.pool(), move |conn| {
69 let mut posts_query = PostQueryBuilder::create(conn)
72 .show_bot_accounts(show_bot_accounts)
73 .show_read_posts(show_read_posts)
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(false) {
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 {