use crate::{
- activities::{
- community::announce::{AnnouncableActivities, AnnounceActivity},
- following::accept::AcceptFollowCommunity,
- private_message::{
- create_or_update::CreateOrUpdatePrivateMessage,
- delete::DeletePrivateMessage,
- undo_delete::UndoDeletePrivateMessage,
- },
- },
- context::WithContext,
- generate_outbox_url,
- http::{
- create_apub_response,
- create_apub_tombstone_response,
- payload_to_string,
- receive_activity,
- },
+ activity_lists::PersonInboxActivitiesWithAnnouncable,
+ fetcher::user_or_community::UserOrCommunity,
+ http::{create_apub_response, create_apub_tombstone_response},
objects::person::ApubPerson,
+ protocol::collections::empty_outbox::EmptyOutbox,
};
-use activitystreams::{
- base::BaseExt,
- collection::{CollectionExt, OrderedCollection},
+use activitypub_federation::{
+ actix_web::inbox::receive_activity,
+ config::Data,
+ protocol::context::WithContext,
+ traits::Object,
};
-use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
-use lemmy_api_common::blocking;
-use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
-use lemmy_db_schema::source::person::Person;
-use lemmy_utils::LemmyError;
-use lemmy_websocket::LemmyContext;
-use log::info;
-use serde::{Deserialize, Serialize};
-use url::Url;
+use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
+use lemmy_api_common::{context::LemmyContext, utils::generate_outbox_url};
+use lemmy_db_schema::{source::person::Person, traits::ApubActor};
+use lemmy_utils::error::LemmyError;
+use serde::Deserialize;
#[derive(Deserialize)]
pub struct PersonQuery {
}
/// Return the ActivityPub json representation of a local person over HTTP.
+#[tracing::instrument(skip_all)]
pub(crate) async fn get_apub_person_http(
info: web::Path<PersonQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
let user_name = info.into_inner().user_name;
// TODO: this needs to be able to read deleted persons, so that it can send tombstones
- let person: ApubPerson = blocking(context.pool(), move |conn| {
- Person::find_by_name(conn, &user_name)
- })
- .await??
- .into();
+ let person: ApubPerson = Person::read_from_name(&mut context.pool(), &user_name, true)
+ .await?
+ .into();
if !person.deleted {
- let apub = person.to_apub(&context).await?;
+ let apub = person.into_json(&context).await?;
- Ok(create_apub_response(&apub))
+ create_apub_response(&apub)
} else {
- Ok(create_apub_tombstone_response(&person.to_tombstone()?))
+ create_apub_tombstone_response(person.actor_id.clone())
}
}
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
-#[serde(untagged)]
-#[activity_handler(LemmyContext)]
-pub enum PersonInboxActivities {
- AcceptFollowCommunity(AcceptFollowCommunity),
- /// Some activities can also be sent from user to user, eg a comment with mentions
- AnnouncableActivities(AnnouncableActivities),
- CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
- DeletePrivateMessage(DeletePrivateMessage),
- UndoDeletePrivateMessage(UndoDeletePrivateMessage),
- AnnounceActivity(Box<AnnounceActivity>),
-}
-
+#[tracing::instrument(skip_all)]
pub async fn person_inbox(
request: HttpRequest,
- payload: Payload,
- _path: web::Path<String>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse, LemmyError> {
- let unparsed = payload_to_string(payload).await?;
- info!("Received person inbox activity {}", unparsed);
- let activity = serde_json::from_str::<WithContext<PersonInboxActivities>>(&unparsed)?;
- receive_person_inbox(activity.inner(), request, &context).await
-}
-
-pub(in crate::http) async fn receive_person_inbox(
- activity: PersonInboxActivities,
- request: HttpRequest,
- context: &LemmyContext,
+ body: Bytes,
+ data: Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
- receive_activity(request, activity, context).await
+ receive_activity::<WithContext<PersonInboxActivitiesWithAnnouncable>, UserOrCommunity, LemmyContext>(
+ request, body, &data,
+ )
+ .await
}
+#[tracing::instrument(skip_all)]
pub(crate) async fn get_apub_person_outbox(
info: web::Path<PersonQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let person = blocking(context.pool(), move |conn| {
- Person::find_by_name(conn, &info.user_name)
- })
- .await??;
- // TODO: populate the person outbox
- let mut collection = OrderedCollection::new();
- collection
- .set_many_items(Vec::<Url>::new())
- .set_id(generate_outbox_url(&person.actor_id)?.into())
- .set_total_items(0_u64);
- let collection = WithContext::new(collection);
- Ok(create_apub_response(&collection))
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let person = Person::read_from_name(&mut context.pool(), &info.user_name, false).await?;
+ let outbox_id = generate_outbox_url(&person.actor_id)?.into();
+ let outbox = EmptyOutbox::new(outbox_id)?;
+ create_apub_response(&outbox)
}