1 use actix_web::{web, web::Query, HttpResponse};
3 use lemmy_api_common::context::LemmyContext;
5 source::{community::Community, person::Person},
8 use lemmy_utils::{error::LemmyError, location_info, WebfingerLink, WebfingerResponse};
9 use serde::Deserialize;
10 use std::collections::HashMap;
13 #[derive(Deserialize)]
18 pub fn config(cfg: &mut web::ServiceConfig) {
20 ".well-known/webfinger",
21 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)
39 .and_then(|c| c.get(1))
40 .context(location_info!())?
44 let name_ = name.clone();
45 let user_id: Option<Url> = Person::read_from_name(context.pool(), &name_, false)
48 .map(|c| c.actor_id.into());
49 let community_id: Option<Url> = Community::read_from_name(context.pool(), &name, false)
52 .map(|c| c.actor_id.into());
54 // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
55 // community last so that it gets prioritized. For Lemmy the order doesnt matter.
57 webfinger_link_for_actor(user_id, "Person"),
58 webfinger_link_for_actor(community_id, "Group"),
64 let json = WebfingerResponse {
65 subject: info.resource.clone(),
69 Ok(HttpResponse::Ok().json(json))
72 fn webfinger_link_for_actor(url: Option<Url>, kind: &str) -> Vec<WebfingerLink> {
73 if let Some(url) = url {
74 let mut properties = HashMap::new();
76 "https://www.w3.org/ns/activitystreams#type".to_string(),
81 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
82 kind: Some("text/html".to_string()),
83 href: Some(url.clone()),
84 properties: Default::default(),
87 rel: Some("self".to_string()),
88 kind: Some("application/activity+json".to_string()),