]> Untitled Git - lemmy.git/blob - crates/routes/src/webfinger.rs
4c015675d11ab2d4382004063006ef10ab865f37
[lemmy.git] / crates / routes / src / webfinger.rs
1 use actix_web::{web, web::Query, HttpResponse};
2 use anyhow::Context;
3 use lemmy_api_common::utils::blocking;
4 use lemmy_apub::fetcher::webfinger::{WebfingerLink, WebfingerResponse};
5 use lemmy_db_schema::{
6   source::{community::Community, person::Person},
7   traits::ApubActor,
8 };
9 use lemmy_utils::{error::LemmyError, location_info, settings::structs::Settings};
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
12 use url::Url;
13
14 #[derive(Deserialize)]
15 struct Params {
16   resource: String,
17 }
18
19 pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
20   if settings.federation.enabled {
21     cfg.route(
22       ".well-known/webfinger",
23       web::get().to(get_webfinger_response),
24     );
25   }
26 }
27
28 /// Responds to webfinger requests of the following format. There isn't any real documentation for
29 /// this, but it described in this blog post:
30 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
31 ///
32 /// You can also view the webfinger response that Mastodon sends:
33 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
34 async fn get_webfinger_response(
35   info: Query<Params>,
36   context: web::Data<LemmyContext>,
37 ) -> Result<HttpResponse, LemmyError> {
38   let name = context
39     .settings()
40     .webfinger_regex()
41     .captures(&info.resource)
42     .and_then(|c| c.get(1))
43     .context(location_info!())?
44     .as_str()
45     .to_string();
46
47   let name_ = name.clone();
48   let user_id: Option<Url> = blocking(context.pool(), move |conn| {
49     Person::read_from_name(conn, &name_, false)
50   })
51   .await?
52   .ok()
53   .map(|c| c.actor_id.into());
54   let community_id: Option<Url> = blocking(context.pool(), move |conn| {
55     Community::read_from_name(conn, &name, false)
56   })
57   .await?
58   .ok()
59   .map(|c| c.actor_id.into());
60
61   // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
62   // community last so that it gets prioritized. For Lemmy the order doesnt matter.
63   let links = vec![
64     webfinger_link_for_actor(user_id),
65     webfinger_link_for_actor(community_id),
66   ]
67   .into_iter()
68   .flatten()
69   .collect();
70
71   let json = WebfingerResponse {
72     subject: info.resource.to_owned(),
73     links,
74   };
75
76   Ok(HttpResponse::Ok().json(json))
77 }
78
79 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
80   if let Some(url) = url {
81     vec![
82       WebfingerLink {
83         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
84         kind: Some("text/html".to_string()),
85         href: Some(url.to_owned()),
86       },
87       WebfingerLink {
88         rel: Some("self".to_string()),
89         kind: Some("application/activity+json".to_string()),
90         href: Some(url),
91       },
92     ]
93   } else {
94     vec![]
95   }
96 }