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, 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)
51 .content_type("application/activity+json")
56 fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> LemmyResult<HttpResponse> {
57 let tombstone = Tombstone::new(id.into());
58 let json = serde_json::to_string_pretty(&WithContext::new(tombstone, CONTEXT.deref().clone()))?;
62 .content_type(FEDERATION_CONTENT_TYPE)
63 .status(StatusCode::GONE)
64 .content_type("application/activity+json")
69 fn err_object_not_local() -> LemmyError {
70 LemmyError::from_message("Object not local, fetch it from original instance")
73 #[derive(Deserialize)]
74 pub struct ActivityQuery {
79 /// Return the ActivityPub json representation of a local activity over HTTP.
80 #[tracing::instrument(skip_all)]
81 pub(crate) async fn get_activity(
82 info: web::Path<ActivityQuery>,
83 context: web::Data<LemmyContext>,
84 ) -> Result<HttpResponse, LemmyError> {
85 let settings = context.settings();
86 let activity_id = Url::parse(&format!(
87 "{}/activities/{}/{}",
88 settings.get_protocol_and_hostname(),
93 let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?;
95 let sensitive = activity.sensitive;
97 Err(err_object_not_local())
99 Ok(HttpResponse::Forbidden().finish())
101 create_apub_response(&activity.data)