]> Untitled Git - lemmy.git/blob - crates/apub/src/http/person.rs
bc0633d8d58e071832e4dcaa3f68eefb8d17ef64
[lemmy.git] / crates / apub / src / http / person.rs
1 use crate::{
2   activity_lists::PersonInboxActivities,
3   context::WithContext,
4   http::{
5     create_apub_response,
6     create_apub_tombstone_response,
7     payload_to_string,
8     receive_activity,
9     ActivityCommonFields,
10   },
11   objects::person::ApubPerson,
12   protocol::collections::person_outbox::PersonOutbox,
13 };
14 use actix_web::{web, web::Payload, HttpRequest, HttpResponse};
15 use lemmy_api_common::blocking;
16 use lemmy_apub_lib::traits::ApubObject;
17 use lemmy_db_schema::{source::person::Person, traits::ApubActor};
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use serde::Deserialize;
21 use tracing::info;
22
23 #[derive(Deserialize)]
24 pub struct PersonQuery {
25   user_name: String,
26 }
27
28 /// Return the ActivityPub json representation of a local person over HTTP.
29 #[tracing::instrument(skip_all)]
30 pub(crate) async fn get_apub_person_http(
31   info: web::Path<PersonQuery>,
32   context: web::Data<LemmyContext>,
33 ) -> Result<HttpResponse, LemmyError> {
34   let user_name = info.into_inner().user_name;
35   // TODO: this needs to be able to read deleted persons, so that it can send tombstones
36   let person: ApubPerson = blocking(context.pool(), move |conn| {
37     Person::read_from_name(conn, &user_name)
38   })
39   .await??
40   .into();
41
42   if !person.deleted {
43     let apub = person.into_apub(&context).await?;
44
45     Ok(create_apub_response(&apub))
46   } else {
47     Ok(create_apub_tombstone_response(&person.to_tombstone()?))
48   }
49 }
50
51 #[tracing::instrument(skip_all)]
52 pub async fn person_inbox(
53   request: HttpRequest,
54   payload: Payload,
55   _path: web::Path<String>,
56   context: web::Data<LemmyContext>,
57 ) -> Result<HttpResponse, LemmyError> {
58   let unparsed = payload_to_string(payload).await?;
59   info!("Received person inbox activity {}", unparsed);
60   let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
61   let activity = serde_json::from_str::<WithContext<PersonInboxActivities>>(&unparsed)?;
62   receive_person_inbox(activity.inner(), activity_data, request, &context).await
63 }
64
65 pub(in crate::http) async fn receive_person_inbox(
66   activity: PersonInboxActivities,
67   activity_data: ActivityCommonFields,
68   request: HttpRequest,
69   context: &LemmyContext,
70 ) -> Result<HttpResponse, LemmyError> {
71   receive_activity(request, activity, activity_data, context).await
72 }
73
74 #[tracing::instrument(skip_all)]
75 pub(crate) async fn get_apub_person_outbox(
76   info: web::Path<PersonQuery>,
77   context: web::Data<LemmyContext>,
78 ) -> Result<HttpResponse, LemmyError> {
79   let person = blocking(context.pool(), move |conn| {
80     Person::read_from_name(conn, &info.user_name)
81   })
82   .await??;
83   let outbox = PersonOutbox::new(person).await?;
84   Ok(create_apub_response(&outbox))
85 }