]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/list.rs
Adding check for requests with no id or name, adding max limit. (#2265)
[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::{source::community::Community, traits::DeleteableOrRemoveable};
14 use lemmy_db_views::comment_view::CommentQueryBuilder;
15 use lemmy_utils::{error::LemmyError, ConnectionId};
16 use lemmy_websocket::LemmyContext;
17
18 #[async_trait::async_trait(?Send)]
19 impl PerformCrud for GetComments {
20   type Response = GetCommentsResponse;
21
22   #[tracing::instrument(skip(context, _websocket_id))]
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     _websocket_id: Option<ConnectionId>,
27   ) -> Result<GetCommentsResponse, LemmyError> {
28     let data: &GetComments = self;
29     let local_user_view =
30       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
31         .await?;
32
33     check_private_instance(&local_user_view, context.pool()).await?;
34
35     let show_bot_accounts = local_user_view
36       .as_ref()
37       .map(|t| t.local_user.show_bot_accounts);
38     let person_id = local_user_view.map(|u| u.person.id);
39
40     let community_id = data.community_id;
41     let listing_type = listing_type_with_site_default(data.type_, context.pool()).await?;
42
43     let community_actor_id = if let Some(name) = &data.community_name {
44       resolve_actor_identifier::<ApubCommunity, Community>(name, context)
45         .await
46         .ok()
47         .map(|c| c.actor_id)
48     } else {
49       None
50     };
51     let sort = data.sort;
52     let saved_only = data.saved_only;
53     let page = data.page;
54     let limit = data.limit;
55     let mut comments = blocking(context.pool(), move |conn| {
56       CommentQueryBuilder::create(conn)
57         .listing_type(listing_type)
58         .sort(sort)
59         .saved_only(saved_only)
60         .community_id(community_id)
61         .community_actor_id(community_actor_id)
62         .my_person_id(person_id)
63         .show_bot_accounts(show_bot_accounts)
64         .page(page)
65         .limit(limit)
66         .list()
67     })
68     .await?
69     .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
70
71     // Blank out deleted or removed info
72     for cv in comments
73       .iter_mut()
74       .filter(|cv| cv.comment.deleted || cv.comment.removed)
75     {
76       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
77     }
78
79     Ok(GetCommentsResponse { comments })
80   }
81 }