]> Untitled Git - lemmy.git/blob - crates/apub/src/api/list_posts.rs
Remove TypedBuilder from db_views and db_views_actor (#3637)
[lemmy.git] / crates / apub / src / api / list_posts.rs
1 use crate::{
2   api::listing_type_with_default,
3   fetcher::resolve_actor_identifier,
4   objects::community::ApubCommunity,
5 };
6 use activitypub_federation::config::Data;
7 use actix_web::web::{Json, Query};
8 use lemmy_api_common::{
9   context::LemmyContext,
10   post::{GetPosts, GetPostsResponse},
11   utils::{check_private_instance, is_mod_or_admin_opt, local_user_view_from_jwt_opt},
12 };
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};
16
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?;
24
25   check_private_instance(&local_user_view, &local_site)?;
26
27   let sort = data.sort;
28
29   let page = data.page;
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?)
33       .map(|c| c.id)
34   } else {
35     data.community_id
36   };
37   let saved_only = data.saved_only;
38
39   let listing_type = Some(listing_type_with_default(
40     data.type_,
41     &local_site,
42     community_id,
43   )?);
44
45   let is_mod_or_admin = Some(
46     is_mod_or_admin_opt(&mut context.pool(), local_user_view.as_ref(), community_id)
47       .await
48       .is_ok(),
49   );
50
51   let posts = PostQuery {
52     local_user: local_user_view.map(|l| l.local_user).as_ref(),
53     listing_type,
54     sort,
55     community_id,
56     saved_only,
57     page,
58     limit,
59     is_mod_or_admin,
60     ..Default::default()
61   }
62   .list(&mut context.pool())
63   .await
64   .with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;
65
66   Ok(Json(GetPostsResponse { posts }))
67 }