lemmy/crates/apub/src/http/person.rs

63 lines
2.1 KiB
Rust
Raw Normal View History

use crate::{
activity_lists::PersonInboxActivitiesWithAnnouncable,
fetcher::user_or_community::UserOrCommunity,
http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
objects::person::ApubPerson,
protocol::collections::empty_outbox::EmptyOutbox,
};
use activitypub_federation::{deser::context::WithContext, traits::ApubObject};
use actix_web::{web, 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)]
2021-03-10 22:33:55 +00:00
pub struct PersonQuery {
user_name: String,
}
2021-03-10 22:33:55 +00:00
/// Return the ActivityPub json representation of a local person over HTTP.
#[tracing::instrument(skip_all)]
pub(crate) async fn get_apub_person_http(
2021-03-10 22:33:55 +00:00
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
2021-12-14 13:30:37 +00:00
) -> Result<HttpResponse, LemmyError> {
let user_name = info.into_inner().user_name;
2021-03-10 22:33:55 +00:00
// TODO: this needs to be able to read deleted persons, so that it can send tombstones
2022-11-09 10:05:00 +00:00
let person: ApubPerson = Person::read_from_name(context.pool(), &user_name, true)
.await?
.into();
2021-03-10 22:33:55 +00:00
if !person.deleted {
let apub = person.into_apub(&context).await?;
Ok(create_apub_response(&apub))
} else {
Ok(create_apub_tombstone_response(person.actor_id.clone()))
}
}
#[tracing::instrument(skip_all)]
pub async fn person_inbox(
request: HttpRequest,
payload: String,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
receive_lemmy_activity::<WithContext<PersonInboxActivitiesWithAnnouncable>, UserOrCommunity>(
request, payload, context,
)
.await
}
#[tracing::instrument(skip_all)]
pub(crate) async fn get_apub_person_outbox(
2021-03-10 22:33:55 +00:00
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
2021-12-14 13:30:37 +00:00
) -> Result<HttpResponse, LemmyError> {
2022-11-09 10:05:00 +00:00
let person = Person::read_from_name(context.pool(), &info.user_name, false).await?;
let outbox_id = generate_outbox_url(&person.actor_id)?.into();
let outbox = EmptyOutbox::new(outbox_id)?;
Ok(create_apub_response(&outbox))
}