]> Untitled Git - lemmy.git/blob - crates/apub/src/http/comment.rs
Merge branch 'main' into move_matrix_and_admin_to_person
[lemmy.git] / crates / apub / src / http / comment.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response},
3   objects::ToApub,
4 };
5 use actix_web::{body::Body, web, web::Path, HttpResponse};
6 use diesel::result::Error::NotFound;
7 use lemmy_api_structs::blocking;
8 use lemmy_db_queries::Crud;
9 use lemmy_db_schema::{source::comment::Comment, CommentId};
10 use lemmy_utils::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use serde::Deserialize;
13
14 #[derive(Deserialize)]
15 pub(crate) struct CommentQuery {
16   comment_id: String,
17 }
18
19 /// Return the ActivityPub json representation of a local comment over HTTP.
20 pub(crate) async fn get_apub_comment(
21   info: Path<CommentQuery>,
22   context: web::Data<LemmyContext>,
23 ) -> Result<HttpResponse<Body>, LemmyError> {
24   let id = CommentId(info.comment_id.parse::<i32>()?);
25   let comment = blocking(context.pool(), move |conn| Comment::read(conn, id)).await??;
26   if !comment.local {
27     return Err(NotFound.into());
28   }
29
30   if !comment.deleted {
31     Ok(create_apub_response(
32       &comment.to_apub(context.pool()).await?,
33     ))
34   } else {
35     Ok(create_apub_tombstone_response(&comment.to_tombstone()?))
36   }
37 }