2 api::{listing_type_with_default, PerformApub},
3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
6 use activitypub_federation::config::Data;
7 use lemmy_api_common::{
8 comment::{GetComments, GetCommentsResponse},
10 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
12 use lemmy_db_schema::{
13 source::{comment::Comment, community::Community, local_site::LocalSite},
16 use lemmy_db_views::comment_view::CommentQuery;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
19 #[async_trait::async_trait]
20 impl PerformApub for GetComments {
21 type Response = GetCommentsResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<GetCommentsResponse, LemmyError> {
29 let data: &GetComments = self;
31 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
33 let local_site = LocalSite::read(context.pool()).await?;
34 check_private_instance(&local_user_view, &local_site)?;
36 let community_id = if let Some(name) = &data.community_name {
37 resolve_actor_identifier::<ApubCommunity, Community>(name, context, &None, true)
45 let max_depth = data.max_depth;
46 let saved_only = data.saved_only;
48 let limit = data.limit;
49 let parent_id = data.parent_id;
51 let listing_type = listing_type_with_default(data.type_, &local_site, community_id)?;
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)
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()
65 .listing_type(Some(listing_type))
68 .saved_only(saved_only)
69 .community_id(community_id)
70 .parent_path(parent_path_cloned)
72 .local_user(local_user.as_ref())
78 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
80 Ok(GetCommentsResponse { comments })