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