3 community::announce::{AnnouncableActivities, AnnounceActivity},
4 following::accept::AcceptFollowCommunity,
6 create_or_update::CreateOrUpdatePrivateMessage,
7 delete::DeletePrivateMessage,
8 undo_delete::UndoDeletePrivateMessage,
11 collections::user_outbox::UserOutbox,
15 create_apub_tombstone_response,
19 objects::person::ApubPerson,
21 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
22 use lemmy_api_common::blocking;
23 use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
24 use lemmy_db_schema::source::person::Person;
25 use lemmy_utils::LemmyError;
26 use lemmy_websocket::LemmyContext;
28 use serde::{Deserialize, Serialize};
30 #[derive(Deserialize)]
31 pub struct PersonQuery {
35 /// Return the ActivityPub json representation of a local person over HTTP.
36 pub(crate) async fn get_apub_person_http(
37 info: web::Path<PersonQuery>,
38 context: web::Data<LemmyContext>,
39 ) -> Result<HttpResponse<Body>, LemmyError> {
40 let user_name = info.into_inner().user_name;
41 // TODO: this needs to be able to read deleted persons, so that it can send tombstones
42 let person: ApubPerson = blocking(context.pool(), move |conn| {
43 Person::find_by_name(conn, &user_name)
49 let apub = person.to_apub(&context).await?;
51 Ok(create_apub_response(&apub))
53 Ok(create_apub_tombstone_response(&person.to_tombstone()?))
57 #[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
59 #[activity_handler(LemmyContext)]
60 pub enum PersonInboxActivities {
61 AcceptFollowCommunity(AcceptFollowCommunity),
62 /// Some activities can also be sent from user to user, eg a comment with mentions
63 AnnouncableActivities(AnnouncableActivities),
64 CreateOrUpdatePrivateMessage(CreateOrUpdatePrivateMessage),
65 DeletePrivateMessage(DeletePrivateMessage),
66 UndoDeletePrivateMessage(UndoDeletePrivateMessage),
67 AnnounceActivity(Box<AnnounceActivity>),
70 pub async fn person_inbox(
73 _path: web::Path<String>,
74 context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse, LemmyError> {
76 let unparsed = payload_to_string(payload).await?;
77 info!("Received person inbox activity {}", unparsed);
78 let activity = serde_json::from_str::<WithContext<PersonInboxActivities>>(&unparsed)?;
79 receive_person_inbox(activity.inner(), request, &context).await
82 pub(in crate::http) async fn receive_person_inbox(
83 activity: PersonInboxActivities,
85 context: &LemmyContext,
86 ) -> Result<HttpResponse, LemmyError> {
87 receive_activity(request, activity, context).await
90 pub(crate) async fn get_apub_person_outbox(
91 info: web::Path<PersonQuery>,
92 context: web::Data<LemmyContext>,
93 ) -> Result<HttpResponse<Body>, LemmyError> {
94 let person = blocking(context.pool(), move |conn| {
95 Person::find_by_name(conn, &info.user_name)
98 let outbox = UserOutbox::new(person).await?;
99 Ok(create_apub_response(&outbox))