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

83 lines
2.5 KiB
Rust
Raw Normal View History

use crate::{
activity_lists::PersonInboxActivities,
context::WithContext,
http::{
create_apub_response,
create_apub_tombstone_response,
payload_to_string,
receive_activity,
ActivityCommonFields,
},
objects::person::ApubPerson,
protocol::collections::person_outbox::PersonOutbox,
};
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use lemmy_api_common::blocking;
use lemmy_apub_lib::traits::ApubObject;
use lemmy_db_schema::source::person::Person;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
2021-11-23 12:16:47 +00:00
use tracing::info;
#[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.
pub(crate) async fn get_apub_person_http(
2021-03-10 22:33:55 +00:00
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, 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
let person: ApubPerson = blocking(context.pool(), move |conn| {
2021-03-10 22:33:55 +00:00
Person::find_by_name(conn, &user_name)
})
.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 {
2021-03-10 22:33:55 +00:00
Ok(create_apub_tombstone_response(&person.to_tombstone()?))
}
}
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_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
let activity = serde_json::from_str::<WithContext<PersonInboxActivities>>(&unparsed)?;
receive_person_inbox(activity.inner(), activity_data, request, &context).await
}
pub(in crate::http) async fn receive_person_inbox(
activity: PersonInboxActivities,
activity_data: ActivityCommonFields,
request: HttpRequest,
context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
receive_activity(request, activity, activity_data, context).await
}
pub(crate) async fn get_apub_person_outbox(
2021-03-10 22:33:55 +00:00
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
2021-03-10 22:33:55 +00:00
let person = blocking(context.pool(), move |conn| {
2021-07-05 16:07:26 +00:00
Person::find_by_name(conn, &info.user_name)
})
.await??;
let outbox = PersonOutbox::new(person).await?;
Ok(create_apub_response(&outbox))
}