]> Untitled Git - lemmy.git/blob - crates/routes/src/webfinger.rs
Activitypub crate rewrite (#2782)
[lemmy.git] / crates / routes / src / webfinger.rs
1 use activitypub_federation::fetch::webfinger::{Webfinger, WebfingerLink};
2 use actix_web::{web, web::Query, HttpResponse};
3 use anyhow::Context;
4 use lemmy_api_common::context::LemmyContext;
5 use lemmy_db_schema::{
6   source::{community::Community, person::Person},
7   traits::ApubActor,
8 };
9 use lemmy_utils::{error::LemmyError, location_info};
10 use serde::Deserialize;
11 use std::collections::HashMap;
12 use url::Url;
13
14 #[derive(Deserialize)]
15 struct Params {
16   resource: String,
17 }
18
19 pub fn config(cfg: &mut web::ServiceConfig) {
20   cfg.route(
21     ".well-known/webfinger",
22     web::get().to(get_webfinger_response),
23   );
24 }
25
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
29 ///
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(
33   info: Query<Params>,
34   context: web::Data<LemmyContext>,
35 ) -> Result<HttpResponse, LemmyError> {
36   let name = context
37     .settings()
38     .webfinger_regex()
39     .captures(&info.resource)
40     .and_then(|c| c.get(1))
41     .context(location_info!())?
42     .as_str()
43     .to_string();
44
45   let name_ = name.clone();
46   let user_id: Option<Url> = Person::read_from_name(context.pool(), &name_, false)
47     .await
48     .ok()
49     .map(|c| c.actor_id.into());
50   let community_id: Option<Url> = Community::read_from_name(context.pool(), &name, false)
51     .await
52     .ok()
53     .map(|c| c.actor_id.into());
54
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.
57   let links = vec![
58     webfinger_link_for_actor(user_id, "Person"),
59     webfinger_link_for_actor(community_id, "Group"),
60   ]
61   .into_iter()
62   .flatten()
63   .collect();
64
65   let json = Webfinger {
66     subject: info.resource.clone(),
67     links,
68     ..Default::default()
69   };
70
71   Ok(HttpResponse::Ok().json(json))
72 }
73
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();
77     properties.insert(
78       "https://www.w3.org/ns/activitystreams#type"
79         .parse()
80         .expect("parse url"),
81       kind.to_string(),
82     );
83     vec![
84       WebfingerLink {
85         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
86         kind: Some("text/html".to_string()),
87         href: Some(url.clone()),
88         ..Default::default()
89       },
90       WebfingerLink {
91         rel: Some("self".to_string()),
92         kind: Some("application/activity+json".to_string()),
93         href: Some(url),
94         properties,
95       },
96     ]
97   } else {
98     vec![]
99   }
100 }