]> Untitled Git - lemmy.git/blob - crates/apub/src/http/mod.rs
Move moderators collection to separate HTTP endpoint
[lemmy.git] / crates / apub / src / http / mod.rs
1 use crate::APUB_JSON_CONTENT_TYPE;
2 use actix_web::{body::Body, web, HttpResponse};
3 use http::StatusCode;
4 use lemmy_api_structs::blocking;
5 use lemmy_db_queries::source::activity::Activity_;
6 use lemmy_db_schema::source::activity::Activity;
7 use lemmy_utils::{settings::structs::Settings, LemmyError};
8 use lemmy_websocket::LemmyContext;
9 use serde::{Deserialize, Serialize};
10 use url::Url;
11
12 pub mod comment;
13 pub mod community;
14 pub mod post;
15 pub mod user;
16
17 /// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
18 /// headers.
19 fn create_apub_response<T>(data: &T) -> HttpResponse<Body>
20 where
21   T: Serialize,
22 {
23   HttpResponse::Ok()
24     .content_type(APUB_JSON_CONTENT_TYPE)
25     .json(data)
26 }
27
28 fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
29 where
30   T: Serialize,
31 {
32   HttpResponse::Gone()
33     .content_type(APUB_JSON_CONTENT_TYPE)
34     .status(StatusCode::GONE)
35     .json(data)
36 }
37
38 #[derive(Deserialize)]
39 pub struct CommunityQuery {
40   type_: String,
41   id: String,
42 }
43
44 /// Return the ActivityPub json representation of a local community over HTTP.
45 pub(crate) async fn get_activity(
46   info: web::Path<CommunityQuery>,
47   context: web::Data<LemmyContext>,
48 ) -> Result<HttpResponse<Body>, LemmyError> {
49   let settings = Settings::get();
50   let activity_id = Url::parse(&format!(
51     "{}/activities/{}/{}",
52     settings.get_protocol_and_hostname(),
53     info.type_,
54     info.id
55   ))?
56   .into();
57   let activity = blocking(context.pool(), move |conn| {
58     Activity::read_from_apub_id(&conn, &activity_id)
59   })
60   .await??;
61
62   let sensitive = activity.sensitive.unwrap_or(true);
63   if !activity.local || sensitive {
64     Ok(HttpResponse::NotFound().finish())
65   } else {
66     Ok(create_apub_response(&activity.data))
67   }
68 }