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