]> Untitled Git - lemmy.git/blob - crates/apub/src/http/routes.rs
cd6b11486ee5afa08a9864a0fdb2425f95b9d819
[lemmy.git] / crates / apub / src / http / routes.rs
1 use crate::{
2   http::{
3     comment::get_apub_comment,
4     community::{
5       community_inbox,
6       get_apub_community_followers,
7       get_apub_community_http,
8       get_apub_community_inbox,
9       get_apub_community_moderators,
10       get_apub_community_outbox,
11     },
12     get_activity,
13     person::{get_apub_person_http, get_apub_person_inbox, get_apub_person_outbox, person_inbox},
14     post::get_apub_post,
15     shared_inbox,
16   },
17   APUB_JSON_CONTENT_TYPE,
18 };
19 use actix_web::*;
20 use http_signature_normalization_actix::digest::middleware::VerifyDigest;
21 use lemmy_utils::settings::structs::Settings;
22 use sha2::{Digest, Sha256};
23
24 static APUB_JSON_CONTENT_TYPE_LONG: &str =
25   "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
26
27 pub fn config(cfg: &mut web::ServiceConfig) {
28   if Settings::get().federation.enabled {
29     println!("federation enabled, host is {}", Settings::get().hostname);
30     let digest_verifier = VerifyDigest::new(Sha256::new());
31
32     let header_guard_accept = guard::Any(guard::Header("Accept", APUB_JSON_CONTENT_TYPE))
33       .or(guard::Header("Accept", APUB_JSON_CONTENT_TYPE_LONG));
34     let header_guard_content_type =
35       guard::Any(guard::Header("Content-Type", APUB_JSON_CONTENT_TYPE))
36         .or(guard::Header("Content-Type", APUB_JSON_CONTENT_TYPE_LONG));
37
38     cfg
39       .service(
40         web::scope("/")
41           .guard(header_guard_accept)
42           .route(
43             "/c/{community_name}",
44             web::get().to(get_apub_community_http),
45           )
46           .route(
47             "/c/{community_name}/followers",
48             web::get().to(get_apub_community_followers),
49           )
50           .route(
51             "/c/{community_name}/outbox",
52             web::get().to(get_apub_community_outbox),
53           )
54           .route(
55             "/c/{community_name}/inbox",
56             web::get().to(get_apub_community_inbox),
57           )
58           .route(
59             "/c/{community_name}/moderators",
60             web::get().to(get_apub_community_moderators),
61           )
62           .route("/u/{user_name}", web::get().to(get_apub_person_http))
63           .route(
64             "/u/{user_name}/outbox",
65             web::get().to(get_apub_person_outbox),
66           )
67           .route("/u/{user_name}/inbox", web::get().to(get_apub_person_inbox))
68           .route("/post/{post_id}", web::get().to(get_apub_post))
69           .route("/comment/{comment_id}", web::get().to(get_apub_comment))
70           .route("/activities/{type_}/{id}", web::get().to(get_activity)),
71       )
72       // Inboxes dont work with the header guard for some reason.
73       .service(
74         web::scope("/")
75           .wrap(digest_verifier)
76           .guard(header_guard_content_type)
77           .route("/c/{community_name}/inbox", web::post().to(community_inbox))
78           .route("/u/{user_name}/inbox", web::post().to(person_inbox))
79           .route("/inbox", web::post().to(shared_inbox)),
80       );
81   }
82 }