]> Untitled Git - lemmy.git/blob - crates/apub/src/http/comment.rs
Dont serve apub json for removed objects (ref #2522) (#2538)
[lemmy.git] / crates / apub / src / http / comment.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response},
3   objects::comment::ApubComment,
4 };
5 use activitypub_federation::traits::ApubObject;
6 use actix_web::{web, web::Path, HttpResponse};
7 use diesel::result::Error::NotFound;
8 use lemmy_api_common::utils::blocking;
9 use lemmy_db_schema::{newtypes::CommentId, source::comment::Comment, traits::Crud};
10 use lemmy_utils::error::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 #[tracing::instrument(skip_all)]
21 pub(crate) async fn get_apub_comment(
22   info: Path<CommentQuery>,
23   context: web::Data<LemmyContext>,
24 ) -> Result<HttpResponse, LemmyError> {
25   let id = CommentId(info.comment_id.parse::<i32>()?);
26   let comment: ApubComment = blocking(context.pool(), move |conn| Comment::read(conn, id))
27     .await??
28     .into();
29   if !comment.local {
30     return Err(NotFound.into());
31   }
32
33   if !comment.deleted && !comment.removed {
34     Ok(create_apub_response(&comment.into_apub(&**context).await?))
35   } else {
36     Ok(create_apub_tombstone_response(comment.ap_id.clone()))
37   }
38 }