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;
12 #[derive(Deserialize)]
17 pub fn config(cfg: &mut web::ServiceConfig) {
19 ".well-known/webfinger",
20 web::get().to(get_webfinger_response),
24 /// Responds to webfinger requests of the following format. There isn't any real documentation for
25 /// this, but it described in this blog post:
26 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
28 /// You can also view the webfinger response that Mastodon sends:
29 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
30 async fn get_webfinger_response(
32 context: web::Data<LemmyContext>,
33 ) -> Result<HttpResponse, LemmyError> {
37 .captures(&info.resource)
38 .and_then(|c| c.get(1))
39 .context(location_info!())?
43 let name_ = name.clone();
44 let user_id: Option<Url> = Person::read_from_name(context.pool(), &name_, false)
47 .map(|c| c.actor_id.into());
48 let community_id: Option<Url> = Community::read_from_name(context.pool(), &name, false)
51 .map(|c| c.actor_id.into());
53 // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
54 // community last so that it gets prioritized. For Lemmy the order doesnt matter.
56 webfinger_link_for_actor(user_id),
57 webfinger_link_for_actor(community_id),
63 let json = WebfingerResponse {
64 subject: info.resource.clone(),
68 Ok(HttpResponse::Ok().json(json))
71 fn webfinger_link_for_actor(url: Option<Url>) -> Vec<WebfingerLink> {
72 if let Some(url) = url {
75 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
76 kind: Some("text/html".to_string()),
77 href: Some(url.clone()),
80 rel: Some("self".to_string()),
81 kind: Some("application/activity+json".to_string()),