]> Untitled Git - lemmy.git/blob - src/routes/federation.rs
Adding v0.9.7 release notes.
[lemmy.git] / src / routes / federation.rs
1 use actix_web::*;
2 use http_signature_normalization_actix::digest::middleware::VerifyDigest;
3 use lemmy_apub::{
4   http::{
5     comment::get_apub_comment,
6     community::{
7       get_apub_community_followers,
8       get_apub_community_http,
9       get_apub_community_inbox,
10       get_apub_community_outbox,
11     },
12     get_activity,
13     post::get_apub_post,
14     user::{get_apub_user_http, get_apub_user_inbox, get_apub_user_outbox},
15   },
16   inbox::{community_inbox::community_inbox, shared_inbox::shared_inbox, user_inbox::user_inbox},
17   APUB_JSON_CONTENT_TYPE,
18 };
19 use lemmy_utils::settings::Settings;
20 use sha2::{Digest, Sha256};
21
22 static APUB_JSON_CONTENT_TYPE_LONG: &str =
23   "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
24
25 pub fn config(cfg: &mut web::ServiceConfig) {
26   if Settings::get().federation.enabled {
27     println!("federation enabled, host is {}", Settings::get().hostname);
28     let digest_verifier = VerifyDigest::new(Sha256::new());
29
30     let header_guard_accept = guard::Any(guard::Header("Accept", APUB_JSON_CONTENT_TYPE))
31       .or(guard::Header("Accept", APUB_JSON_CONTENT_TYPE_LONG));
32     let header_guard_content_type =
33       guard::Any(guard::Header("Content-Type", APUB_JSON_CONTENT_TYPE))
34         .or(guard::Header("Content-Type", APUB_JSON_CONTENT_TYPE_LONG))
35         // TODO: compatibility with previous lemmy versions, remove this later
36         .or(guard::Header("Content-Type", "application/json"));
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("/u/{user_name}", web::get().to(get_apub_user_http))
59           .route("/u/{user_name}/outbox", web::get().to(get_apub_user_outbox))
60           .route("/u/{user_name}/inbox", web::get().to(get_apub_user_inbox))
61           .route("/post/{post_id}", web::get().to(get_apub_post))
62           .route("/comment/{comment_id}", web::get().to(get_apub_comment))
63           .route("/activities/{type_}/{id}", web::get().to(get_activity)),
64       )
65       // Inboxes dont work with the header guard for some reason.
66       .service(
67         web::scope("/")
68           .wrap(digest_verifier)
69           .guard(header_guard_content_type)
70           .route("/c/{community_name}/inbox", web::post().to(community_inbox))
71           .route("/u/{user_name}/inbox", web::post().to(user_inbox))
72           .route("/inbox", web::post().to(shared_inbox)),
73       );
74   }
75 }