]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Adding a GetComment endpoint. Fixes #1919 (#1944)
[lemmy.git] / crates / api_crud / src / comment / read.rs
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_apub::{
5   fetcher::webfinger::webfinger_resolve,
6   objects::community::ApubCommunity,
7   EndpointType,
8 };
9 use lemmy_db_schema::{
10   from_opt_str_to_opt_enum,
11   traits::DeleteableOrRemoveable,
12   ListingType,
13   SortType,
14 };
15 use lemmy_db_views::comment_view::{CommentQueryBuilder, CommentView};
16 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
17 use lemmy_websocket::LemmyContext;
18
19 #[async_trait::async_trait(?Send)]
20 impl PerformCrud for GetComment {
21   type Response = CommentResponse;
22
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     _websocket_id: Option<ConnectionId>,
27   ) -> Result<Self::Response, LemmyError> {
28     let data = self;
29     let local_user_view =
30       get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
31
32     let person_id = local_user_view.map(|u| u.person.id);
33     let id = data.id;
34     let comment_view = blocking(context.pool(), move |conn| {
35       CommentView::read(conn, id, person_id)
36     })
37     .await?
38     .map_err(|e| ApiError::err("couldnt_find_comment", e))?;
39
40     Ok(Self::Response {
41       comment_view,
42       form_id: None,
43       recipient_ids: Vec::new(),
44     })
45   }
46 }
47
48 #[async_trait::async_trait(?Send)]
49 impl PerformCrud for GetComments {
50   type Response = GetCommentsResponse;
51
52   async fn perform(
53     &self,
54     context: &Data<LemmyContext>,
55     _websocket_id: Option<ConnectionId>,
56   ) -> Result<GetCommentsResponse, LemmyError> {
57     let data: &GetComments = self;
58     let local_user_view =
59       get_local_user_view_from_jwt_opt(&data.auth, context.pool(), context.secret()).await?;
60
61     let show_bot_accounts = local_user_view
62       .as_ref()
63       .map(|t| t.local_user.show_bot_accounts);
64     let person_id = local_user_view.map(|u| u.person.id);
65
66     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
67     let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.type_);
68
69     let community_id = data.community_id;
70     let community_actor_id = if let Some(name) = &data.community_name {
71       webfinger_resolve::<ApubCommunity>(name, EndpointType::Community, context, &mut 0)
72         .await
73         .ok()
74     } else {
75       None
76     };
77     let saved_only = data.saved_only;
78     let page = data.page;
79     let limit = data.limit;
80     let mut comments = blocking(context.pool(), move |conn| {
81       CommentQueryBuilder::create(conn)
82         .listing_type(listing_type)
83         .sort(sort)
84         .saved_only(saved_only)
85         .community_id(community_id)
86         .community_actor_id(community_actor_id)
87         .my_person_id(person_id)
88         .show_bot_accounts(show_bot_accounts)
89         .page(page)
90         .limit(limit)
91         .list()
92     })
93     .await?
94     .map_err(|e| ApiError::err("couldnt_get_comments", e))?;
95
96     // Blank out deleted or removed info
97     for cv in comments
98       .iter_mut()
99       .filter(|cv| cv.comment.deleted || cv.comment.removed)
100     {
101       cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
102     }
103
104     Ok(GetCommentsResponse { comments })
105   }
106 }