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 community_id: Option<Url> = blocking(context.pool(), move |conn| {
47 Community::read_from_name(conn, &name_)
51 .map(|c| c.actor_id.into());
52 let user_id: Option<Url> = blocking(context.pool(), move |conn| {
53 Person::find_by_name(conn, &name)
57 .map(|c| c.actor_id.into());
59 webfinger_link_for_actor(community_id),
60 webfinger_link_for_actor(user_id),
66 let json = WebfingerResponse {
67 subject: info.resource.to_owned(),
71 Ok(HttpResponse::Ok().json(json))
74 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
75 if let Some(url) = url {
78 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
79 type_: Some("text/html".to_string()),
80 href: Some(url.to_owned()),
83 rel: Some("self".to_string()),
84 type_: Some("application/activity+json".to_string()),