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};
10 use lemmy_websocket::LemmyContext;
11 use serde::Deserialize;
14 #[derive(Deserialize)]
19 pub fn config(cfg: &mut web::ServiceConfig) {
21 ".well-known/webfinger",
22 web::get().to(get_webfinger_response),
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
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(
34 context: web::Data<LemmyContext>,
35 ) -> Result<HttpResponse, LemmyError> {
39 .captures(&info.resource)
40 .and_then(|c| c.get(1))
41 .context(location_info!())?
45 let name_ = name.clone();
46 let user_id: Option<Url> = blocking(context.pool(), move |conn| {
47 Person::read_from_name(conn, &name_, false)
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)
57 .map(|c| c.actor_id.into());
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.
62 webfinger_link_for_actor(user_id),
63 webfinger_link_for_actor(community_id),
69 let json = WebfingerResponse {
70 subject: info.resource.to_owned(),
74 Ok(HttpResponse::Ok().json(json))
77 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
78 if let Some(url) = url {
81 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
82 kind: Some("text/html".to_string()),
83 href: Some(url.to_owned()),
86 rel: Some("self".to_string()),
87 kind: Some("application/activity+json".to_string()),