]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/list.rs
Check user accepted before sending jwt in password reset (fixes #2591) (#2597)
[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     check_private_instance,
7     get_local_user_view_from_jwt_opt,
8     listing_type_with_site_default,
9   },
10 };
11 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
12 use lemmy_db_schema::{
13   source::{comment::Comment, community::Community, local_site::LocalSite},
14   traits::{Crud, DeleteableOrRemoveable},
15 };
16 use lemmy_db_views::comment_view::CommentQuery;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::LemmyContext;
19
20 #[async_trait::async_trait(?Send)]
21 impl PerformCrud for GetComments {
22   type Response = GetCommentsResponse;
23
24   #[tracing::instrument(skip(context, _websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     _websocket_id: Option<ConnectionId>,
29   ) -> Result<GetCommentsResponse, LemmyError> {
30     let data: &GetComments = self;
31     let local_user_view =
32       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
33         .await?;
34     let local_site = LocalSite::read(context.pool()).await?;
35     check_private_instance(&local_user_view, &local_site)?;
36
37     let community_id = data.community_id;
38     let listing_type = listing_type_with_site_default(data.type_, &local_site)?;
39
40     let community_actor_id = if let Some(name) = &data.community_name {
41       resolve_actor_identifier::<ApubCommunity, Community>(name, context, true)
42         .await
43         .ok()
44         .map(|c| c.actor_id)
45     } else {
46       None
47     };
48     let sort = data.sort;
49     let max_depth = data.max_depth;
50     let saved_only = data.saved_only;
51     let page = data.page;
52     let limit = data.limit;
53     let parent_id = data.parent_id;
54
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(context.pool(), parent_id).await?.path)
58     } else {
59       None
60     };
61
62     let parent_path_cloned = parent_path.clone();
63     let post_id = data.post_id;
64     let local_user = local_user_view.map(|l| l.local_user);
65     let mut comments = CommentQuery::builder()
66       .pool(context.pool())
67       .listing_type(Some(listing_type))
68       .sort(sort)
69       .max_depth(max_depth)
70       .saved_only(saved_only)
71       .community_id(community_id)
72       .community_actor_id(community_actor_id)
73       .parent_path(parent_path_cloned)
74       .post_id(post_id)
75       .local_user(local_user.as_ref())
76       .page(page)
77       .limit(limit)
78       .build()
79       .list()
80       .await
81       .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
82
83     // Blank out deleted or removed info
84     for cv in comments
85       .iter_mut()
86       .filter(|cv| cv.comment.deleted || cv.comment.removed)
87     {
88       cv.comment = cv.clone().comment.blank_out_deleted_or_removed_info();
89     }
90
91     Ok(GetCommentsResponse { comments })
92   }
93 }