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_db_schema::source::activity::Activity;
19 use lemmy_utils::error::LemmyError;
20 use lemmy_websocket::LemmyContext;
21 use once_cell::sync::OnceCell;
22 use serde::{de::DeserializeOwned, Deserialize, Serialize};
23 use serde_json::Value;
25 use tracing::{debug, log::info};
35 pub async fn shared_inbox(
38 context: web::Data<LemmyContext>,
39 ) -> Result<HttpResponse, LemmyError> {
40 receive_lemmy_activity::<SharedInboxActivities, UserOrCommunity>(request, payload, context).await
43 pub async fn receive_lemmy_activity<Activity, ActorT>(
46 context: web::Data<LemmyContext>,
47 ) -> Result<HttpResponse, LemmyError>
49 Activity: ActivityHandler<DataType = LemmyContext, Error = LemmyError>
53 ActorT: ApubObject<DataType = LemmyContext, Error = LemmyError> + Actor + Send + 'static,
54 for<'de2> <ActorT as ApubObject>::ApubType: serde::Deserialize<'de2>,
56 static DATA: OnceCell<Data<LemmyContext>> = OnceCell::new();
57 let activity_value: Value = serde_json::from_str(&payload)?;
58 debug!("Parsing activity {}", 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 let data = DATA.get_or_init(|| Data::new(context.get_ref().clone()));
69 receive_activity::<Activity, ActorT, LemmyContext>(
72 local_instance(&context).await,
78 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
80 fn create_apub_response<T>(data: &T) -> HttpResponse
85 .content_type(APUB_JSON_CONTENT_TYPE)
86 .json(WithContext::new(data, CONTEXT.deref().clone()))
89 fn create_json_apub_response(data: serde_json::Value) -> HttpResponse {
91 .content_type(APUB_JSON_CONTENT_TYPE)
95 fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
96 let tombstone = Tombstone::new(id.into());
98 .content_type(APUB_JSON_CONTENT_TYPE)
99 .status(StatusCode::GONE)
100 .json(WithContext::new(tombstone, CONTEXT.deref().clone()))
103 #[derive(Deserialize)]
104 pub struct ActivityQuery {
109 /// Return the ActivityPub json representation of a local activity over HTTP.
110 #[tracing::instrument(skip_all)]
111 pub(crate) async fn get_activity(
112 info: web::Path<ActivityQuery>,
113 context: web::Data<LemmyContext>,
114 ) -> Result<HttpResponse, LemmyError> {
115 let settings = context.settings();
116 let activity_id = Url::parse(&format!(
117 "{}/activities/{}/{}",
118 settings.get_protocol_and_hostname(),
123 let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?;
125 let sensitive = activity.sensitive.unwrap_or(true);
126 if !activity.local || sensitive {
127 Ok(HttpResponse::NotFound().finish())
129 Ok(create_json_apub_response(activity.data))