1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 check_private_instance,
6 get_local_user_view_from_jwt_opt,
8 resolve_actor_identifier,
10 use lemmy_db_schema::{from_opt_str_to_opt_enum, source::person::Person, SortType};
11 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
12 use lemmy_db_views_actor::{
13 community_moderator_view::CommunityModeratorView,
14 person_view::PersonViewSafe,
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetPersonDetails {
21 type Response = GetPersonDetailsResponse;
23 #[tracing::instrument(skip(self, context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetPersonDetailsResponse, LemmyError> {
29 let data: &GetPersonDetails = self;
31 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34 check_private_instance(&local_user_view, context.pool()).await?;
36 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
37 let show_bot_accounts = local_user_view
39 .map(|t| t.local_user.show_bot_accounts);
40 let show_read_posts = local_user_view
42 .map(|t| t.local_user.show_read_posts);
44 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
46 let person_details_id = match data.person_id {
52 .unwrap_or_else(|| "admin".to_string());
54 resolve_actor_identifier::<Person>(&name, context.pool())
56 .map_err(LemmyError::from)
57 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
62 let person_id = local_user_view.map(|uv| uv.person.id);
64 // You don't need to return settings for the user, since this comes back with GetSite
66 let person_view = blocking(context.pool(), move |conn| {
67 PersonViewSafe::read(conn, person_details_id)
72 let limit = data.limit;
73 let saved_only = data.saved_only;
74 let community_id = data.community_id;
76 let (posts, comments) = blocking(context.pool(), move |conn| {
77 let mut posts_query = PostQueryBuilder::create(conn)
80 .show_bot_accounts(show_bot_accounts)
81 .show_read_posts(show_read_posts)
82 .saved_only(saved_only)
83 .community_id(community_id)
84 .my_person_id(person_id)
88 let mut comments_query = CommentQueryBuilder::create(conn)
89 .my_person_id(person_id)
90 .show_bot_accounts(show_bot_accounts)
92 .saved_only(saved_only)
93 .community_id(community_id)
97 // If its saved only, you don't care what creator it was
98 // Or, if its not saved, then you only want it for that specific creator
99 if !saved_only.unwrap_or(false) {
100 posts_query = posts_query.creator_id(person_details_id);
101 comments_query = comments_query.creator_id(person_details_id);
104 let posts = posts_query.list()?;
105 let comments = comments_query.list()?;
107 Ok((posts, comments)) as Result<_, LemmyError>
111 let moderates = blocking(context.pool(), move |conn| {
112 CommunityModeratorView::for_person(conn, person_details_id)
117 Ok(GetPersonDetailsResponse {