2 activity_lists::SharedInboxActivities,
3 fetcher::user_or_community::UserOrCommunity,
4 protocol::objects::tombstone::Tombstone,
7 use activitypub_federation::{
8 actix_web::inbox::receive_activity,
10 protocol::context::WithContext,
11 FEDERATION_CONTENT_TYPE,
13 use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
15 use lemmy_api_common::context::LemmyContext;
16 use lemmy_db_schema::source::activity::Activity;
17 use lemmy_utils::error::{LemmyError, LemmyErrorType, LemmyResult};
18 use serde::{Deserialize, Serialize};
29 pub async fn shared_inbox(
32 data: Data<LemmyContext>,
33 ) -> LemmyResult<HttpResponse> {
34 receive_activity::<SharedInboxActivities, UserOrCommunity, LemmyContext>(request, body, &data)
38 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
41 /// actix-web doesn't allow pretty-print for json so we need to do this manually.
42 fn create_apub_response<T>(data: &T) -> LemmyResult<HttpResponse>
46 let json = serde_json::to_string_pretty(&WithContext::new(data, CONTEXT.clone()))?;
50 .content_type(FEDERATION_CONTENT_TYPE)
55 fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> LemmyResult<HttpResponse> {
56 let tombstone = Tombstone::new(id.into());
57 let json = serde_json::to_string_pretty(&WithContext::new(tombstone, CONTEXT.deref().clone()))?;
61 .content_type(FEDERATION_CONTENT_TYPE)
62 .status(StatusCode::GONE)
67 fn err_object_not_local() -> LemmyError {
68 LemmyErrorType::ObjectNotLocal.into()
71 #[derive(Deserialize)]
72 pub struct ActivityQuery {
77 /// Return the ActivityPub json representation of a local activity over HTTP.
78 #[tracing::instrument(skip_all)]
79 pub(crate) async fn get_activity(
80 info: web::Path<ActivityQuery>,
81 context: web::Data<LemmyContext>,
82 ) -> Result<HttpResponse, LemmyError> {
83 let settings = context.settings();
84 let activity_id = Url::parse(&format!(
85 "{}/activities/{}/{}",
86 settings.get_protocol_and_hostname(),
91 let activity = Activity::read_from_apub_id(&mut context.pool(), &activity_id).await?;
93 let sensitive = activity.sensitive;
95 Err(err_object_not_local())
97 Ok(HttpResponse::Forbidden().finish())
99 create_apub_response(&activity.data)