1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{GetComments, GetCommentsResponse},
7 check_private_instance,
8 get_local_user_view_from_jwt_opt,
9 listing_type_with_site_default,
12 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
13 use lemmy_db_schema::{
14 source::{comment::Comment, community::Community, local_site::LocalSite},
15 traits::{Crud, DeleteableOrRemoveable},
17 use lemmy_db_views::comment_view::CommentQuery;
18 use lemmy_utils::{error::LemmyError, ConnectionId};
19 use lemmy_websocket::LemmyContext;
21 #[async_trait::async_trait(?Send)]
22 impl PerformCrud for GetComments {
23 type Response = GetCommentsResponse;
25 #[tracing::instrument(skip(context, _websocket_id))]
28 context: &Data<LemmyContext>,
29 _websocket_id: Option<ConnectionId>,
30 ) -> Result<GetCommentsResponse, LemmyError> {
31 let data: &GetComments = self;
33 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
35 let local_site = blocking(context.pool(), LocalSite::read).await??;
36 check_private_instance(&local_user_view, &local_site)?;
38 let community_id = data.community_id;
39 let listing_type = listing_type_with_site_default(data.type_, &local_site)?;
41 let community_actor_id = if let Some(name) = &data.community_name {
42 resolve_actor_identifier::<ApubCommunity, Community>(name, context, true)
50 let max_depth = data.max_depth;
51 let saved_only = data.saved_only;
53 let limit = data.limit;
54 let parent_id = data.parent_id;
56 // If a parent_id is given, fetch the comment to get the path
57 let parent_path = if let Some(parent_id) = parent_id {
59 blocking(context.pool(), move |conn| Comment::read(conn, parent_id))
67 let parent_path_cloned = parent_path.to_owned();
68 let post_id = data.post_id;
69 let local_user = local_user_view.map(|l| l.local_user);
70 let mut comments = blocking(context.pool(), move |conn| {
71 CommentQuery::builder()
73 .listing_type(Some(listing_type))
76 .saved_only(saved_only)
77 .community_id(community_id)
78 .community_actor_id(community_actor_id)
79 .parent_path(parent_path_cloned)
81 .local_user(local_user.as_ref())
88 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
90 // Blank out deleted or removed info
93 .filter(|cv| cv.comment.deleted || cv.comment.removed)
95 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
98 Ok(GetCommentsResponse { comments })