]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Some formatting
[lemmy.git] / crates / api_crud / src / comment / read.rs
1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   comment::*,
6   get_local_user_view_from_jwt_opt,
7   user_show_bot_accounts,
8 };
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;
13
14 #[async_trait::async_trait(?Send)]
15 impl PerformCrud for GetComments {
16   type Response = GetCommentsResponse;
17
18   async fn perform(
19     &self,
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?;
25
26     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
27     let person_id = local_user_view.map(|u| u.person.id);
28
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_);
31
32     let community_id = data.community_id;
33     let community_name = data.community_name.to_owned();
34     let saved_only = data.saved_only;
35     let page = data.page;
36     let limit = data.limit;
37     let comments = blocking(context.pool(), move |conn| {
38       CommentQueryBuilder::create(conn)
39         .listing_type(listing_type)
40         .sort(sort)
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)
46         .page(page)
47         .limit(limit)
48         .list()
49     })
50     .await?
51     .map_err(|_| ApiError::err("couldnt_get_comments"))?;
52
53     Ok(GetCommentsResponse { comments })
54   }
55 }