]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/webfinger.rs
Add tracing (#1942)
[lemmy.git] / crates / apub / src / fetcher / webfinger.rs
1 use crate::{generate_local_apub_endpoint, EndpointType};
2 use anyhow::anyhow;
3 use itertools::Itertools;
4 use lemmy_apub_lib::{
5   object_id::ObjectId,
6   traits::{ActorType, ApubObject},
7 };
8 use lemmy_db_schema::newtypes::DbUrl;
9 use lemmy_utils::{
10   request::{retry, RecvError},
11   LemmyError,
12 };
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
15 use tracing::debug;
16 use url::Url;
17
18 #[derive(Serialize, Deserialize, Debug)]
19 pub struct WebfingerLink {
20   pub rel: Option<String>,
21   #[serde(rename = "type")]
22   pub kind: Option<String>,
23   pub href: Option<Url>,
24 }
25
26 #[derive(Serialize, Deserialize, Debug)]
27 pub struct WebfingerResponse {
28   pub subject: String,
29   pub links: Vec<WebfingerLink>,
30 }
31
32 /// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and
33 /// outputs the actor id. Used in the API for communities and users.
34 ///
35 /// TODO: later provide a method in ApubObject to generate the endpoint, so that we dont have to
36 ///       pass in EndpointType
37 pub async fn webfinger_resolve<Kind>(
38   identifier: &str,
39   endpoint_type: EndpointType,
40   context: &LemmyContext,
41   request_counter: &mut i32,
42 ) -> Result<DbUrl, LemmyError>
43 where
44   Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
45   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
46 {
47   // remote actor
48   if identifier.contains('@') {
49     webfinger_resolve_actor::<Kind>(identifier, context, request_counter).await
50   }
51   // local actor
52   else {
53     let domain = context.settings().get_protocol_and_hostname();
54     Ok(generate_local_apub_endpoint(
55       endpoint_type,
56       identifier,
57       &domain,
58     )?)
59   }
60 }
61
62 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
63 /// using webfinger.
64 pub(crate) async fn webfinger_resolve_actor<Kind>(
65   identifier: &str,
66   context: &LemmyContext,
67   request_counter: &mut i32,
68 ) -> Result<DbUrl, LemmyError>
69 where
70   Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
71   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
72 {
73   let protocol = context.settings().get_protocol_string();
74   let (_, domain) = identifier
75     .splitn(2, '@')
76     .collect_tuple()
77     .expect("invalid query");
78   let fetch_url = format!(
79     "{}://{}/.well-known/webfinger?resource=acct:{}",
80     protocol, domain, identifier
81   );
82   debug!("Fetching webfinger url: {}", &fetch_url);
83
84   let response = retry(|| context.client().get(&fetch_url).send()).await?;
85
86   let res: WebfingerResponse = response
87     .json()
88     .await
89     .map_err(|e| RecvError(e.to_string()))?;
90
91   let links: Vec<Url> = res
92     .links
93     .iter()
94     .filter(|link| {
95       if let Some(type_) = &link.kind {
96         type_.starts_with("application/")
97       } else {
98         false
99       }
100     })
101     .map(|l| l.href.clone())
102     .flatten()
103     .collect();
104   for l in links {
105     let object = ObjectId::<Kind>::new(l)
106       .dereference(context, request_counter)
107       .await;
108     if object.is_ok() {
109       return object.map(|o| o.actor_id().into());
110     }
111   }
112   Err(anyhow!("Failed to resolve actor for {}", identifier).into())
113 }