1 use crate::{generate_local_apub_endpoint, EndpointType};
3 use itertools::Itertools;
6 traits::{ActorType, ApubObject},
8 use lemmy_db_schema::newtypes::DbUrl;
10 request::{retry, RecvError},
13 use lemmy_websocket::LemmyContext;
14 use serde::{Deserialize, Serialize};
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>,
26 #[derive(Serialize, Deserialize, Debug)]
27 pub struct WebfingerResponse {
29 pub links: Vec<WebfingerLink>,
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.
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>(
39 endpoint_type: EndpointType,
40 context: &LemmyContext,
41 request_counter: &mut i32,
42 ) -> Result<DbUrl, LemmyError>
44 Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
45 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
48 if identifier.contains('@') {
49 webfinger_resolve_actor::<Kind>(identifier, context, request_counter).await
53 let domain = context.settings().get_protocol_and_hostname();
54 Ok(generate_local_apub_endpoint(
62 /// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
64 pub(crate) async fn webfinger_resolve_actor<Kind>(
66 context: &LemmyContext,
67 request_counter: &mut i32,
68 ) -> Result<DbUrl, LemmyError>
70 Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
71 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
73 let protocol = context.settings().get_protocol_string();
74 let (_, domain) = identifier
77 .expect("invalid query");
78 let fetch_url = format!(
79 "{}://{}/.well-known/webfinger?resource=acct:{}",
80 protocol, domain, identifier
82 debug!("Fetching webfinger url: {}", &fetch_url);
84 *request_counter += 1;
85 if *request_counter > context.settings().http_fetch_retry_limit {
86 return Err(LemmyError::from(anyhow!("Request retry limit reached")));
89 let response = retry(|| context.client().get(&fetch_url).send()).await?;
91 let res: WebfingerResponse = response
94 .map_err(|e| RecvError(e.to_string()))?;
96 let links: Vec<Url> = res
100 if let Some(type_) = &link.kind {
101 type_.starts_with("application/")
106 .map(|l| l.href.clone())
110 let object = ObjectId::<Kind>::new(l)
111 .dereference(context, request_counter)
114 return object.map(|o| o.actor_id().into());
117 Err(anyhow!("Failed to resolve actor for {}", identifier).into())