]> Untitled Git - lemmy.git/blob - crates/apub/src/http/post.rs
Don't drop error context when adding a message to errors (#1958)
[lemmy.git] / crates / apub / src / http / post.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response},
3   objects::post::ApubPost,
4 };
5 use actix_web::{body::AnyBody, web, HttpResponse};
6 use diesel::result::Error::NotFound;
7 use lemmy_api_common::blocking;
8 use lemmy_apub_lib::traits::ApubObject;
9 use lemmy_db_schema::{newtypes::PostId, source::post::Post, traits::Crud};
10 use lemmy_utils::LemmyError;
11 use lemmy_websocket::LemmyContext;
12 use serde::Deserialize;
13
14 #[derive(Deserialize)]
15 pub(crate) struct PostQuery {
16   post_id: String,
17 }
18
19 /// Return the ActivityPub json representation of a local post over HTTP.
20 #[tracing::instrument(skip_all)]
21 pub(crate) async fn get_apub_post(
22   info: web::Path<PostQuery>,
23   context: web::Data<LemmyContext>,
24 ) -> Result<HttpResponse<AnyBody>, LemmyError> {
25   let id = PostId(info.post_id.parse::<i32>()?);
26   let post: ApubPost = blocking(context.pool(), move |conn| Post::read(conn, id))
27     .await??
28     .into();
29   if !post.local {
30     return Err(NotFound.into());
31   }
32
33   if !post.deleted {
34     Ok(create_apub_response(&post.into_apub(&context).await?))
35   } else {
36     Ok(create_apub_tombstone_response(&post.to_tombstone()?))
37   }
38 }