1 use actix_web::{web, web::Query, HttpResponse};
3 use lemmy_api_common::utils::blocking;
4 use lemmy_apub::fetcher::webfinger::{WebfingerLink, WebfingerResponse};
6 source::{community::Community, person::Person},
9 use lemmy_utils::{error::LemmyError, location_info, settings::structs::Settings};
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
14 #[derive(Deserialize)]
19 pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
20 if settings.federation.enabled {
22 ".well-known/webfinger",
23 web::get().to(get_webfinger_response),
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
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(
36 context: web::Data<LemmyContext>,
37 ) -> Result<HttpResponse, LemmyError> {
41 .captures(&info.resource)
42 .and_then(|c| c.get(1))
43 .context(location_info!())?
47 let name_ = name.clone();
48 let user_id: Option<Url> = blocking(context.pool(), move |conn| {
49 Person::read_from_name(conn, &name_)
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)
59 .map(|c| c.actor_id.into());
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.
64 webfinger_link_for_actor(user_id),
65 webfinger_link_for_actor(community_id),
71 let json = WebfingerResponse {
72 subject: info.resource.to_owned(),
76 Ok(HttpResponse::Ok().json(json))
79 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
80 if let Some(url) = url {
83 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
84 kind: Some("text/html".to_string()),
85 href: Some(url.to_owned()),
88 rel: Some("self".to_string()),
89 kind: Some("application/activity+json".to_string()),