]> Untitled Git - lemmy.git/blob - crates/apub/src/http/routes.rs
Adding clippy:unwrap to husky. Fixes #1892 (#1893)
[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::{dev::RequestHead, guard::Guard, http::Method, *};
16 use http_signature_normalization_actix::digest::middleware::VerifyDigest;
17 use lemmy_utils::settings::structs::Settings;
18 use sha2::{Digest, Sha256};
19
20 pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
21   if settings.federation.enabled {
22     println!("federation enabled, host is {}", settings.hostname);
23
24     cfg
25       .route(
26         "/c/{community_name}",
27         web::get().to(get_apub_community_http),
28       )
29       .route(
30         "/c/{community_name}/followers",
31         web::get().to(get_apub_community_followers),
32       )
33       .route(
34         "/c/{community_name}/outbox",
35         web::get().to(get_apub_community_outbox),
36       )
37       .route(
38         "/c/{community_name}/moderators",
39         web::get().to(get_apub_community_moderators),
40       )
41       .route("/u/{user_name}", web::get().to(get_apub_person_http))
42       .route(
43         "/u/{user_name}/outbox",
44         web::get().to(get_apub_person_outbox),
45       )
46       .route("/post/{post_id}", web::get().to(get_apub_post))
47       .route("/comment/{comment_id}", web::get().to(get_apub_comment))
48       .route("/activities/{type_}/{id}", web::get().to(get_activity));
49
50     cfg.service(
51       web::scope("")
52         .wrap(VerifyDigest::new(Sha256::new()))
53         .guard(InboxRequestGuard)
54         .route("/c/{community_name}/inbox", web::post().to(community_inbox))
55         .route("/u/{user_name}/inbox", web::post().to(person_inbox))
56         .route("/inbox", web::post().to(shared_inbox)),
57     );
58   }
59 }
60
61 /// Without this, things like webfinger or RSS feeds stop working, as all requests seem to get
62 /// routed into the inbox service (because it covers the root path). So we filter out anything that
63 /// definitely can't be an inbox request (based on Accept header and request method).
64 struct InboxRequestGuard;
65
66 impl Guard for InboxRequestGuard {
67   fn check(&self, request: &RequestHead) -> bool {
68     if request.method != Method::POST {
69       return false;
70     }
71     if let Some(val) = request.headers.get("Content-Type") {
72       return val
73         .to_str()
74         .expect("Content-Type header contains non-ascii chars.")
75         .starts_with("application/");
76     }
77     false
78   }
79 }