1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt};
5 fetcher::webfinger::webfinger_resolve,
6 objects::community::ApubCommunity,
10 from_opt_str_to_opt_enum,
11 traits::DeleteableOrRemoveable,
15 use lemmy_db_views::comment_view::CommentQueryBuilder;
16 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetComments {
21 type Response = GetCommentsResponse;
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<GetCommentsResponse, LemmyError> {
28 let data: &GetComments = self;
30 get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
32 let show_bot_accounts = local_user_view
34 .map(|t| t.local_user.show_bot_accounts);
35 let person_id = local_user_view.map(|u| u.person.id);
37 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
38 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
40 let community_id = data.community_id;
41 let community_actor_id = if let Some(name) = &data.community_name {
42 webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
48 let saved_only = data.saved_only;
50 let limit = data.limit;
51 let mut comments = blocking(context.pool(), move |conn| {
52 CommentQueryBuilder::create(conn)
53 .listing_type(listing_type)
55 .saved_only(saved_only)
56 .community_id(community_id)
57 .community_actor_id(community_actor_id)
58 .my_person_id(person_id)
59 .show_bot_accounts(show_bot_accounts)
65 .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
67 // Blank out deleted or removed info
70 .filter(|cv| cv.comment.deleted || cv.comment.removed)
72 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
75 Ok(GetCommentsResponse { comments })