1 use crate::db::community::Community;
4 use actix_web::web::Query;
5 use actix_web::HttpResponse;
6 use diesel::r2d2::{ConnectionManager, Pool};
7 use diesel::PgConnection;
9 use serde::Deserialize;
12 #[derive(Deserialize)]
17 pub fn config(cfg: &mut web::ServiceConfig) {
18 if Settings::get().federation_enabled {
20 ".well-known/webfinger",
21 web::get().to(get_webfinger_response),
27 static ref WEBFINGER_COMMUNITY_REGEX: Regex = Regex::new(&format!(
28 "^group:([a-z0-9_]{{3, 20}})@{}$",
29 Settings::get().hostname
34 /// Responds to webfinger requests of the following format. There isn't any real documentation for
35 /// this, but it described in this blog post:
36 /// https://mastodon.social/.well-known/webfinger?resource=acct:gargron@mastodon.social
38 /// You can also view the webfinger response that Mastodon sends:
39 /// https://radical.town/.well-known/webfinger?resource=acct:felix@radical.town
40 async fn get_webfinger_response(
42 db: web::Data<Pool<ConnectionManager<PgConnection>>>,
43 ) -> Result<HttpResponse, actix_web::Error> {
44 let res = web::block(move || {
47 let regex_parsed = WEBFINGER_COMMUNITY_REGEX
48 .captures(&info.resource)
50 // TODO: replace this with .flatten() once we are running rust 1.40
51 let regex_parsed_flattened = match regex_parsed {
55 let community_name = match regex_parsed_flattened {
56 Some(c) => c.as_str(),
57 None => return Err(format_err!("not_found")),
60 // Make sure the requested community exists.
61 let community = match Community::read_from_name(&conn, community_name.to_string()) {
63 Err(_) => return Err(format_err!("not_found")),
66 let community_url = community.get_url();
69 "subject": info.resource,
75 "rel": "http://webfinger.net/rel/profile-page",
81 "type": "application/activity+json",
82 // Yes this is correct, this link doesn't include the `.json` extension
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}"
94 .map(|json| HttpResponse::Ok().json(json))
95 .map_err(|_| HttpResponse::InternalServerError())?;