]> Untitled Git - lemmy.git/blob - crates/apub/src/http/person.rs
Merge pull request #2593 from LemmyNet/refactor-notifications
[lemmy.git] / crates / apub / src / http / person.rs
1 use crate::{
2   activity_lists::PersonInboxActivitiesWithAnnouncable,
3   fetcher::user_or_community::UserOrCommunity,
4   http::{create_apub_response, create_apub_tombstone_response, receive_lemmy_activity},
5   objects::person::ApubPerson,
6   protocol::collections::empty_outbox::EmptyOutbox,
7 };
8 use activitypub_federation::{deser::context::WithContext, traits::ApubObject};
9 use actix_web::{web, HttpRequest, HttpResponse};
10 use lemmy_api_common::{context::LemmyContext, utils::generate_outbox_url};
11 use lemmy_db_schema::{source::person::Person, traits::ApubActor};
12 use lemmy_utils::error::LemmyError;
13 use serde::Deserialize;
14
15 #[derive(Deserialize)]
16 pub struct PersonQuery {
17   user_name: String,
18 }
19
20 /// Return the ActivityPub json representation of a local person over HTTP.
21 #[tracing::instrument(skip_all)]
22 pub(crate) async fn get_apub_person_http(
23   info: web::Path<PersonQuery>,
24   context: web::Data<LemmyContext>,
25 ) -> Result<HttpResponse, 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: ApubPerson = Person::read_from_name(context.pool(), &user_name, true)
29     .await?
30     .into();
31
32   if !person.deleted {
33     let apub = person.into_apub(&context).await?;
34
35     Ok(create_apub_response(&apub))
36   } else {
37     Ok(create_apub_tombstone_response(person.actor_id.clone()))
38   }
39 }
40
41 #[tracing::instrument(skip_all)]
42 pub async fn person_inbox(
43   request: HttpRequest,
44   payload: String,
45   context: web::Data<LemmyContext>,
46 ) -> Result<HttpResponse, LemmyError> {
47   receive_lemmy_activity::<WithContext<PersonInboxActivitiesWithAnnouncable>, UserOrCommunity>(
48     request, payload, context,
49   )
50   .await
51 }
52
53 #[tracing::instrument(skip_all)]
54 pub(crate) async fn get_apub_person_outbox(
55   info: web::Path<PersonQuery>,
56   context: web::Data<LemmyContext>,
57 ) -> Result<HttpResponse, LemmyError> {
58   let person = Person::read_from_name(context.pool(), &info.user_name, false).await?;
59   let outbox_id = generate_outbox_url(&person.actor_id)?.into();
60   let outbox = EmptyOutbox::new(outbox_id)?;
61   Ok(create_apub_response(&outbox))
62 }