]> Untitled Git - lemmy.git/blob - crates/apub_receive/src/http/post.rs
Apub inbox rewrite (#1652)
[lemmy.git] / crates / apub_receive / src / http / post.rs
1 use crate::http::{create_apub_response, create_apub_tombstone_response};
2 use actix_web::{body::Body, web, HttpResponse};
3 use diesel::result::Error::NotFound;
4 use lemmy_api_common::blocking;
5 use lemmy_apub::objects::ToApub;
6 use lemmy_db_queries::Crud;
7 use lemmy_db_schema::{source::post::Post, PostId};
8 use lemmy_utils::LemmyError;
9 use lemmy_websocket::LemmyContext;
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 pub(crate) async fn get_apub_post(
19   info: web::Path<PostQuery>,
20   context: web::Data<LemmyContext>,
21 ) -> Result<HttpResponse<Body>, LemmyError> {
22   let id = PostId(info.post_id.parse::<i32>()?);
23   let post = blocking(context.pool(), move |conn| Post::read(conn, id)).await??;
24   if !post.local {
25     return Err(NotFound.into());
26   }
27
28   if !post.deleted {
29     Ok(create_apub_response(&post.to_apub(context.pool()).await?))
30   } else {
31     Ok(create_apub_tombstone_response(&post.to_tombstone()?))
32   }
33 }