]> Untitled Git - lemmy.git/blob - crates/apub/src/api/list_comments.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[lemmy.git] / crates / apub / src / api / list_comments.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   comment::{GetComments, GetCommentsResponse},
10   context::LemmyContext,
11   utils::{check_private_instance, local_user_view_from_jwt_opt},
12 };
13 use lemmy_db_schema::{
14   source::{comment::Comment, community::Community, local_site::LocalSite},
15   traits::Crud,
16 };
17 use lemmy_db_views::comment_view::CommentQuery;
18 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
19
20 #[tracing::instrument(skip(context))]
21 pub async fn list_comments(
22   data: Query<GetComments>,
23   context: Data<LemmyContext>,
24 ) -> Result<Json<GetCommentsResponse>, LemmyError> {
25   let local_user_view = local_user_view_from_jwt_opt(data.auth.as_ref(), &context).await;
26   let local_site = LocalSite::read(&mut context.pool()).await?;
27   check_private_instance(&local_user_view, &local_site)?;
28
29   let community_id = if let Some(name) = &data.community_name {
30     Some(resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &None, true).await?)
31       .map(|c| c.id)
32   } else {
33     data.community_id
34   };
35   let sort = data.sort;
36   let max_depth = data.max_depth;
37   let saved_only = data.saved_only.unwrap_or_default();
38
39   let liked_only = data.liked_only.unwrap_or_default();
40   let disliked_only = data.disliked_only.unwrap_or_default();
41   if liked_only && disliked_only {
42     return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
43   }
44
45   let page = data.page;
46   let limit = data.limit;
47   let parent_id = data.parent_id;
48
49   let listing_type = Some(listing_type_with_default(
50     data.type_,
51     &local_site,
52     community_id,
53   )?);
54
55   // If a parent_id is given, fetch the comment to get the path
56   let parent_path = if let Some(parent_id) = parent_id {
57     Some(Comment::read(&mut context.pool(), parent_id).await?.path)
58   } else {
59     None
60   };
61
62   let parent_path_cloned = parent_path.clone();
63   let post_id = data.post_id;
64   let comments = CommentQuery {
65     listing_type,
66     sort,
67     max_depth,
68     saved_only,
69     liked_only,
70     disliked_only,
71     community_id,
72     parent_path: parent_path_cloned,
73     post_id,
74     local_user: local_user_view.as_ref(),
75     page,
76     limit,
77     ..Default::default()
78   }
79   .list(&mut context.pool())
80   .await
81   .with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
82
83   Ok(Json(GetCommentsResponse { comments }))
84 }