]> Untitled Git - lemmy.git/blob - lemmy_apub/src/http/post.rs
563af8456d9d4234739979aeb942a6cd2b983bb1
[lemmy.git] / lemmy_apub / src / http / post.rs
1 use crate::{
2   http::{create_apub_response, create_apub_tombstone_response},
3   objects::ToApub,
4 };
5 use actix_web::{body::Body, web, HttpResponse};
6 use diesel::result::Error::NotFound;
7 use lemmy_db::source::post::Post;
8 use lemmy_structs::blocking;
9 use lemmy_utils::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
12
13 #[derive(Deserialize)]
14 pub struct PostQuery {
15   post_id: String,
16 }
17
18 /// Return the ActivityPub json representation of a local post over HTTP.
19 pub async fn get_apub_post(
20   info: web::Path<PostQuery>,
21   context: web::Data<LemmyContext>,
22 ) -> Result<HttpResponse<Body>, LemmyError> {
23   let id = info.post_id.parse::<i32>()?;
24   let post = blocking(context.pool(), move |conn| Post::read(conn, id)).await??;
25   if !post.local {
26     return Err(NotFound.into());
27   }
28
29   if !post.deleted {
30     Ok(create_apub_response(&post.to_apub(context.pool()).await?))
31   } else {
32     Ok(create_apub_tombstone_response(&post.to_tombstone()?))
33   }
34 }