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