]> Untitled Git - lemmy.git/blob - crates/apub/src/http/person.rs
~80% done
[lemmy.git] / crates / apub / src / http / person.rs
1 use crate::{
2   extensions::context::lemmy_context,
3   http::{create_apub_response, create_apub_tombstone_response},
4   objects::ToApub,
5   ActorType,
6 };
7 use activitystreams::{
8   base::BaseExt,
9   collection::{CollectionExt, OrderedCollection},
10 };
11 use actix_web::{body::Body, web, HttpResponse};
12 use lemmy_api_structs::blocking;
13 use lemmy_db_queries::source::person::Person_;
14 use lemmy_db_schema::source::person::Person;
15 use lemmy_utils::LemmyError;
16 use lemmy_websocket::LemmyContext;
17 use serde::Deserialize;
18 use url::Url;
19
20 #[derive(Deserialize)]
21 pub struct PersonQuery {
22   user_name: String,
23 }
24
25 /// Return the ActivityPub json representation of a local person over HTTP.
26 pub async fn get_apub_person_http(
27   info: web::Path<PersonQuery>,
28   context: web::Data<LemmyContext>,
29 ) -> Result<HttpResponse<Body>, LemmyError> {
30   let user_name = info.into_inner().user_name;
31   // TODO: this needs to be able to read deleted persons, so that it can send tombstones
32   let person = blocking(context.pool(), move |conn| {
33     Person::find_by_name(conn, &user_name)
34   })
35   .await??;
36
37   if !person.deleted {
38     let apub = person.to_apub(context.pool()).await?;
39
40     Ok(create_apub_response(&apub))
41   } else {
42     Ok(create_apub_tombstone_response(&person.to_tombstone()?))
43   }
44 }
45
46 pub async fn get_apub_person_outbox(
47   info: web::Path<PersonQuery>,
48   context: web::Data<LemmyContext>,
49 ) -> Result<HttpResponse<Body>, LemmyError> {
50   let person = blocking(context.pool(), move |conn| {
51     Person::find_by_name(&conn, &info.user_name)
52   })
53   .await??;
54   // TODO: populate the person outbox
55   let mut collection = OrderedCollection::new();
56   collection
57     .set_many_items(Vec::<Url>::new())
58     .set_many_contexts(lemmy_context()?)
59     .set_id(person.get_outbox_url()?)
60     .set_total_items(0_u64);
61   Ok(create_apub_response(&collection))
62 }
63
64 pub async fn get_apub_person_inbox(
65   info: web::Path<PersonQuery>,
66   context: web::Data<LemmyContext>,
67 ) -> Result<HttpResponse<Body>, LemmyError> {
68   let person = blocking(context.pool(), move |conn| {
69     Person::find_by_name(&conn, &info.user_name)
70   })
71   .await??;
72
73   let mut collection = OrderedCollection::new();
74   collection
75     .set_id(format!("{}/inbox", person.actor_id.into_inner()).parse()?)
76     .set_many_contexts(lemmy_context()?);
77   Ok(create_apub_response(&collection))
78 }