]> Untitled Git - lemmy.git/blob - crates/apub/src/fetcher/webfinger.rs
d039c1c1744158c6a5d7295aacb8496d07859c26
[lemmy.git] / crates / apub / src / fetcher / webfinger.rs
1 use itertools::Itertools;
2 use lemmy_apub_lib::{
3   object_id::ObjectId,
4   traits::{ActorType, ApubObject},
5 };
6 use lemmy_db_schema::newtypes::DbUrl;
7 use lemmy_utils::{
8   request::{retry, RecvError},
9   LemmyError,
10 };
11 use lemmy_websocket::LemmyContext;
12 use serde::{Deserialize, Serialize};
13 use tracing::debug;
14 use url::Url;
15
16 #[derive(Serialize, Deserialize, Debug)]
17 pub struct WebfingerLink {
18   pub rel: Option<String>,
19   #[serde(rename = "type")]
20   pub kind: Option<String>,
21   pub href: Option<Url>,
22 }
23
24 #[derive(Serialize, Deserialize, Debug)]
25 pub struct WebfingerResponse {
26   pub subject: String,
27   pub links: Vec<WebfingerLink>,
28 }
29
30 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
31 /// using webfinger.
32 #[tracing::instrument(skip_all)]
33 pub(crate) async fn webfinger_resolve_actor<Kind>(
34   identifier: &str,
35   context: &LemmyContext,
36   request_counter: &mut i32,
37 ) -> Result<DbUrl, LemmyError>
38 where
39   Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
40   for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
41 {
42   let protocol = context.settings().get_protocol_string();
43   let (_, domain) = identifier
44     .splitn(2, '@')
45     .collect_tuple()
46     .expect("invalid query");
47   let fetch_url = format!(
48     "{}://{}/.well-known/webfinger?resource=acct:{}",
49     protocol, domain, identifier
50   );
51   debug!("Fetching webfinger url: {}", &fetch_url);
52
53   *request_counter += 1;
54   if *request_counter > context.settings().http_fetch_retry_limit {
55     return Err(LemmyError::from_message("Request retry limit reached"));
56   }
57
58   let response = retry(|| context.client().get(&fetch_url).send()).await?;
59
60   let res: WebfingerResponse = response
61     .json()
62     .await
63     .map_err(|e| RecvError(e.to_string()))?;
64
65   let links: Vec<Url> = res
66     .links
67     .iter()
68     .filter(|link| {
69       if let Some(type_) = &link.kind {
70         type_.starts_with("application/")
71       } else {
72         false
73       }
74     })
75     .map(|l| l.href.clone())
76     .flatten()
77     .collect();
78   for l in links {
79     let object = ObjectId::<Kind>::new(l)
80       .dereference(context, context.client(), request_counter)
81       .await;
82     if object.is_ok() {
83       return object.map(|o| o.actor_id().into());
84     }
85   }
86   let error = LemmyError::from(anyhow::anyhow!(
87     "Failed to resolve actor for {}",
88     identifier
89   ));
90   Err(error.with_message("failed_to_resolve"))
91 }