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