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 let activity: Activity = serde_json::from_value(activity_value.clone())?;
60 // Log the activity, so we avoid receiving and parsing it twice.
61 let insert = insert_activity(activity.id(), activity_value, false, true, context.pool()).await?;
63 debug!("Received duplicate activity {}", activity.id().to_string());
64 return Ok(HttpResponse::BadRequest().finish());
66 info!("Received activity {}", payload);
68 static DATA: OnceCell<Data<LemmyContext>> = OnceCell::new();
69 let data = DATA.get_or_init(|| Data::new(context.get_ref().clone()));
70 receive_activity::<Activity, ActorT, LemmyContext>(
73 local_instance(&context),
79 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
81 fn create_apub_response<T>(data: &T) -> HttpResponse
86 .content_type(APUB_JSON_CONTENT_TYPE)
87 .json(WithContext::new(data, CONTEXT.deref().clone()))
90 fn create_json_apub_response(data: serde_json::Value) -> HttpResponse {
92 .content_type(APUB_JSON_CONTENT_TYPE)
96 fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
97 let tombstone = Tombstone::new(id.into());
99 .content_type(APUB_JSON_CONTENT_TYPE)
100 .status(StatusCode::GONE)
101 .json(WithContext::new(tombstone, CONTEXT.deref().clone()))
104 #[derive(Deserialize)]
105 pub struct ActivityQuery {
110 /// Return the ActivityPub json representation of a local activity over HTTP.
111 #[tracing::instrument(skip_all)]
112 pub(crate) async fn get_activity(
113 info: web::Path<ActivityQuery>,
114 context: web::Data<LemmyContext>,
115 ) -> Result<HttpResponse, LemmyError> {
116 let settings = context.settings();
117 let activity_id = Url::parse(&format!(
118 "{}/activities/{}/{}",
119 settings.get_protocol_and_hostname(),
124 let activity = blocking(context.pool(), move |conn| {
125 Activity::read_from_apub_id(conn, &activity_id)
129 let sensitive = activity.sensitive.unwrap_or(true);
130 if !activity.local || sensitive {
131 Ok(HttpResponse::NotFound().finish())
133 Ok(create_json_apub_response(activity.data))