]> Untitled Git - lemmy.git/blob - crates/apub/src/http/post.rs
Add diesel_async, get rid of blocking function (#2510)
[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 activitypub_federation::traits::ApubObject;
6 use actix_web::{web, HttpResponse};
7 use diesel::result::Error::NotFound;
8 use lemmy_db_schema::{newtypes::PostId, source::post::Post, traits::Crud};
9 use lemmy_utils::error::LemmyError;
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
12
13 #[derive(Deserialize)]
14 pub(crate) struct PostQuery {
15   post_id: String,
16 }
17
18 /// Return the ActivityPub json representation of a local post over HTTP.
19 #[tracing::instrument(skip_all)]
20 pub(crate) async fn get_apub_post(
21   info: web::Path<PostQuery>,
22   context: web::Data<LemmyContext>,
23 ) -> Result<HttpResponse, LemmyError> {
24   let id = PostId(info.post_id.parse::<i32>()?);
25   let post: ApubPost = Post::read(context.pool(), id).await?.into();
26   if !post.local {
27     return Err(NotFound.into());
28   }
29
30   if !post.deleted && !post.removed {
31     Ok(create_apub_response(&post.into_apub(&context).await?))
32   } else {
33     Ok(create_apub_tombstone_response(post.ap_id.clone()))
34   }
35 }