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