1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{blocking, comment::*, get_local_user_view_from_jwt_opt, user_show_bot_accounts};
4 use lemmy_db_queries::{from_opt_str_to_opt_enum, ListingType, SortType};
5 use lemmy_db_views::comment_view::CommentQueryBuilder;
6 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
7 use lemmy_websocket::LemmyContext;
9 #[async_trait::async_trait(?Send)]
10 impl PerformCrud for GetComments {
11 type Response = GetCommentsResponse;
15 context: &Data<LemmyContext>,
16 _websocket_id: Option<ConnectionId>,
17 ) -> Result<GetCommentsResponse, LemmyError> {
18 let data: &GetComments = &self;
19 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
21 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
22 let person_id = local_user_view.map(|u| u.person.id);
24 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
25 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
27 let community_id = data.community_id;
28 let community_name = data.community_name.to_owned();
29 let saved_only = data.saved_only;
31 let limit = data.limit;
32 let comments = blocking(context.pool(), move |conn| {
33 CommentQueryBuilder::create(conn)
34 .listing_type(listing_type)
36 .saved_only(saved_only)
37 .community_id(community_id)
38 .community_name(community_name)
39 .my_person_id(person_id)
40 .show_bot_accounts(show_bot_accounts)
46 .map_err(|_| ApiError::err("couldnt_get_comments"))?;
48 Ok(GetCommentsResponse { comments })