1 use activitypub_federation::fetch::webfinger::{Webfinger, WebfingerLink};
2 use actix_web::{web, web::Query, HttpResponse};
4 use lemmy_api_common::context::LemmyContext;
6 source::{community::Community, person::Person},
9 use lemmy_utils::{error::LemmyError, location_info};
10 use serde::Deserialize;
11 use std::collections::HashMap;
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> = Person::read_from_name(context.pool(), &name_, false)
49 .map(|c| c.actor_id.into());
50 let community_id: Option<Url> = Community::read_from_name(context.pool(), &name, false)
53 .map(|c| c.actor_id.into());
55 // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
56 // community last so that it gets prioritized. For Lemmy the order doesnt matter.
58 webfinger_link_for_actor(user_id, "Person"),
59 webfinger_link_for_actor(community_id, "Group"),
65 let json = Webfinger {
66 subject: info.resource.clone(),
71 Ok(HttpResponse::Ok().json(json))
74 fn webfinger_link_for_actor(url: Option<Url>, kind: &str) -> Vec<WebfingerLink> {
75 if let Some(url) = url {
76 let mut properties = HashMap::new();
78 "https://www.w3.org/ns/activitystreams#type"
85 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
86 kind: Some("text/html".to_string()),
87 href: Some(url.clone()),
91 rel: Some("self".to_string()),
92 kind: Some("application/activity+json".to_string()),