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