2022-03-16 20:11:49 +00:00
|
|
|
use anyhow::anyhow;
|
2021-11-16 17:03:09 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use lemmy_apub_lib::{
|
|
|
|
object_id::ObjectId,
|
|
|
|
traits::{ActorType, ApubObject},
|
|
|
|
};
|
|
|
|
use lemmy_db_schema::newtypes::DbUrl;
|
|
|
|
use lemmy_utils::{
|
|
|
|
request::{retry, RecvError},
|
|
|
|
LemmyError,
|
|
|
|
};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2021-11-23 12:16:47 +00:00
|
|
|
use tracing::debug;
|
2021-11-16 17:03:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebfingerLink {
|
|
|
|
pub rel: Option<String>,
|
2021-11-18 18:28:53 +00:00
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub kind: Option<String>,
|
2021-11-16 17:03:09 +00:00
|
|
|
pub href: Option<Url>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct WebfingerResponse {
|
|
|
|
pub subject: String,
|
|
|
|
pub links: Vec<WebfingerLink>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
|
|
|
/// using webfinger.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-18 18:28:53 +00:00
|
|
|
pub(crate) async fn webfinger_resolve_actor<Kind>(
|
2021-11-16 17:03:09 +00:00
|
|
|
identifier: &str,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<DbUrl, LemmyError>
|
|
|
|
where
|
|
|
|
Kind: ApubObject<DataType = LemmyContext> + ActorType + Send + 'static,
|
|
|
|
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
|
|
|
{
|
|
|
|
let protocol = context.settings().get_protocol_string();
|
|
|
|
let (_, domain) = identifier
|
|
|
|
.splitn(2, '@')
|
|
|
|
.collect_tuple()
|
|
|
|
.expect("invalid query");
|
|
|
|
let fetch_url = format!(
|
|
|
|
"{}://{}/.well-known/webfinger?resource=acct:{}",
|
|
|
|
protocol, domain, identifier
|
|
|
|
);
|
|
|
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
|
|
|
2021-12-05 15:03:13 +00:00
|
|
|
*request_counter += 1;
|
|
|
|
if *request_counter > context.settings().http_fetch_retry_limit {
|
2021-12-07 23:17:19 +00:00
|
|
|
return Err(LemmyError::from_message("Request retry limit reached"));
|
2021-12-05 15:03:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 17:03:09 +00:00
|
|
|
let response = retry(|| context.client().get(&fetch_url).send()).await?;
|
|
|
|
|
|
|
|
let res: WebfingerResponse = response
|
|
|
|
.json()
|
|
|
|
.await
|
|
|
|
.map_err(|e| RecvError(e.to_string()))?;
|
|
|
|
|
|
|
|
let links: Vec<Url> = res
|
|
|
|
.links
|
|
|
|
.iter()
|
2021-11-18 18:28:53 +00:00
|
|
|
.filter(|link| {
|
|
|
|
if let Some(type_) = &link.kind {
|
|
|
|
type_.starts_with("application/")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
2022-03-30 14:58:03 +00:00
|
|
|
.filter_map(|l| l.href.clone())
|
2021-11-16 17:03:09 +00:00
|
|
|
.collect();
|
|
|
|
for l in links {
|
|
|
|
let object = ObjectId::<Kind>::new(l)
|
2021-12-06 22:54:34 +00:00
|
|
|
.dereference(context, context.client(), request_counter)
|
2021-11-16 17:03:09 +00:00
|
|
|
.await;
|
|
|
|
if object.is_ok() {
|
|
|
|
return object.map(|o| o.actor_id().into());
|
|
|
|
}
|
|
|
|
}
|
2022-03-16 20:11:49 +00:00
|
|
|
let err = anyhow!("Failed to resolve actor for {}", identifier);
|
|
|
|
Err(LemmyError::from_error_message(err, "failed_to_resolve"))
|
2021-11-16 17:03:09 +00:00
|
|
|
}
|