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