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_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 = local_user_view
23 .map(|t| t.local_user.show_bot_accounts);
24 let person_id = local_user_view.map(|u| u.person.id);
26 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
27 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
29 let community_id = data.community_id;
30 let community_name = data.community_name.to_owned();
31 let saved_only = data.saved_only;
33 let limit = data.limit;
34 let comments = blocking(context.pool(), move |conn| {
35 CommentQueryBuilder::create(conn)
36 .listing_type(listing_type)
38 .saved_only(saved_only)
39 .community_id(community_id)
40 .community_name(community_name)
41 .my_person_id(person_id)
42 .show_bot_accounts(show_bot_accounts)
48 .map_err(|_| ApiError::err("couldnt_get_comments"))?;
50 Ok(GetCommentsResponse { comments })