]> Untitled Git - lemmy.git/blob - crates/routes/src/webfinger.rs
Merge branch 'remove_settings_and_secret_singletons_squashed'
[lemmy.git] / crates / routes / src / webfinger.rs
1 use actix_web::{error::ErrorBadRequest, web::Query, *};
2 use anyhow::anyhow;
3 use lemmy_api_common::blocking;
4 use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse};
5 use lemmy_db_queries::source::{community::Community_, person::Person_};
6 use lemmy_db_schema::source::{community::Community, person::Person};
7 use lemmy_utils::{settings::structs::Settings, LemmyError};
8 use lemmy_websocket::LemmyContext;
9 use serde::Deserialize;
10
11 #[derive(Deserialize)]
12 struct Params {
13   resource: String,
14 }
15
16 pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
17   if settings.federation.enabled {
18     cfg.route(
19       ".well-known/webfinger",
20       web::get().to(get_webfinger_response),
21     );
22   }
23 }
24
25 /// Responds to webfinger requests of the following format. There isn't any real documentation for
26 /// this, but it described in this blog post:
27 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
28 ///
29 /// You can also view the webfinger response that Mastodon sends:
30 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
31 async fn get_webfinger_response(
32   info: Query<Params>,
33   context: web::Data<LemmyContext>,
34 ) -> Result<HttpResponse, Error> {
35   let community_regex_parsed = context
36     .settings()
37     .webfinger_community_regex()
38     .captures(&info.resource)
39     .map(|c| c.get(1))
40     .flatten();
41
42   let username_regex_parsed = context
43     .settings()
44     .webfinger_username_regex()
45     .captures(&info.resource)
46     .map(|c| c.get(1))
47     .flatten();
48
49   let url = if let Some(community_name) = community_regex_parsed {
50     let community_name = community_name.as_str().to_owned();
51     // Make sure the requested community exists.
52     blocking(context.pool(), move |conn| {
53       Community::read_from_name(conn, &community_name)
54     })
55     .await?
56     .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
57     .actor_id
58   } else if let Some(person_name) = username_regex_parsed {
59     let person_name = person_name.as_str().to_owned();
60     // Make sure the requested person exists.
61     blocking(context.pool(), move |conn| {
62       Person::find_by_name(conn, &person_name)
63     })
64     .await?
65     .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
66     .actor_id
67   } else {
68     return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
69   };
70
71   let json = WebfingerResponse {
72     subject: info.resource.to_owned(),
73     aliases: vec![url.to_owned().into()],
74     links: vec![
75       WebfingerLink {
76         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
77         type_: Some("text/html".to_string()),
78         href: Some(url.to_owned().into()),
79         template: None,
80       },
81       WebfingerLink {
82         rel: Some("self".to_string()),
83         type_: Some("application/activity+json".to_string()),
84         href: Some(url.into()),
85         template: None,
86       }, // TODO: this also needs to return the subscribe link once that's implemented
87          //{
88          //  "rel": "http://ostatus.org/schema/1.0/subscribe",
89          //  "template": "https://my_instance.com/authorize_interaction?uri={uri}"
90          //}
91     ],
92   };
93
94   Ok(HttpResponse::Ok().json(json))
95 }