1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 person::{GetPersonDetails, GetPersonDetailsResponse},
5 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::person::ApubPerson};
8 use lemmy_db_schema::{source::person::Person, utils::post_to_comment_sort_type};
9 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
10 use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe};
11 use lemmy_utils::{error::LemmyError, ConnectionId};
12 use lemmy_websocket::LemmyContext;
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetPersonDetails {
16 type Response = GetPersonDetailsResponse;
18 #[tracing::instrument(skip(self, context, _websocket_id))]
21 context: &Data<LemmyContext>,
22 _websocket_id: Option<ConnectionId>,
23 ) -> Result<GetPersonDetailsResponse, LemmyError> {
24 let data: &GetPersonDetails = self;
26 // Check to make sure a person name or an id is given
27 if data.username.is_none() && data.person_id.is_none() {
28 return Err(LemmyError::from_message("no_id_given"));
32 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 person_details_id = match data.person_id {
39 if let Some(username) = &data.username {
40 resolve_actor_identifier::<ApubPerson, Person>(username, context)
42 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?
45 return Err(LemmyError::from_message(
46 "couldnt_find_that_username_or_email",
52 // You don't need to return settings for the user, since this comes back with GetSite
54 let person_view = blocking(context.pool(), move |conn| {
55 PersonViewSafe::read(conn, person_details_id)
61 let limit = data.limit;
62 let saved_only = data.saved_only;
63 let community_id = data.community_id;
65 let (posts, comments) = blocking(context.pool(), move |conn| {
66 let posts_query = PostQuery::builder()
69 .saved_only(saved_only)
70 .community_id(community_id)
74 let local_user = local_user_view.map(|l| l.local_user);
75 let comments_query = CommentQuery::builder()
77 .local_user(local_user.as_ref())
78 .sort(sort.map(post_to_comment_sort_type))
79 .saved_only(saved_only)
80 .community_id(community_id)
84 // If its saved only, you don't care what creator it was
85 // Or, if its not saved, then you only want it for that specific creator
86 let (posts, comments) = if !saved_only.unwrap_or(false) {
89 .creator_id(Some(person_details_id))
93 .creator_id(Some(person_details_id))
98 (posts_query.build().list()?, comments_query.build().list()?)
101 Ok((posts, comments)) as Result<_, LemmyError>
105 let moderates = blocking(context.pool(), move |conn| {
106 CommunityModeratorView::for_person(conn, person_details_id)
111 Ok(GetPersonDetailsResponse {