2 api::listing_type_with_default,
3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
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},
13 use lemmy_db_schema::{
14 source::{comment::Comment, community::Community, local_site::LocalSite},
17 use lemmy_db_views::comment_view::CommentQuery;
18 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
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)?;
29 let community_id = if let Some(name) = &data.community_name {
30 Some(resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &None, true).await?)
36 let max_depth = data.max_depth;
37 let saved_only = data.saved_only;
39 let liked_only = data.liked_only;
40 let disliked_only = data.disliked_only;
41 if liked_only.unwrap_or_default() && disliked_only.unwrap_or_default() {
42 return Err(LemmyError::from(LemmyErrorType::ContradictingFilters));
46 let limit = data.limit;
47 let parent_id = data.parent_id;
49 let listing_type = Some(listing_type_with_default(
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)
62 let parent_path_cloned = parent_path.clone();
63 let post_id = data.post_id;
64 let comments = CommentQuery {
72 parent_path: parent_path_cloned,
74 local_user: local_user_view.as_ref(),
79 .list(&mut context.pool())
81 .with_lemmy_type(LemmyErrorType::CouldntGetComments)?;
83 Ok(Json(GetCommentsResponse { comments }))