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)
48 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
53 let person_id = local_user_view.map(|uv| uv.person.id);
55 // You don't need to return settings for the user, since this comes back with GetSite
57 let person_view = blocking(context.pool(), move |conn| {
58 PersonViewSafe::read(conn, person_details_id)
63 let limit = data.limit;
64 let saved_only = data.saved_only;
65 let community_id = data.community_id;
67 let (posts, comments) = blocking(context.pool(), move |conn| {
68 let mut posts_query = PostQueryBuilder::create(conn)
71 .saved_only(saved_only)
72 .community_id(community_id)
73 .my_person_id(person_id)
77 let mut comments_query = CommentQueryBuilder::create(conn)
78 .my_person_id(person_id)
80 .saved_only(saved_only)
81 .community_id(community_id)
85 // If its saved only, you don't care what creator it was
86 // Or, if its not saved, then you only want it for that specific creator
88 posts_query = posts_query.creator_id(person_details_id);
89 comments_query = comments_query.creator_id(person_details_id);
92 let posts = posts_query.list()?;
93 let comments = comments_query.list()?;
95 Ok((posts, comments)) as Result<_, LemmyError>
99 let mut follows = vec![];
100 if let Some(pid) = person_id {
101 if pid == person_details_id {
102 follows = blocking(context.pool(), move |conn| {
103 CommunityFollowerView::for_person(conn, person_details_id)
108 let moderates = blocking(context.pool(), move |conn| {
109 CommunityModeratorView::for_person(conn, person_details_id)
114 Ok(GetPersonDetailsResponse {