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(|e| e.with_message("couldnt_find_that_username_or_email"))?
61 let person_id = local_user_view.map(|uv| uv.person.id);
63 // You don't need to return settings for the user, since this comes back with GetSite
65 let person_view = blocking(context.pool(), move |conn| {
66 PersonViewSafe::read(conn, person_details_id)
71 let limit = data.limit;
72 let saved_only = data.saved_only;
73 let community_id = data.community_id;
75 let (posts, comments) = blocking(context.pool(), move |conn| {
76 let mut posts_query = PostQueryBuilder::create(conn)
79 .show_bot_accounts(show_bot_accounts)
80 .show_read_posts(show_read_posts)
81 .saved_only(saved_only)
82 .community_id(community_id)
83 .my_person_id(person_id)
87 let mut comments_query = CommentQueryBuilder::create(conn)
88 .my_person_id(person_id)
89 .show_bot_accounts(show_bot_accounts)
91 .saved_only(saved_only)
92 .community_id(community_id)
96 // If its saved only, you don't care what creator it was
97 // Or, if its not saved, then you only want it for that specific creator
98 if !saved_only.unwrap_or(false) {
99 posts_query = posts_query.creator_id(person_details_id);
100 comments_query = comments_query.creator_id(person_details_id);
103 let posts = posts_query.list()?;
104 let comments = comments_query.list()?;
106 Ok((posts, comments)) as Result<_, LemmyError>
110 let moderates = blocking(context.pool(), move |conn| {
111 CommunityModeratorView::for_person(conn, person_details_id)
116 Ok(GetPersonDetailsResponse {