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