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::{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;
14 use std::str::FromStr;
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for GetPersonDetails {
18 type Response = GetPersonDetailsResponse;
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<GetPersonDetailsResponse, LemmyError> {
25 let data: &GetPersonDetails = &self;
26 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
28 let show_nsfw = match &local_user_view {
29 Some(uv) => uv.local_user.show_nsfw,
33 let sort = SortType::from_str(&data.sort)?;
38 .unwrap_or_else(|| "admin".to_string());
39 let person_details_id = match data.person_id {
42 let person = blocking(context.pool(), move |conn| {
43 Person::find_by_name(conn, &username)
47 .map_err(|_| ApiError::err("couldnt_find_that_username_or_email"))?
52 let person_id = local_user_view.map(|uv| uv.person.id);
54 // You don't need to return settings for the user, since this comes back with GetSite
56 let person_view = blocking(context.pool(), move |conn| {
57 PersonViewSafe::read(conn, person_details_id)
62 let limit = data.limit;
63 let saved_only = data.saved_only;
64 let community_id = data.community_id;
66 let (posts, comments) = blocking(context.pool(), move |conn| {
67 let mut posts_query = PostQueryBuilder::create(conn)
70 .saved_only(saved_only)
71 .community_id(community_id)
72 .my_person_id(person_id)
76 let mut comments_query = CommentQueryBuilder::create(conn)
77 .my_person_id(person_id)
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
87 posts_query = posts_query.creator_id(person_details_id);
88 comments_query = comments_query.creator_id(person_details_id);
91 let posts = posts_query.list()?;
92 let comments = comments_query.list()?;
94 Ok((posts, comments)) as Result<_, LemmyError>
98 let mut follows = vec![];
99 if let Some(pid) = person_id {
100 if pid == person_details_id {
101 follows = blocking(context.pool(), move |conn| {
102 CommunityFollowerView::for_person(conn, person_details_id)
107 let moderates = blocking(context.pool(), move |conn| {
108 CommunityModeratorView::for_person(conn, person_details_id)
113 Ok(GetPersonDetailsResponse {