1 use actix_web::{error::ErrorBadRequest, web::Query, *};
3 use lemmy_db::{community::Community, user::User_};
4 use lemmy_structs::{blocking, WebFingerLink, WebFingerResponse};
8 WEBFINGER_COMMUNITY_REGEX,
11 use lemmy_websocket::LemmyContext;
12 use serde::Deserialize;
14 #[derive(Deserialize)]
19 pub fn config(cfg: &mut web::ServiceConfig) {
20 if Settings::get().federation.enabled {
22 ".well-known/webfinger",
23 web::get().to(get_webfinger_response),
28 /// Responds to webfinger requests of the following format. There isn't any real documentation for
29 /// this, but it described in this blog post:
30 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
32 /// You can also view the webfinger response that Mastodon sends:
33 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
34 async fn get_webfinger_response(
36 context: web::Data<LemmyContext>,
37 ) -> Result<HttpResponse, Error> {
38 let community_regex_parsed = WEBFINGER_COMMUNITY_REGEX
39 .captures(&info.resource)
43 let user_regex_parsed = WEBFINGER_USER_REGEX
44 .captures(&info.resource)
48 let url = if let Some(community_name) = community_regex_parsed {
49 let community_name = community_name.as_str().to_owned();
50 // Make sure the requested community exists.
51 blocking(context.pool(), move |conn| {
52 Community::read_from_name(conn, &community_name)
55 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
57 } else if let Some(user_name) = user_regex_parsed {
58 let user_name = user_name.as_str().to_owned();
59 // Make sure the requested user exists.
60 blocking(context.pool(), move |conn| {
61 User_::read_from_name(conn, &user_name)
64 .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?
67 return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
70 let json = WebFingerResponse {
71 subject: info.resource.to_owned(),
72 aliases: vec![url.to_owned()],
75 rel: Some("http://webfinger.net/rel/profile-page".to_string()),
76 type_: Some("text/html".to_string()),
77 href: Some(url.to_owned()),
81 rel: Some("self".to_string()),
82 type_: Some("application/activity+json".to_string()),
85 }, // TODO: this also needs to return the subscribe link once that's implemented
87 // "rel": "http://ostatus.org/schema/1.0/subscribe",
88 // "template": "https://my_instance.com/authorize_interaction?uri={uri}"
93 Ok(HttpResponse::Ok().json(json))