]> Untitled Git - lemmy.git/blob - crates/apub/src/http/comment.rs
Improved error message when attempting to fetch non-local object (fixes #2715) (...
[lemmy.git] / crates / apub / src / http / comment.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response, err_object_not_local},
3   objects::comment::ApubComment,
4 };
5 use activitypub_federation::traits::ApubObject;
6 use actix_web::{web, web::Path, HttpResponse};
7 use lemmy_api_common::context::LemmyContext;
8 use lemmy_db_schema::{newtypes::CommentId, source::comment::Comment, traits::Crud};
9 use lemmy_utils::error::LemmyError;
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 #[tracing::instrument(skip_all)]
19 pub(crate) async fn get_apub_comment(
20   info: Path<CommentQuery>,
21   context: web::Data<LemmyContext>,
22 ) -> Result<HttpResponse, LemmyError> {
23   let id = CommentId(info.comment_id.parse::<i32>()?);
24   let comment: ApubComment = Comment::read(context.pool(), id).await?.into();
25   if !comment.local {
26     return Err(err_object_not_local());
27   }
28
29   if !comment.deleted && !comment.removed {
30     Ok(create_apub_response(&comment.into_apub(&context).await?))
31   } else {
32     Ok(create_apub_tombstone_response(comment.ap_id.clone()))
33   }
34 }