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