]> Untitled Git - lemmy.git/blob - crates/apub/src/http/routes.rs
Move code to apub library (#1795)
[lemmy.git] / crates / apub / src / http / routes.rs
1 use crate::http::{
2   comment::get_apub_comment,
3   community::{
4     community_inbox,
5     get_apub_community_followers,
6     get_apub_community_http,
7     get_apub_community_inbox,
8     get_apub_community_moderators,
9     get_apub_community_outbox,
10   },
11   get_activity,
12   person::{get_apub_person_http, get_apub_person_inbox, get_apub_person_outbox, person_inbox},
13   post::get_apub_post,
14   shared_inbox,
15 };
16 use actix_web::*;
17 use http_signature_normalization_actix::digest::middleware::VerifyDigest;
18 use lemmy_apub_lib::APUB_JSON_CONTENT_TYPE;
19 use lemmy_utils::settings::structs::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, settings: &Settings) {
26   if settings.federation.enabled {
27     println!("federation enabled, host is {}", settings.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
36     cfg
37       .service(
38         web::scope("")
39           .guard(header_guard_accept)
40           .route(
41             "/c/{community_name}",
42             web::get().to(get_apub_community_http),
43           )
44           .route(
45             "/c/{community_name}/followers",
46             web::get().to(get_apub_community_followers),
47           )
48           .route(
49             "/c/{community_name}/outbox",
50             web::get().to(get_apub_community_outbox),
51           )
52           .route(
53             "/c/{community_name}/inbox",
54             web::get().to(get_apub_community_inbox),
55           )
56           .route(
57             "/c/{community_name}/moderators",
58             web::get().to(get_apub_community_moderators),
59           )
60           .route("/u/{user_name}", web::get().to(get_apub_person_http))
61           .route(
62             "/u/{user_name}/outbox",
63             web::get().to(get_apub_person_outbox),
64           )
65           .route("/u/{user_name}/inbox", web::get().to(get_apub_person_inbox))
66           .route("/post/{post_id}", web::get().to(get_apub_post))
67           .route("/comment/{comment_id}", web::get().to(get_apub_comment))
68           .route("/activities/{type_}/{id}", web::get().to(get_activity)),
69       )
70       // Inboxes dont work with the header guard for some reason.
71       .service(
72         web::scope("")
73           .wrap(digest_verifier)
74           .guard(header_guard_content_type)
75           .route("/c/{community_name}/inbox", web::post().to(community_inbox))
76           .route("/u/{user_name}/inbox", web::post().to(person_inbox))
77           .route("/inbox", web::post().to(shared_inbox)),
78       );
79   }
80 }