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)
51 let community_name = match regex_parsed {
52 Some(c) => c.as_str(),
53 None => return Err(format_err!("not_found")),
56 // Make sure the requested community exists.
57 let community = match Community::read_from_name(&conn, community_name.to_string()) {
59 Err(_) => return Err(format_err!("not_found")),
62 let community_url = community.get_url();
65 "subject": info.resource,
71 "rel": "http://webfinger.net/rel/profile-page",
73 "href": community.get_url(),
77 "type": "application/activity+json",
80 // TODO: this also needs to return the subscribe link once that's implemented
82 // "rel": "http://ostatus.org/schema/1.0/subscribe",
83 // "template": "https://my_instance.com/authorize_interaction?uri={uri}"
89 .map(|json| HttpResponse::Ok().json(json))
90 .map_err(|_| HttpResponse::InternalServerError())?;