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::get_actor_id_from_name;
5 use lemmy_apub_lib::webfinger::WebfingerType;
7 from_opt_str_to_opt_enum,
8 traits::DeleteableOrRemoveable,
12 use lemmy_db_views::comment_view::CommentQueryBuilder;
13 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
14 use lemmy_websocket::LemmyContext;
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for GetComments {
18 type Response = GetCommentsResponse;
22 context: &Data<LemmyContext>,
23 _websocket_id: Option<ConnectionId>,
24 ) -> Result<GetCommentsResponse, LemmyError> {
25 let data: &GetComments = self;
27 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
29 let show_bot_accounts = local_user_view
31 .map(|t| t.local_user.show_bot_accounts);
32 let person_id = local_user_view.map(|u| u.person.id);
34 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
35 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
37 let community_id = data.community_id;
38 let community_actor_id = if let Some(name) = &data.community_name {
39 get_actor_id_from_name(WebfingerType::Group, name, context)
45 let saved_only = data.saved_only;
47 let limit = data.limit;
48 let mut comments = blocking(context.pool(), move |conn| {
49 CommentQueryBuilder::create(conn)
50 .listing_type(listing_type)
52 .saved_only(saved_only)
53 .community_id(community_id)
54 .community_actor_id(community_actor_id)
55 .my_person_id(person_id)
56 .show_bot_accounts(show_bot_accounts)
62 .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
64 // Blank out deleted or removed info
67 .filter(|cv| cv.comment.deleted || cv.comment.removed)
69 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
72 Ok(GetCommentsResponse { comments })