2 api::{listing_type_with_default, PerformApub},
3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
6 use activitypub_federation::config::Data;
7 use lemmy_api_common::{
9 post::{GetPosts, GetPostsResponse},
10 utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
12 use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
13 use lemmy_db_views::post_view::PostQuery;
14 use lemmy_utils::error::LemmyError;
16 #[async_trait::async_trait]
17 impl PerformApub for GetPosts {
18 type Response = GetPostsResponse;
20 #[tracing::instrument(skip(context))]
21 async fn perform(&self, context: &Data<LemmyContext>) -> Result<GetPostsResponse, LemmyError> {
22 let data: &GetPosts = self;
23 let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), context).await;
24 let local_site = LocalSite::read(context.pool()).await?;
26 check_private_instance(&local_user_view, &local_site)?;
31 let limit = data.limit;
32 let community_id = if let Some(name) = &data.community_name {
33 resolve_actor_identifier::<ApubCommunity, Community>(name, context, &None, true)
40 let saved_only = data.saved_only;
42 let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
45 is_mod_or_admin_opt(context.pool(), local_user_view.as_ref(), community_id)
49 let posts = PostQuery::builder()
51 .local_user(local_user_view.map(|l| l.local_user).as_ref())
52 .listing_type(Some(listing_type))
54 .community_id(community_id)
55 .saved_only(saved_only)
58 .is_mod_or_admin(Some(is_mod_or_admin))
62 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
64 Ok(GetPostsResponse { posts })