]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Adding listing_type to search.
[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::{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;
14
15 #[async_trait::async_trait(?Send)]
16 impl PerformCrud for GetComments {
17   type Response = GetCommentsResponse;
18
19   async fn perform(
20     &self,
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?;
26
27     let show_bot_accounts = user_show_bot_accounts(&local_user_view);
28     let person_id = local_user_view.map(|u| u.person.id);
29
30     let listing_type = ListingType::from_str(&data.type_)?;
31     let sort = SortType::from_str(&data.sort)?;
32
33     let community_id = data.community_id;
34     let community_name = data.community_name.to_owned();
35     let saved_only = data.saved_only;
36     let page = data.page;
37     let limit = data.limit;
38     let comments = blocking(context.pool(), move |conn| {
39       CommentQueryBuilder::create(conn)
40         .listing_type(&listing_type)
41         .sort(&sort)
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)
47         .page(page)
48         .limit(limit)
49         .list()
50     })
51     .await?
52     .map_err(|_| ApiError::err("couldnt_get_comments"))?;
53
54     Ok(GetCommentsResponse { comments })
55   }
56 }