2 api::listing_type_with_default,
3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
6 use activitypub_federation::config::Data;
7 use actix_web::web::{Json, Query};
8 use lemmy_api_common::{
10 post::{GetPosts, GetPostsResponse},
11 utils::{check_private_instance, local_user_view_from_jwt_opt},
13 use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
14 use lemmy_db_views::post_view::PostQuery;
15 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
17 #[tracing::instrument(skip(context))]
18 pub async fn list_posts(
19 data: Query<GetPosts>,
20 context: Data<LemmyContext>,
21 ) -> Result<Json<GetPostsResponse>, LemmyError> {
22 let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
23 let local_site = LocalSite::read(&mut context.pool()).await?;
25 check_private_instance(&local_user_view, &local_site)?;
30 let limit = data.limit;
31 let community_id = if let Some(name) = &data.community_name {
32 Some(resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &None, true).await?)
37 let saved_only = data.saved_only;
39 let listing_type = Some(listing_type_with_default(
45 let posts = PostQuery {
46 local_user: local_user_view.as_ref(),
55 .list(&mut context.pool())
57 .with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
59 Ok(Json(GetPostsResponse { posts }))