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::{ListingType, SortType};
10 use lemmy_db_views::comment_view::CommentQueryBuilder;
11 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
12 use lemmy_websocket::LemmyContext;
13 use std::str::FromStr;
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for GetComments {
17 type Response = GetCommentsResponse;
21 context: &Data<LemmyContext>,
22 _websocket_id: Option<ConnectionId>,
23 ) -> Result<GetCommentsResponse, LemmyError> {
24 let data: &GetComments = &self;
25 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
27 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
28 let person_id = local_user_view.map(|u| u.person.id);
30 let type_ = ListingType::from_str(&data.type_)?;
31 let sort = SortType::from_str(&data.sort)?;
33 let community_id = data.community_id;
34 let community_name = data.community_name.to_owned();
35 let saved_only = data.saved_only;
37 let limit = data.limit;
38 let comments = blocking(context.pool(), move |conn| {
39 CommentQueryBuilder::create(conn)
42 .saved_only(saved_only)
43 .community_id(community_id)
44 .community_name(community_name)
45 .my_person_id(person_id)
46 .show_bot_accounts(show_bot_accounts)
52 .map_err(|_| ApiError::err("couldnt_get_comments"))?;
54 Ok(GetCommentsResponse { comments })