]> Untitled Git - lemmy.git/blob - crates/apub/src/api/list_comments.rs
c4af6900aa3c7d1102326706c96bb24077488429
[lemmy.git] / crates / apub / src / api / list_comments.rs
1 use crate::{
2   api::PerformApub,
3   fetcher::resolve_actor_identifier,
4   objects::community::ApubCommunity,
5 };
6 use actix_web::web::Data;
7 use lemmy_api_common::{
8   comment::{GetComments, GetCommentsResponse},
9   context::LemmyContext,
10   utils::{
11     check_private_instance,
12     get_local_user_view_from_jwt_opt,
13     listing_type_with_site_default,
14   },
15 };
16 use lemmy_db_schema::{
17   source::{comment::Comment, community::Community, local_site::LocalSite},
18   traits::{Crud, DeleteableOrRemoveable},
19 };
20 use lemmy_db_views::comment_view::CommentQuery;
21 use lemmy_utils::{error::LemmyError, ConnectionId};
22
23 #[async_trait::async_trait(?Send)]
24 impl PerformApub for GetComments {
25   type Response = GetCommentsResponse;
26
27   #[tracing::instrument(skip(context, _websocket_id))]
28   async fn perform(
29     &self,
30     context: &Data<LemmyContext>,
31     _websocket_id: Option<ConnectionId>,
32   ) -> Result<GetCommentsResponse, LemmyError> {
33     let data: &GetComments = self;
34     let local_user_view =
35       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
36         .await?;
37     let local_site = LocalSite::read(context.pool()).await?;
38     check_private_instance(&local_user_view, &local_site)?;
39
40     let community_id = data.community_id;
41     let listing_type = listing_type_with_site_default(data.type_, &local_site)?;
42
43     let community_actor_id = if let Some(name) = &data.community_name {
44       resolve_actor_identifier::<ApubCommunity, Community>(name, context, true)
45         .await
46         .ok()
47         .map(|c| c.actor_id)
48     } else {
49       None
50     };
51     let sort = data.sort;
52     let max_depth = data.max_depth;
53     let saved_only = data.saved_only;
54     let page = data.page;
55     let limit = data.limit;
56     let parent_id = data.parent_id;
57
58     // If a parent_id is given, fetch the comment to get the path
59     let parent_path = if let Some(parent_id) = parent_id {
60       Some(Comment::read(context.pool(), parent_id).await?.path)
61     } else {
62       None
63     };
64
65     let parent_path_cloned = parent_path.clone();
66     let post_id = data.post_id;
67     let local_user = local_user_view.map(|l| l.local_user);
68     let mut comments = CommentQuery::builder()
69       .pool(context.pool())
70       .listing_type(Some(listing_type))
71       .sort(sort)
72       .max_depth(max_depth)
73       .saved_only(saved_only)
74       .community_id(community_id)
75       .community_actor_id(community_actor_id)
76       .parent_path(parent_path_cloned)
77       .post_id(post_id)
78       .local_user(local_user.as_ref())
79       .page(page)
80       .limit(limit)
81       .build()
82       .list()
83       .await
84       .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
85
86     // Blank out deleted or removed info
87     for cv in comments
88       .iter_mut()
89       .filter(|cv| cv.comment.deleted || cv.comment.removed)
90     {
91       cv.comment = cv.clone().comment.blank_out_deleted_or_removed_info();
92     }
93
94     Ok(GetCommentsResponse { comments })
95   }
96 }