]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Rewrite fetcher (#1792)
[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 = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
21
22     let show_bot_accounts = local_user_view
23       .as_ref()
24       .map(|t| t.local_user.show_bot_accounts);
25     let person_id = local_user_view.map(|u| u.person.id);
26
27     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
28     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
29
30     let community_id = data.community_id;
31     let community_actor_id = data
32       .community_name
33       .as_ref()
34       .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
35       .unwrap_or(None);
36     let saved_only = data.saved_only;
37     let page = data.page;
38     let limit = data.limit;
39     let mut comments = blocking(context.pool(), move |conn| {
40       CommentQueryBuilder::create(conn)
41         .listing_type(listing_type)
42         .sort(sort)
43         .saved_only(saved_only)
44         .community_id(community_id)
45         .community_actor_id(community_actor_id)
46         .my_person_id(person_id)
47         .show_bot_accounts(show_bot_accounts)
48         .page(page)
49         .limit(limit)
50         .list()
51     })
52     .await?
53     .map_err(|_| ApiError::err("couldnt_get_comments"))?;
54
55     // Blank out deleted or removed info
56     for cv in comments
57       .iter_mut()
58       .filter(|cv| cv.comment.deleted || cv.comment.removed)
59     {
60       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
61     }
62
63     Ok(GetCommentsResponse { comments })
64   }
65 }