]> Untitled Git - lemmy.git/blob - crates/routes/src/webfinger.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / routes / src / webfinger.rs
1 use activitypub_federation::{
2   config::Data,
3   fetch::webfinger::{extract_webfinger_name, Webfinger, WebfingerLink},
4 };
5 use actix_web::{web, web::Query, HttpResponse};
6 use lemmy_api_common::context::LemmyContext;
7 use lemmy_db_schema::{
8   source::{community::Community, person::Person},
9   traits::ApubActor,
10 };
11 use lemmy_utils::error::LemmyError;
12 use serde::Deserialize;
13 use std::collections::HashMap;
14 use url::Url;
15
16 #[derive(Deserialize)]
17 struct Params {
18   resource: String,
19 }
20
21 pub fn config(cfg: &mut web::ServiceConfig) {
22   cfg.route(
23     ".well-known/webfinger",
24     web::get().to(get_webfinger_response),
25   );
26 }
27
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
31 ///
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(
35   info: Query<Params>,
36   context: Data<LemmyContext>,
37 ) -> Result<HttpResponse, LemmyError> {
38   let name = extract_webfinger_name(&info.resource, &context)?;
39
40   let name_ = name.clone();
41   let user_id: Option<Url> = Person::read_from_name(&mut context.pool(), &name_, false)
42     .await
43     .ok()
44     .map(|c| c.actor_id.into());
45   let community_id: Option<Url> = Community::read_from_name(&mut context.pool(), &name, false)
46     .await
47     .ok()
48     .map(|c| c.actor_id.into());
49
50   // Mastodon seems to prioritize the last webfinger item in case of duplicates. Put
51   // community last so that it gets prioritized. For Lemmy the order doesnt matter.
52   let links = vec![
53     webfinger_link_for_actor(user_id, "Person"),
54     webfinger_link_for_actor(community_id, "Group"),
55   ]
56   .into_iter()
57   .flatten()
58   .collect();
59
60   let json = Webfinger {
61     subject: info.resource.clone(),
62     links,
63     ..Default::default()
64   };
65
66   Ok(HttpResponse::Ok().json(json))
67 }
68
69 fn webfinger_link_for_actor(url: Option<Url>, kind: &str) -> Vec<WebfingerLink> {
70   if let Some(url) = url {
71     let mut properties = HashMap::new();
72     properties.insert(
73       "https://www.w3.org/ns/activitystreams#type"
74         .parse()
75         .expect("parse url"),
76       kind.to_string(),
77     );
78     vec![
79       WebfingerLink {
80         rel: Some("http://webfinger.net/rel/profile-page".to_string()),
81         kind: Some("text/html".to_string()),
82         href: Some(url.clone()),
83         ..Default::default()
84       },
85       WebfingerLink {
86         rel: Some("self".to_string()),
87         kind: Some("application/activity+json".to_string()),
88         href: Some(url),
89         properties,
90       },
91     ]
92   } else {
93     vec![]
94   }
95 }