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