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