2023-07-03 09:01:41 +00:00
|
|
|
use crate::{fetcher::resolve_actor_identifier, objects::person::ApubPerson};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2023-07-03 09:01:41 +00:00
|
|
|
use actix_web::web::{Json, Query};
|
2021-12-15 19:49:59 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-05-03 17:44:13 +00:00
|
|
|
person::{GetPersonDetails, GetPersonDetailsResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{check_private_instance, is_admin, local_user_view_from_jwt_opt},
|
2021-12-15 19:49:59 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{local_site::LocalSite, person::Person},
|
|
|
|
utils::post_to_comment_sort_type,
|
|
|
|
};
|
2022-08-04 19:30:17 +00:00
|
|
|
use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
|
2023-03-01 17:19:46 +00:00
|
|
|
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt2, LemmyErrorType};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn read_person(
|
|
|
|
data: Query<GetPersonDetails>,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<Json<GetPersonDetailsResponse>, LemmyError> {
|
|
|
|
// Check to make sure a person name or an id is given
|
|
|
|
if data.username.is_none() && data.person_id.is_none() {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::NoIdGiven)?;
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
2022-07-08 10:21:33 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
let is_admin = local_user_view.as_ref().map(|luv| is_admin(luv).is_ok());
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
check_private_instance(&local_user_view, &local_site)?;
|
2021-12-15 19:49:59 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let person_details_id = match data.person_id {
|
|
|
|
Some(id) => id,
|
|
|
|
None => {
|
|
|
|
if let Some(username) = &data.username {
|
|
|
|
resolve_actor_identifier::<ApubPerson, Person>(username, &context, &local_user_view, true)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntFindPerson)?
|
2023-07-03 09:01:41 +00:00
|
|
|
.id
|
|
|
|
} else {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::CouldntFindPerson)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
// You don't need to return settings for the user, since this comes back with GetSite
|
|
|
|
// `my_user`
|
2023-07-11 13:09:59 +00:00
|
|
|
let person_view = PersonView::read(&mut context.pool(), person_details_id).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
let sort = data.sort;
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let saved_only = data.saved_only;
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let local_user = local_user_view.map(|l| l.local_user);
|
|
|
|
let local_user_clone = local_user.clone();
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let posts = PostQuery::builder()
|
|
|
|
.pool(&mut context.pool())
|
2023-07-03 09:01:41 +00:00
|
|
|
.sort(sort)
|
|
|
|
.saved_only(saved_only)
|
|
|
|
.local_user(local_user.as_ref())
|
|
|
|
.community_id(community_id)
|
|
|
|
.is_mod_or_admin(is_admin)
|
|
|
|
.page(page)
|
2023-07-11 13:09:59 +00:00
|
|
|
.limit(limit)
|
|
|
|
.creator_id(
|
|
|
|
// If its saved only, you don't care what creator it was
|
|
|
|
// Or, if its not saved, then you only want it for that specific creator
|
|
|
|
if !saved_only.unwrap_or(false) {
|
|
|
|
Some(person_details_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
.list()
|
|
|
|
.await?;
|
2022-09-26 14:09:32 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let comments = CommentQuery::builder()
|
|
|
|
.pool(&mut context.pool())
|
2023-07-03 09:01:41 +00:00
|
|
|
.local_user(local_user_clone.as_ref())
|
|
|
|
.sort(sort.map(post_to_comment_sort_type))
|
|
|
|
.saved_only(saved_only)
|
|
|
|
.show_deleted_and_removed(Some(false))
|
|
|
|
.community_id(community_id)
|
|
|
|
.page(page)
|
2023-07-11 13:09:59 +00:00
|
|
|
.limit(limit)
|
|
|
|
.creator_id(
|
|
|
|
// If its saved only, you don't care what creator it was
|
|
|
|
// Or, if its not saved, then you only want it for that specific creator
|
|
|
|
if !saved_only.unwrap_or(false) {
|
|
|
|
Some(person_details_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.build()
|
|
|
|
.list()
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let moderates =
|
|
|
|
CommunityModeratorView::for_person(&mut context.pool(), person_details_id).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
// Return the jwt
|
|
|
|
Ok(Json(GetPersonDetailsResponse {
|
|
|
|
person_view,
|
|
|
|
moderates,
|
|
|
|
comments,
|
|
|
|
posts,
|
|
|
|
}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|