]> Untitled Git - lemmy.git/blob - crates/routes/src/webfinger.rs
Dont make webfinger request when viewing community/user profile (fixes #1896) (#2049)
[lemmy.git] / crates / routes / src / webfinger.rs
1 use actix_web::{web, web::Query, HttpResponse};
2 use anyhow::Context;
3 use lemmy_api_common::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::{location_info, settings::structs::Settings, LemmyError};
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     .map(|c| c.get(1))
43     .flatten()
44     .context(location_info!())?
45     .as_str()
46     .to_string();
47
48   let name_ = name.clone();
49   let user_id: Option<Url> = blocking(context.pool(), move |conn| {
50     Person::read_from_name(conn, &name_)
51   })
52   .await?
53   .ok()
54   .map(|c| c.actor_id.into());
55   let community_id: Option<Url> = blocking(context.pool(), move |conn| {
56     Community::read_from_name(conn, &name)
57   })
58   .await?
59   .ok()
60   .map(|c| c.actor_id.into());
61
62   // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
63   // community last so that it gets prioritized. For Lemmy the order doesnt matter.
64   let links = vec![
65     webfinger_link_for_actor(user_id),
66     webfinger_link_for_actor(community_id),
67   ]
68   .into_iter()
69   .flatten()
70   .collect();
71
72   let json = WebfingerResponse {
73     subject: info.resource.to_owned(),
74     links,
75   };
76
77   Ok(HttpResponse::Ok().json(json))
78 }
79
80 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
81   if let Some(url) = url {
82     vec![
83       WebfingerLink {
84         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
85         kind: Some("text/html".to_string()),
86         href: Some(url.to_owned()),
87       },
88       WebfingerLink {
89         rel: Some("self".to_string()),
90         kind: Some("application/activity+json".to_string()),
91         href: Some(url),
92       },
93     ]
94   } else {
95     vec![]
96   }
97 }