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