]> Untitled Git - lemmy.git/blob - crates/api_crud/src/comment/read.rs
Split apart api files (#2216)
[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   check_private_instance,
6   comment::*,
7   get_local_user_view_from_jwt_opt,
8 };
9 use lemmy_db_views::comment_view::CommentView;
10 use lemmy_utils::{ConnectionId, LemmyError};
11 use lemmy_websocket::LemmyContext;
12
13 #[async_trait::async_trait(?Send)]
14 impl PerformCrud for GetComment {
15   type Response = CommentResponse;
16
17   #[tracing::instrument(skip(context, _websocket_id))]
18   async fn perform(
19     &self,
20     context: &Data<LemmyContext>,
21     _websocket_id: Option<ConnectionId>,
22   ) -> Result<Self::Response, LemmyError> {
23     let data = self;
24     let local_user_view =
25       get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
26         .await?;
27
28     check_private_instance(&local_user_view, context.pool()).await?;
29
30     let person_id = local_user_view.map(|u| u.person.id);
31     let id = data.id;
32     let comment_view = blocking(context.pool(), move |conn| {
33       CommentView::read(conn, id, person_id)
34     })
35     .await?
36     .map_err(|e| LemmyError::from_error_message(e, "couldnt_find_comment"))?;
37
38     Ok(Self::Response {
39       comment_view,
40       form_id: None,
41       recipient_ids: Vec::new(),
42     })
43   }
44 }