2022-11-21 16:44:34 +00:00
|
|
|
use crate::{local_instance, ActorType, FEDERATION_HTTP_FETCH_LIMIT};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{core::object_id::ObjectId, traits::ApubObject};
|
2022-03-16 20:11:49 +00:00
|
|
|
use anyhow::anyhow;
|
2021-11-16 17:03:09 +00:00
|
|
|
use itertools::Itertools;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-11-21 16:44:34 +00:00
|
|
|
use lemmy_db_schema::newtypes::DbUrl;
|
2022-11-26 20:34:38 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, WebfingerResponse};
|
2021-11-23 12:16:47 +00:00
|
|
|
use tracing::debug;
|
2021-11-16 17:03:09 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
|
|
|
/// using webfinger.
|
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,
|
2022-10-13 16:30:31 +00:00
|
|
|
local_only: bool,
|
2021-11-16 17:03:09 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<DbUrl, LemmyError>
|
|
|
|
where
|
2022-06-02 14:33:41 +00:00
|
|
|
Kind: ApubObject<DataType = LemmyContext, Error = LemmyError> + ActorType + Send + 'static,
|
2021-11-16 17:03:09 +00:00
|
|
|
for<'de2> <Kind as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
|
|
|
{
|
|
|
|
let protocol = context.settings().get_protocol_string();
|
|
|
|
let (_, domain) = identifier
|
|
|
|
.splitn(2, '@')
|
|
|
|
.collect_tuple()
|
2022-07-27 21:03:44 +00:00
|
|
|
.ok_or_else(|| LemmyError::from_message("Invalid webfinger query, missing domain"))?;
|
2023-01-30 19:17:24 +00:00
|
|
|
let fetch_url = format!("{protocol}://{domain}/.well-known/webfinger?resource=acct:{identifier}");
|
2021-11-16 17:03:09 +00:00
|
|
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
|
|
|
2021-12-05 15:03:13 +00:00
|
|
|
*request_counter += 1;
|
2022-11-21 16:44:34 +00:00
|
|
|
if *request_counter > FEDERATION_HTTP_FETCH_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
|
|
|
}
|
|
|
|
|
2022-06-02 14:33:41 +00:00
|
|
|
let response = context.client().get(&fetch_url).send().await?;
|
2021-11-16 17:03:09 +00:00
|
|
|
|
2022-05-03 17:44:13 +00:00
|
|
|
let res: WebfingerResponse = response.json().await.map_err(LemmyError::from)?;
|
2021-11-16 17:03:09 +00:00
|
|
|
|
|
|
|
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 {
|
2022-10-13 16:30:31 +00:00
|
|
|
let object_id = ObjectId::<Kind>::new(l);
|
|
|
|
let object = if local_only {
|
|
|
|
object_id.dereference_local(context).await
|
|
|
|
} else {
|
|
|
|
object_id
|
2022-11-09 10:05:00 +00:00
|
|
|
.dereference(context, local_instance(context).await, request_counter)
|
2022-10-13 16:30:31 +00:00
|
|
|
.await
|
|
|
|
};
|
2021-11-16 17:03:09 +00:00
|
|
|
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
|
|
|
}
|