You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lemmy/crates/apub/src/http/person.rs

103 lines
3.2 KiB
Rust

use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
use log::info;
use serde::{Deserialize, Serialize};
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 crate::{
activities::{
community::announce::{AnnouncableActivities, AnnounceActivity},
following::accept::AcceptFollowCommunity,
private_message::{
create_or_update::CreateOrUpdatePrivateMessage,
delete::DeletePrivateMessage,
undo_delete::UndoDeletePrivateMessage,
},
},
context::WithContext,
http::{
create_apub_response,
create_apub_tombstone_response,
payload_to_string,
receive_activity,
},
objects::person::ApubPerson,
protocol::collections::person_outbox::UserOutbox,
};
#[derive(Deserialize)]
3 years ago
pub struct PersonQuery {
user_name: String,
}
3 years ago
/// Return the ActivityPub json representation of a local person over HTTP.
pub(crate) async fn get_apub_person_http(
3 years ago
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let user_name = info.into_inner().user_name;
3 years ago
// TODO: this needs to be able to read deleted persons, so that it can send tombstones
let person: ApubPerson = blocking(context.pool(), move |conn| {
3 years ago
Person::find_by_name(conn, &user_name)
})
.await??
.into();
3 years ago
if !person.deleted {
let apub = person.to_apub(&context).await?;
Ok(create_apub_response(&apub))
} else {
3 years ago
Ok(create_apub_tombstone_response(&person.to_tombstone()?))
}
}
#[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>),
}
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,
) -> Result<HttpResponse, LemmyError> {
receive_activity(request, activity, context).await
}
pub(crate) async fn get_apub_person_outbox(
3 years ago
info: web::Path<PersonQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
3 years ago
let person = blocking(context.pool(), move |conn| {
Person::find_by_name(conn, &info.user_name)
})
.await??;
let outbox = UserOutbox::new(person).await?;
Ok(create_apub_response(&outbox))
}