]> Untitled Git - lemmy.git/blob - crates/apub/src/http/person.rs
Remove ActivityFields trait, deserialize into another struct instead
[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::{body::Body, 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;
18 use lemmy_utils::LemmyError;
19 use lemmy_websocket::LemmyContext;
20 use log::info;
21 use serde::Deserialize;
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 pub(crate) async fn get_apub_person_http(
30   info: web::Path<PersonQuery>,
31   context: web::Data<LemmyContext>,
32 ) -> Result<HttpResponse<Body>, LemmyError> {
33   let user_name = info.into_inner().user_name;
34   // TODO: this needs to be able to read deleted persons, so that it can send tombstones
35   let person: ApubPerson = blocking(context.pool(), move |conn| {
36     Person::find_by_name(conn, &user_name)
37   })
38   .await??
39   .into();
40
41   if !person.deleted {
42     let apub = person.to_apub(&context).await?;
43
44     Ok(create_apub_response(&apub))
45   } else {
46     Ok(create_apub_tombstone_response(&person.to_tombstone()?))
47   }
48 }
49
50 pub async fn person_inbox(
51   request: HttpRequest,
52   payload: Payload,
53   _path: web::Path<String>,
54   context: web::Data<LemmyContext>,
55 ) -> Result<HttpResponse, LemmyError> {
56   let unparsed = payload_to_string(payload).await?;
57   info!("Received person inbox activity {}", unparsed);
58   let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
59   let activity = serde_json::from_str::<WithContext<PersonInboxActivities>>(&unparsed)?;
60   receive_person_inbox(activity.inner(), activity_data, request, &context).await
61 }
62
63 pub(in crate::http) async fn receive_person_inbox(
64   activity: PersonInboxActivities,
65   activity_data: ActivityCommonFields,
66   request: HttpRequest,
67   context: &LemmyContext,
68 ) -> Result<HttpResponse, LemmyError> {
69   receive_activity(request, activity, activity_data, context).await
70 }
71
72 pub(crate) async fn get_apub_person_outbox(
73   info: web::Path<PersonQuery>,
74   context: web::Data<LemmyContext>,
75 ) -> Result<HttpResponse<Body>, LemmyError> {
76   let person = blocking(context.pool(), move |conn| {
77     Person::find_by_name(conn, &info.user_name)
78   })
79   .await??;
80   let outbox = PersonOutbox::new(person).await?;
81   Ok(create_apub_response(&outbox))
82 }