1 use crate::{generate_local_apub_endpoint, EndpointType};
2 use itertools::Itertools;
5 traits::{ActorType, ApubObject},
7 use lemmy_db_schema::newtypes::DbUrl;
9 request::{retry, RecvError},
12 use lemmy_websocket::LemmyContext;
13 use serde::{Deserialize, Serialize};
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>,
25 #[derive(Serialize, Deserialize, Debug)]
26 pub struct WebfingerResponse {
28 pub links: Vec<WebfingerLink>,
31 /// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and
32 /// outputs the actor id. Used in the API for communities and users.
34 /// TODO: later provide a method in ApubObject to generate the endpoint, so that we dont have to
35 /// pass in EndpointType
36 #[tracing::instrument(skip_all)]
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 #[tracing::instrument(skip_all)]
65 pub(crate) async fn webfinger_resolve_actor<Kind>(
67 context: &LemmyContext,
68 request_counter: &mut i32,
69 ) -> Result<DbUrl, LemmyError>
71 Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
72 for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
74 let protocol = context.settings().get_protocol_string();
75 let (_, domain) = identifier
78 .expect("invalid query");
79 let fetch_url = format!(
80 "{}://{}/.well-known/webfinger?resource=acct:{}",
81 protocol, domain, identifier
83 debug!("Fetching webfinger url: {}", &fetch_url);
85 *request_counter += 1;
86 if *request_counter > context.settings().http_fetch_retry_limit {
87 return Err(LemmyError::from_message("Request retry limit reached"));
90 let response = retry(|| context.client().get(&fetch_url).send()).await?;
92 let res: WebfingerResponse = response
95 .map_err(|e| RecvError(e.to_string()))?;
97 let links: Vec<Url> = res
101 if let Some(type_) = &link.kind {
102 type_.starts_with("application/")
107 .map(|l| l.href.clone())
111 let object = ObjectId::<Kind>::new(l)
112 .dereference(context, context.client(), request_counter)
115 return object.map(|o| o.actor_id().into());
118 let error = LemmyError::from(anyhow::anyhow!(
119 "Failed to resolve actor for {}",
122 Err(error.with_message("failed_to_resolve"))