]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Moving settings and secrets to context.
[lemmy.git] / crates / api_crud / src / comment / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
4 use lemmy_apub::{build_actor_id_from_shortname, EndpointType};
5 use lemmy_db_queries::{from_opt_str_to_opt_enum, DeleteableOrRemoveable, ListingType, SortType};
6 use lemmy_db_views::comment_view::CommentQueryBuilder;
7 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
8 use lemmy_websocket::LemmyContext;
9
10 #[async_trait::async_trait(?Send)]
11 impl PerformCrud for GetComments {
12   type Response = GetCommentsResponse;
13
14   async fn perform(
15     &self,
16     context: &Data<LemmyContext>,
17     _websocket_id: Option<ConnectionId>,
18   ) -> Result<GetCommentsResponse, LemmyError> {
19     let data: &GetComments = self;
20     let local_user_view =
21       get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
22
23     let show_bot_accounts = local_user_view
24       .as_ref()
25       .map(|t| t.local_user.show_bot_accounts);
26     let person_id = local_user_view.map(|u| u.person.id);
27
28     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
29     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
30
31     let community_id = data.community_id;
32     let community_actor_id = data
33       .community_name
34       .as_ref()
35       .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
36       .unwrap_or(None);
37     let saved_only = data.saved_only;
38     let page = data.page;
39     let limit = data.limit;
40     let mut comments = blocking(context.pool(), move |conn| {
41       CommentQueryBuilder::create(conn)
42         .listing_type(listing_type)
43         .sort(sort)
44         .saved_only(saved_only)
45         .community_id(community_id)
46         .community_actor_id(community_actor_id)
47         .my_person_id(person_id)
48         .show_bot_accounts(show_bot_accounts)
49         .page(page)
50         .limit(limit)
51         .list()
52     })
53     .await?
54     .map_err(|_| ApiError::err("couldnt_get_comments"))?;
55
56     // Blank out deleted or removed info
57     for cv in comments
58       .iter_mut()
59       .filter(|cv| cv.comment.deleted || cv.comment.removed)
60     {
61       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
62     }
63
64     Ok(GetCommentsResponse { comments })
65   }
66 }