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;
10 #[async_trait::async_trait(?Send)]
11 impl PerformCrud for GetComments {
12 type Response = GetCommentsResponse;
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?;
22 let show_bot_accounts = local_user_view
24 .map(|t| t.local_user.show_bot_accounts);
25 let person_id = local_user_view.map(|u| u.person.id);
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_);
30 let community_id = data.community_id;
31 let community_actor_id = data
34 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t).ok())
36 let saved_only = data.saved_only;
38 let limit = data.limit;
39 let mut comments = blocking(context.pool(), move |conn| {
40 CommentQueryBuilder::create(conn)
41 .listing_type(listing_type)
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)
53 .map_err(|_| ApiError::err("couldnt_get_comments"))?;
55 // Blank out deleted or removed info
58 .filter(|cv| cv.comment.deleted || cv.comment.removed)
60 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
63 Ok(GetCommentsResponse { comments })