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