2 activity_lists::SharedInboxActivities,
3 fetcher::user_or_community::UserOrCommunity,
6 protocol::objects::tombstone::Tombstone,
9 use activitypub_federation::{
10 core::inbox::receive_activity,
12 deser::context::WithContext,
13 traits::{ActivityHandler, Actor, ApubObject},
14 APUB_JSON_CONTENT_TYPE,
16 use actix_web::{web, HttpRequest, HttpResponse};
18 use lemmy_api_common::utils::blocking;
19 use lemmy_db_schema::source::activity::Activity;
20 use lemmy_utils::error::LemmyError;
21 use lemmy_websocket::LemmyContext;
22 use once_cell::sync::OnceCell;
23 use serde::{de::DeserializeOwned, Deserialize, Serialize};
24 use serde_json::Value;
26 use tracing::{debug, log::info};
36 #[tracing::instrument(skip_all)]
37 pub async fn shared_inbox(
40 context: web::Data<LemmyContext>,
41 ) -> Result<HttpResponse, LemmyError> {
42 receive_lemmy_activity::<SharedInboxActivities, UserOrCommunity>(request, payload, context).await
45 pub async fn receive_lemmy_activity<Activity, ActorT>(
48 context: web::Data<LemmyContext>,
49 ) -> Result<HttpResponse, LemmyError>
51 Activity: ActivityHandler<DataType = LemmyContext, Error = LemmyError>
55 ActorT: ApubObject<DataType = LemmyContext, Error = LemmyError> + Actor + Send + 'static,
56 for<'de2> <ActorT as ApubObject>::ApubType: serde::Deserialize<'de2>,
58 let activity_value: Value = serde_json::from_str(&payload)?;
59 debug!("Received activity {:#}", payload.as_str());
60 let activity: Activity = serde_json::from_value(activity_value.clone())?;
61 // Log the activity, so we avoid receiving and parsing it twice.
62 let insert = insert_activity(activity.id(), activity_value, false, true, context.pool()).await?;
64 debug!("Received duplicate activity {}", activity.id().to_string());
65 return Ok(HttpResponse::BadRequest().finish());
67 info!("Received activity {}", payload);
69 static DATA: OnceCell<Data<LemmyContext>> = OnceCell::new();
70 let data = DATA.get_or_init(|| Data::new(context.get_ref().clone()));
71 receive_activity::<Activity, ActorT, LemmyContext>(
74 local_instance(&context),
80 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
82 fn create_apub_response<T>(data: &T) -> HttpResponse
87 .content_type(APUB_JSON_CONTENT_TYPE)
88 .json(WithContext::new(data, CONTEXT.deref().clone()))
91 fn create_json_apub_response(data: serde_json::Value) -> HttpResponse {
93 .content_type(APUB_JSON_CONTENT_TYPE)
97 fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
98 let tombstone = Tombstone::new(id.into());
100 .content_type(APUB_JSON_CONTENT_TYPE)
101 .status(StatusCode::GONE)
102 .json(WithContext::new(tombstone, CONTEXT.deref().clone()))
105 #[derive(Deserialize)]
106 pub struct ActivityQuery {
111 /// Return the ActivityPub json representation of a local activity over HTTP.
112 #[tracing::instrument(skip_all)]
113 pub(crate) async fn get_activity(
114 info: web::Path<ActivityQuery>,
115 context: web::Data<LemmyContext>,
116 ) -> Result<HttpResponse, LemmyError> {
117 let settings = context.settings();
118 let activity_id = Url::parse(&format!(
119 "{}/activities/{}/{}",
120 settings.get_protocol_and_hostname(),
125 let activity = blocking(context.pool(), move |conn| {
126 Activity::read_from_apub_id(conn, &activity_id)
130 let sensitive = activity.sensitive.unwrap_or(true);
131 if !activity.local || sensitive {
132 Ok(HttpResponse::NotFound().finish())
134 Ok(create_json_apub_response(activity.data))