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};
6 from_opt_str_to_opt_enum,
7 traits::DeleteableOrRemoveable,
11 use lemmy_db_views::comment_view::CommentQueryBuilder;
12 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
13 use lemmy_websocket::LemmyContext;
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for GetComments {
17 type Response = GetCommentsResponse;
21 context: &Data<LemmyContext>,
22 _websocket_id: Option<ConnectionId>,
23 ) -> Result<GetCommentsResponse, LemmyError> {
24 let data: &GetComments = self;
26 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
28 let show_bot_accounts = local_user_view
30 .map(|t| t.local_user.show_bot_accounts);
31 let person_id = local_user_view.map(|u| u.person.id);
33 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
34 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
36 let community_id = data.community_id;
37 let community_actor_id = data
40 .map(|t| build_actor_id_from_shortname(EndpointType::Community, t, &context.settings()).ok())
42 let saved_only = data.saved_only;
44 let limit = data.limit;
45 let mut comments = blocking(context.pool(), move |conn| {
46 CommentQueryBuilder::create(conn)
47 .listing_type(listing_type)
49 .saved_only(saved_only)
50 .community_id(community_id)
51 .community_actor_id(community_actor_id)
52 .my_person_id(person_id)
53 .show_bot_accounts(show_bot_accounts)
59 .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
61 // Blank out deleted or removed info
64 .filter(|cv| cv.comment.deleted || cv.comment.removed)
66 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
69 Ok(GetCommentsResponse { comments })