use crate::{
+ activity_lists::SharedInboxActivities,
check_is_apub_id_valid,
- extensions::signatures::verify_signature,
- fetcher::get_or_fetch_and_upsert_actor,
- http::{
- community::{receive_group_inbox, GroupInboxActivities},
- person::{receive_person_inbox, PersonInboxActivities},
- },
+ context::WithContext,
+ fetcher::user_or_community::UserOrCommunity,
+ http::{community::receive_group_inbox, person::receive_person_inbox},
insert_activity,
- APUB_JSON_CONTENT_TYPE,
};
use actix_web::{
body::Body,
use futures::StreamExt;
use http::StatusCode;
use lemmy_api_common::blocking;
-use lemmy_apub_lib::{ActivityFields, ActivityHandler};
-use lemmy_db_queries::{source::activity::Activity_, DbPool};
+use lemmy_apub_lib::{
+ data::Data,
+ object_id::ObjectId,
+ signatures::verify_signature,
+ traits::{ActivityHandler, ActorType},
+ APUB_JSON_CONTENT_TYPE,
+};
use lemmy_db_schema::source::activity::Activity;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
-use log::{info, trace};
+use log::info;
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, io::Read};
use url::Url;
mod post;
pub mod routes;
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
-#[serde(untagged)]
-pub enum SharedInboxActivities {
- GroupInboxActivities(GroupInboxActivities),
- // Note, pm activities need to be at the end, otherwise comments will end up here. We can probably
- // avoid this problem by replacing createpm.object with our own struct, instead of NoteExt.
- PersonInboxActivities(PersonInboxActivities),
-}
-
pub async fn shared_inbox(
request: HttpRequest,
payload: Payload,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
let unparsed = payload_to_string(payload).await?;
- trace!("Received shared inbox activity {}", unparsed);
- let activity = serde_json::from_str::<SharedInboxActivities>(&unparsed)?;
- match activity {
+ info!("Received shared inbox activity {}", unparsed);
+ let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
+ let activity = serde_json::from_str::<WithContext<SharedInboxActivities>>(&unparsed)?;
+ match activity.inner() {
SharedInboxActivities::GroupInboxActivities(g) => {
- receive_group_inbox(g, request, &context).await
+ receive_group_inbox(*g, activity_data, request, &context).await
}
SharedInboxActivities::PersonInboxActivities(p) => {
- receive_person_inbox(p, request, &context).await
+ receive_person_inbox(*p, activity_data, request, &context).await
}
}
}
Ok(unparsed)
}
+#[derive(Clone, Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct ActivityCommonFields {
+ pub(crate) id: Url,
+ pub(crate) actor: Url,
+}
+
// TODO: move most of this code to library
async fn receive_activity<'a, T>(
request: HttpRequest,
activity: T,
+ activity_data: ActivityCommonFields,
context: &LemmyContext,
) -> Result<HttpResponse, LemmyError>
where
- T: ActivityHandler
- + ActivityFields
+ T: ActivityHandler<DataType = LemmyContext>
+ Clone
+ Deserialize<'a>
+ Serialize
+ Send
+ 'static,
{
+ check_is_apub_id_valid(&activity_data.actor, false, &context.settings())?;
let request_counter = &mut 0;
- let actor =
- get_or_fetch_and_upsert_actor(activity.actor().clone(), context, request_counter).await?;
- verify_signature(&request, &actor.public_key().context(location_info!())?)?;
+ let actor = ObjectId::<UserOrCommunity>::new(activity_data.actor)
+ .dereference(context, request_counter)
+ .await?;
+ verify_signature(&request, &actor.public_key())?;
- // Do nothing if we received the same activity before
- if is_activity_already_known(context.pool(), activity.id_unchecked()).await? {
- return Ok(HttpResponse::Ok().finish());
- }
- check_is_apub_id_valid(activity.actor(), false, &context.settings())?;
- info!("Verifying activity {}", activity.id_unchecked().to_string());
- activity.verify(context, request_counter).await?;
- assert_activity_not_local(&activity, &context.settings().hostname)?;
+ info!("Verifying activity {}", activity_data.id.to_string());
+ activity
+ .verify(&Data::new(context.clone()), request_counter)
+ .await?;
+ assert_activity_not_local(&activity_data.id, &context.settings().hostname)?;
// Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen
// if we receive the same activity twice in very quick succession.
- insert_activity(
- activity.id_unchecked(),
- activity.clone(),
- false,
- true,
- context.pool(),
- )
- .await?;
-
- info!("Receiving activity {}", activity.id_unchecked().to_string());
- activity.receive(context, request_counter).await?;
+ let object_value = serde_json::to_value(&activity)?;
+ insert_activity(&activity_data.id, object_value, false, true, context.pool()).await?;
+
+ info!("Receiving activity {}", activity_data.id.to_string());
+ activity
+ .receive(&Data::new(context.clone()), request_counter)
+ .await?;
Ok(HttpResponse::Ok().finish())
}
where
T: Serialize,
{
+ HttpResponse::Ok()
+ .content_type(APUB_JSON_CONTENT_TYPE)
+ .json(WithContext::new(data))
+}
+
+fn create_json_apub_response(data: serde_json::Value) -> HttpResponse<Body> {
HttpResponse::Ok()
.content_type(APUB_JSON_CONTENT_TYPE)
.json(data)
HttpResponse::Gone()
.content_type(APUB_JSON_CONTENT_TYPE)
.status(StatusCode::GONE)
- .json(data)
+ .json(WithContext::new(data))
}
#[derive(Deserialize)]
if !activity.local || sensitive {
Ok(HttpResponse::NotFound().finish())
} else {
- Ok(create_apub_response(&activity.data))
- }
-}
-
-pub(crate) async fn is_activity_already_known(
- pool: &DbPool,
- activity_id: &Url,
-) -> Result<bool, LemmyError> {
- let activity_id = activity_id.to_owned().into();
- let existing = blocking(pool, move |conn| {
- Activity::read_from_apub_id(conn, &activity_id)
- })
- .await?;
- match existing {
- Ok(_) => Ok(true),
- Err(_) => Ok(false),
+ Ok(create_json_apub_response(activity.data))
}
}
-fn assert_activity_not_local<T: Debug + ActivityFields>(
- activity: &T,
- hostname: &str,
-) -> Result<(), LemmyError> {
- let activity_domain = activity.id_unchecked().domain().context(location_info!())?;
+fn assert_activity_not_local(id: &Url, hostname: &str) -> Result<(), LemmyError> {
+ let activity_domain = id.domain().context(location_info!())?;
if activity_domain == hostname {
return Err(
anyhow!(
"Error: received activity which was sent by local instance: {:?}",
- activity
+ id
)
.into(),
);