]> Untitled Git - lemmy.git/blob - crates/apub_receive/src/http/person.rs
Apub inbox rewrite (#1652)
[lemmy.git] / crates / apub_receive / src / http / person.rs
1 use crate::http::{
2   create_apub_response,
3   create_apub_tombstone_response,
4   inbox_enums::PersonInboxActivities,
5   payload_to_string,
6   receive_activity,
7 };
8 use activitystreams::{
9   base::BaseExt,
10   collection::{CollectionExt, OrderedCollection},
11 };
12 use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
13 use lemmy_api_common::blocking;
14 use lemmy_apub::{extensions::context::lemmy_context, objects::ToApub, ActorType};
15 use lemmy_db_queries::source::person::Person_;
16 use lemmy_db_schema::source::person::Person;
17 use lemmy_utils::LemmyError;
18 use lemmy_websocket::LemmyContext;
19 use serde::Deserialize;
20 use url::Url;
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 = blocking(context.pool(), move |conn| {
35     Person::find_by_name(conn, &user_name)
36   })
37   .await??;
38
39   if !person.deleted {
40     let apub = person.to_apub(context.pool()).await?;
41
42     Ok(create_apub_response(&apub))
43   } else {
44     Ok(create_apub_tombstone_response(&person.to_tombstone()?))
45   }
46 }
47
48 pub async fn person_inbox(
49   request: HttpRequest,
50   payload: Payload,
51   _path: web::Path<String>,
52   context: web::Data<LemmyContext>,
53 ) -> Result<HttpResponse, LemmyError> {
54   let unparsed = payload_to_string(payload).await?;
55   receive_activity::<PersonInboxActivities>(request, &unparsed, context).await
56 }
57
58 pub(crate) async fn get_apub_person_outbox(
59   info: web::Path<PersonQuery>,
60   context: web::Data<LemmyContext>,
61 ) -> Result<HttpResponse<Body>, LemmyError> {
62   let person = blocking(context.pool(), move |conn| {
63     Person::find_by_name(conn, &info.user_name)
64   })
65   .await??;
66   // TODO: populate the person outbox
67   let mut collection = OrderedCollection::new();
68   collection
69     .set_many_items(Vec::<Url>::new())
70     .set_many_contexts(lemmy_context()?)
71     .set_id(person.get_outbox_url()?)
72     .set_total_items(0_u64);
73   Ok(create_apub_response(&collection))
74 }
75
76 pub(crate) async fn get_apub_person_inbox(
77   info: web::Path<PersonQuery>,
78   context: web::Data<LemmyContext>,
79 ) -> Result<HttpResponse<Body>, LemmyError> {
80   let person = blocking(context.pool(), move |conn| {
81     Person::find_by_name(conn, &info.user_name)
82   })
83   .await??;
84
85   let mut collection = OrderedCollection::new();
86   collection
87     .set_id(person.inbox_url.into())
88     .set_many_contexts(lemmy_context()?);
89   Ok(create_apub_response(&collection))
90 }