1 use actix_web::{web, web::Query, HttpResponse};
3 use lemmy_api_common::blocking;
4 use lemmy_apub::fetcher::webfinger::{WebfingerLink, WebfingerResponse};
5 use lemmy_db_schema::source::{community::Community, person::Person};
6 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
7 use lemmy_websocket::LemmyContext;
8 use serde::Deserialize;
11 #[derive(Deserialize)]
16 pub fn config(cfg: &mut web::ServiceConfig, settings: &Settings) {
17 if settings.federation.enabled {
19 ".well-known/webfinger",
20 web::get().to(get_webfinger_response),
25 /// Responds to webfinger requests of the following format. There isn't any real documentation for
26 /// this, but it described in this blog post:
27 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
29 /// You can also view the webfinger response that Mastodon sends:
30 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
31 async fn get_webfinger_response(
33 context: web::Data<LemmyContext>,
34 ) -> Result<HttpResponse, LemmyError> {
38 .captures(&info.resource)
41 .context(location_info!())?
45 let name_ = name.clone();
46 let user_id: Option<Url> = blocking(context.pool(), move |conn| {
47 Person::find_by_name(conn, &name_)
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)
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()),