mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-05-08 04:52:40 +00:00
a610211557
* Try to fix docker/drone plugin * Trying to use one rust image. * Fixing drone 1. * Fixing drone 2. * Add drone notify. * Fixing drone 3. * Fixing drone 4. * Fix clippy. * Remove uninlined format lint. * Combine all cargo tasks * Fixing drone 5. * Fixing drone 6. * Fixing drone 7. * Fixing drone 8. * Fixing drone 9. * Fixing drone 10. * Fixing drone 12. * Fixing drone 13. * Fixing drone 14. * Fixing drone 15. * Fixing drone 16. * Fixing drone 17. * Fixing drone 18. * Fixing drone 19. * Fixing drone 20. * Fixing drone 21. * Fixing drone 22. * Fixing drone 23. * Fixing drone 24. * Fixing drone 25. * Fixing drone 26. * Fixing drone 27. * Fixing drone 28. * Fixing drone 29. * Fixing drone 30. * Fixing drone 31. * Fixing drone 32. * Fixing drone 33. * Fixing drone 34. * Fixing drone 35. * Fixing drone 36. * Fixing drone 37. * Fixing drone 38. * Fixing drone 39. * Fixing drone 40. * Fixing drone 41. * Fixing drone 43. * Fixing drone 44. * Fixing drone 45. * Last cleanup. * Fixing drone 46. * Separate ci steps (#2679) * separate ci steps * fix 1 * add comments * dont add rustfmt explicitly * Revert "dont add rustfmt explicitly" This reverts commit 358ce3302a134b7ac88d90a854079356995e9725. * dont use all features for tests --------- Co-authored-by: Nutomic <me@nutomic.com>
68 lines
2.2 KiB
Rust
68 lines
2.2 KiB
Rust
use crate::{local_instance, ActorType, FEDERATION_HTTP_FETCH_LIMIT};
|
|
use activitypub_federation::{core::object_id::ObjectId, traits::ApubObject};
|
|
use anyhow::anyhow;
|
|
use itertools::Itertools;
|
|
use lemmy_api_common::context::LemmyContext;
|
|
use lemmy_db_schema::newtypes::DbUrl;
|
|
use lemmy_utils::{error::LemmyError, WebfingerResponse};
|
|
use tracing::debug;
|
|
use url::Url;
|
|
|
|
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
|
|
/// using webfinger.
|
|
pub(crate) async fn webfinger_resolve_actor<Kind>(
|
|
identifier: &str,
|
|
local_only: bool,
|
|
context: &LemmyContext,
|
|
request_counter: &mut i32,
|
|
) -> Result<DbUrl, LemmyError>
|
|
where
|
|
Kind: ApubObject<DataType = LemmyContext, Error = LemmyError> + 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()
|
|
.ok_or_else(|| LemmyError::from_message("Invalid webfinger query, missing domain"))?;
|
|
let fetch_url = format!("{protocol}://{domain}/.well-known/webfinger?resource=acct:{identifier}");
|
|
debug!("Fetching webfinger url: {}", &fetch_url);
|
|
|
|
*request_counter += 1;
|
|
if *request_counter > FEDERATION_HTTP_FETCH_LIMIT {
|
|
return Err(LemmyError::from_message("Request retry limit reached"));
|
|
}
|
|
|
|
let response = context.client().get(&fetch_url).send().await?;
|
|
|
|
let res: WebfingerResponse = response.json().await.map_err(LemmyError::from)?;
|
|
|
|
let links: Vec<Url> = res
|
|
.links
|
|
.iter()
|
|
.filter(|link| {
|
|
if let Some(type_) = &link.kind {
|
|
type_.starts_with("application/")
|
|
} else {
|
|
false
|
|
}
|
|
})
|
|
.filter_map(|l| l.href.clone())
|
|
.collect();
|
|
for l in links {
|
|
let object_id = ObjectId::<Kind>::new(l);
|
|
let object = if local_only {
|
|
object_id.dereference_local(context).await
|
|
} else {
|
|
object_id
|
|
.dereference(context, local_instance(context).await, request_counter)
|
|
.await
|
|
};
|
|
if object.is_ok() {
|
|
return object.map(|o| o.actor_id().into());
|
|
}
|
|
}
|
|
let err = anyhow!("Failed to resolve actor for {}", identifier);
|
|
Err(LemmyError::from_error_message(err, "failed_to_resolve"))
|
|
}
|