]> Untitled Git - lemmy.git/blob - crates/apub/src/http/post.rs
Improved error message when attempting to fetch non-local object (fixes #2715) (...
[lemmy.git] / crates / apub / src / http / post.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response, err_object_not_local},
3   objects::post::ApubPost,
4 };
5 use activitypub_federation::traits::ApubObject;
6 use actix_web::{web, HttpResponse};
7 use lemmy_api_common::context::LemmyContext;
8 use lemmy_db_schema::{newtypes::PostId, source::post::Post, traits::Crud};
9 use lemmy_utils::error::LemmyError;
10 use serde::Deserialize;
11
12 #[derive(Deserialize)]
13 pub(crate) struct PostQuery {
14   post_id: String,
15 }
16
17 /// Return the ActivityPub json representation of a local post over HTTP.
18 #[tracing::instrument(skip_all)]
19 pub(crate) async fn get_apub_post(
20   info: web::Path<PostQuery>,
21   context: web::Data<LemmyContext>,
22 ) -> Result<HttpResponse, LemmyError> {
23   let id = PostId(info.post_id.parse::<i32>()?);
24   let post: ApubPost = Post::read(context.pool(), id).await?.into();
25   if !post.local {
26     return Err(err_object_not_local());
27   }
28
29   if !post.deleted && !post.removed {
30     Ok(create_apub_response(&post.into_apub(&context).await?))
31   } else {
32     Ok(create_apub_tombstone_response(post.ap_id.clone()))
33   }
34 }