1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{GetComments, GetCommentsResponse},
5 utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
9 source::community::Community,
10 traits::DeleteableOrRemoveable,
11 utils::{from_opt_str_to_opt_enum, ListingType, SortType},
13 use lemmy_db_views::comment_view::CommentQueryBuilder;
14 use lemmy_utils::{ConnectionId, LemmyError};
15 use lemmy_websocket::LemmyContext;
17 #[async_trait::async_trait(?Send)]
18 impl PerformCrud for GetComments {
19 type Response = GetCommentsResponse;
21 #[tracing::instrument(skip(context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<GetCommentsResponse, LemmyError> {
27 let data: &GetComments = self;
29 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
32 check_private_instance(&local_user_view, context.pool()).await?;
34 let show_bot_accounts = local_user_view
36 .map(|t| t.local_user.show_bot_accounts);
37 let person_id = local_user_view.map(|u| u.person.id);
39 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
40 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
42 let community_id = data.community_id;
43 let community_actor_id = if let Some(name) = &data.community_name {
44 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
51 let saved_only = data.saved_only;
53 let limit = data.limit;
54 let mut comments = blocking(context.pool(), move |conn| {
55 CommentQueryBuilder::create(conn)
56 .listing_type(listing_type)
58 .saved_only(saved_only)
59 .community_id(community_id)
60 .community_actor_id(community_actor_id)
61 .my_person_id(person_id)
62 .show_bot_accounts(show_bot_accounts)
68 .map_err(|e| LemmyError::from_error_message(e, "couldnt_get_comments"))?;
70 // Blank out deleted or removed info
73 .filter(|cv| cv.comment.deleted || cv.comment.removed)
75 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
78 Ok(GetCommentsResponse { comments })