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