]> Untitled Git - lemmy.git/blob - server/src/routes/webfinger.rs
Remove federation option from master. (#745)
[lemmy.git] / server / src / routes / webfinger.rs
1 use super::*;
2 use crate::db::community::Community;
3
4 #[derive(Deserialize)]
5 pub struct Params {
6   resource: String,
7 }
8
9 pub fn config(cfg: &mut web::ServiceConfig) {
10   cfg.route(
11     ".well-known/webfinger",
12     web::get().to(get_webfinger_response),
13   );
14 }
15
16 lazy_static! {
17   static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
18     "^group:([a-z0-9_]{{3, 20}})@{}$",
19     Settings::get().hostname
20   ))
21   .unwrap();
22 }
23
24 /// Responds to webfinger requests of the following format. There isn't any real documentation for
25 /// this, but it described in this blog post:
26 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
27 ///
28 /// You can also view the webfinger response that Mastodon sends:
29 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
30 async fn get_webfinger_response(
31   info: Query<Params>,
32   db: web::Data<Pool<ConnectionManager<PgConnection>>>,
33 ) -> Result<HttpResponse, Error> {
34   let res = web::block(move || {
35     let conn = db.get()?;
36
37     let regex_parsed = WEBFINGER_COMMUNITY_REGEX
38       .captures(&info.resource)
39       .map(|c| c.get(1));
40     // TODO: replace this with .flatten() once we are running rust 1.40
41     let regex_parsed_flattened = match regex_parsed {
42       Some(s) => s,
43       None => None,
44     };
45     let community_name = match regex_parsed_flattened {
46       Some(c) => c.as_str(),
47       None => return Err(format_err!("not_found")),
48     };
49
50     // Make sure the requested community exists.
51     let community = match Community::read_from_name(&conn, community_name.to_string()) {
52       Ok(o) => o,
53       Err(_) => return Err(format_err!("not_found")),
54     };
55
56     let community_url = community.get_url();
57
58     Ok(json!({
59     "subject": info.resource,
60     "aliases": [
61       community_url,
62     ],
63     "links": [
64     {
65       "rel": "http://webfinger.net/rel/profile-page",
66       "type": "text/html",
67       "href": community_url
68     },
69     {
70       "rel": "self",
71       "type": "application/activity+json",
72       // Yes this is correct, this link doesn't include the `.json` extension
73       "href": community_url
74     }
75     // TODO: this also needs to return the subscribe link once that's implemented
76     //{
77     //  "rel": "http://ostatus.org/schema/1.0/subscribe",
78     //  "template": "https://my_instance.com/authorize_interaction?uri={uri}"
79     //}
80     ]
81     }))
82   })
83   .await
84   .map(|json| HttpResponse::Ok().json(json))
85   .map_err(ErrorBadRequest)?;
86   Ok(res)
87 }