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::{ListingType, SortType};
5 use lemmy_db_views::comment_view::CommentQueryBuilder;
6 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
7 use lemmy_websocket::LemmyContext;
10 #[async_trait::async_trait(?Send)]
11 impl PerformCrud for GetComments {
12 type Response = GetCommentsResponse;
16 context: &Data<LemmyContext>,
17 _websocket_id: Option<ConnectionId>,
18 ) -> Result<GetCommentsResponse, LemmyError> {
19 let data: &GetComments = &self;
20 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
21 let person_id = local_user_view.map(|u| u.person.id);
23 let type_ = ListingType::from_str(&data.type_)?;
24 let sort = SortType::from_str(&data.sort)?;
26 let community_id = data.community_id;
27 let community_name = data.community_name.to_owned();
28 let saved_only = data.saved_only;
30 let limit = data.limit;
31 let comments = blocking(context.pool(), move |conn| {
32 CommentQueryBuilder::create(conn)
35 .saved_only(saved_only)
36 .community_id(community_id)
37 .community_name(community_name)
38 .my_person_id(person_id)
44 .map_err(|_| ApiError::err("couldnt_get_comments"))?;
46 Ok(GetCommentsResponse { comments })