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