From d79d1f4ede5b3a408571abb7e5b05b24c84ed94b Mon Sep 17 00:00:00 2001 From: aumetra Date: Mon, 13 Nov 2023 21:32:29 +0100 Subject: [PATCH] remove kitsune-activitypub webfinger dependence --- Cargo.lock | 2 - crates/kitsune-activitypub/Cargo.toml | 2 +- crates/kitsune-activitypub/src/error.rs | 7 +-- .../kitsune-activitypub/src/fetcher/actor.rs | 8 +++- .../kitsune-activitypub/src/fetcher/emoji.rs | 6 ++- crates/kitsune-activitypub/src/fetcher/mod.rs | 20 ++++++--- .../kitsune-activitypub/src/fetcher/object.rs | 6 ++- crates/kitsune-activitypub/src/lib.rs | 45 +++++++++++++------ crates/kitsune-core/Cargo.toml | 2 - crates/kitsune-core/src/resolve/mod.rs | 3 -- crates/kitsune-core/src/resolve/post.rs | 1 - crates/kitsune-core/src/traits/fetcher.rs | 2 +- crates/kitsune-core/src/traits/resolver.rs | 2 +- crates/kitsune-jobs/Cargo.toml | 12 +++-- 14 files changed, 74 insertions(+), 44 deletions(-) delete mode 100644 crates/kitsune-core/src/resolve/mod.rs delete mode 100644 crates/kitsune-core/src/resolve/post.rs diff --git a/Cargo.lock b/Cargo.lock index a64cc07de..a9b3db61d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3008,7 +3008,6 @@ dependencies = [ "kitsune-db", "kitsune-email", "kitsune-embed", - "kitsune-http-client", "kitsune-messaging", "kitsune-search", "kitsune-storage", @@ -3016,7 +3015,6 @@ dependencies = [ "kitsune-type", "kitsune-util", "mime", - "post-process", "pretty_assertions", "pulldown-cmark", "redis", diff --git a/crates/kitsune-activitypub/Cargo.toml b/crates/kitsune-activitypub/Cargo.toml index 72872c58f..8bb230df6 100644 --- a/crates/kitsune-activitypub/Cargo.toml +++ b/crates/kitsune-activitypub/Cargo.toml @@ -25,7 +25,6 @@ kitsune-language = { path = "../kitsune-language" } kitsune-search = { path = "../kitsune-search" } kitsune-type = { path = "../kitsune-type" } kitsune-util = { path = "../kitsune-util" } -kitsune-webfinger = { path = "../kitsune-webfinger" } mime = "0.3.17" mime_guess = { version = "2.0.4", default-features = false } pulldown-cmark = { version = "0.9.3", default-features = false, features = [ @@ -49,6 +48,7 @@ sha2 = { version = "0.10.8", features = ["asm"] } hyper = "0.14.27" kitsune-config = { path = "../kitsune-config" } kitsune-test = { path = "../kitsune-test" } +kitsune-webfinger = { path = "../kitsune-webfinger" } pretty_assertions = "1.4.0" serial_test = "2.0.0" tokio = { version = "1.34.0", features = ["macros"] } diff --git a/crates/kitsune-activitypub/src/error.rs b/crates/kitsune-activitypub/src/error.rs index d76997c81..4e61f73aa 100644 --- a/crates/kitsune-activitypub/src/error.rs +++ b/crates/kitsune-activitypub/src/error.rs @@ -1,4 +1,5 @@ use diesel_async::pooled_connection::deadpool::PoolError as DatabasePoolError; +use kitsune_core::error::BoxError; use kitsune_http_signatures::ring; use rsa::pkcs8::der; use std::convert::Infallible; @@ -50,6 +51,9 @@ pub enum Error { #[error("Missing host")] MissingHost, + #[error(transparent)] + Resolver(BoxError), + #[error(transparent)] Search(#[from] kitsune_search::Error), @@ -58,9 +62,6 @@ pub enum Error { #[error(transparent)] UrlParse(#[from] url::ParseError), - - #[error(transparent)] - Webfinger(#[from] kitsune_webfinger::error::Error), } impl From for Error { diff --git a/crates/kitsune-activitypub/src/fetcher/actor.rs b/crates/kitsune-activitypub/src/fetcher/actor.rs index e34ac36fd..c3bc8296c 100644 --- a/crates/kitsune-activitypub/src/fetcher/actor.rs +++ b/crates/kitsune-activitypub/src/fetcher/actor.rs @@ -18,7 +18,10 @@ use kitsune_util::{convert::timestamp_to_uuid, sanitize::CleanHtmlExt}; use scoped_futures::ScopedFutureExt; use url::Url; -impl Fetcher { +impl Fetcher +where + R: Resolver, +{ /// Fetch an ActivityPub actor /// /// # Panics @@ -66,7 +69,8 @@ impl Fetcher { match self .resolver .resolve_account(&actor.preferred_username, domain) - .await? + .await + .map_err(|err| Error::Resolver(err.into()))? { Some(resource) if resource.uri == actor.id => { actor.preferred_username = resource.username; diff --git a/crates/kitsune-activitypub/src/fetcher/emoji.rs b/crates/kitsune-activitypub/src/fetcher/emoji.rs index 286c35b51..b4fe6fd03 100644 --- a/crates/kitsune-activitypub/src/fetcher/emoji.rs +++ b/crates/kitsune-activitypub/src/fetcher/emoji.rs @@ -3,6 +3,7 @@ use crate::error::{Error, Result}; use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use iso8601_timestamp::Timestamp; +use kitsune_core::traits::Resolver; use kitsune_db::{ model::{ custom_emoji::CustomEmoji, @@ -15,7 +16,10 @@ use scoped_futures::ScopedFutureExt; use speedy_uuid::Uuid; use url::Url; -impl Fetcher { +impl Fetcher +where + R: Resolver, +{ pub(crate) async fn fetch_emoji(&self, url: &str) -> Result { let existing_emoji = self .db_pool diff --git a/crates/kitsune-activitypub/src/fetcher/mod.rs b/crates/kitsune-activitypub/src/fetcher/mod.rs index 43a7c0658..e00b13891 100644 --- a/crates/kitsune-activitypub/src/fetcher/mod.rs +++ b/crates/kitsune-activitypub/src/fetcher/mod.rs @@ -3,7 +3,7 @@ use headers::{ContentType, HeaderMapExt}; use http::HeaderValue; use kitsune_cache::ArcCache; use kitsune_consts::USER_AGENT; -use kitsune_core::traits::{fetcher::AccountFetchOptions, Fetcher as FetcherTrait}; +use kitsune_core::traits::{fetcher::AccountFetchOptions, Fetcher as FetcherTrait, Resolver}; use kitsune_db::{ model::{account::Account, custom_emoji::CustomEmoji, post::Post}, PgPool, @@ -12,7 +12,6 @@ use kitsune_embed::Client as EmbedClient; use kitsune_federation_filter::FederationFilter; use kitsune_http_client::Client; use kitsune_type::jsonld::RdfNode; -use kitsune_webfinger::Webfinger; use mime::Mime; use serde::de::DeserializeOwned; use typed_builder::TypedBuilder; @@ -25,7 +24,10 @@ mod emoji; mod object; #[derive(Clone, TypedBuilder)] -pub struct Fetcher { +pub struct Fetcher +where + R: Resolver, +{ #[builder(default = Client::builder() .default_header( @@ -45,14 +47,17 @@ pub struct Fetcher { federation_filter: FederationFilter, #[builder(setter(into))] search_backend: kitsune_search::AnySearchBackend, - resolver: Webfinger, + resolver: R, // Caches post_cache: ArcCache, user_cache: ArcCache, } -impl Fetcher { +impl Fetcher +where + R: Resolver, +{ async fn fetch_ap_resource(&self, url: U) -> Result where U: TryInto, @@ -101,7 +106,10 @@ impl Fetcher { } } -impl FetcherTrait for Fetcher { +impl FetcherTrait for Fetcher +where + R: Resolver, +{ type Error = Error; async fn fetch_account(&self, opts: AccountFetchOptions<'_>) -> Result { diff --git a/crates/kitsune-activitypub/src/fetcher/object.rs b/crates/kitsune-activitypub/src/fetcher/object.rs index 0f69c5679..3244a9496 100644 --- a/crates/kitsune-activitypub/src/fetcher/object.rs +++ b/crates/kitsune-activitypub/src/fetcher/object.rs @@ -5,6 +5,7 @@ use autometrics::autometrics; use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use kitsune_cache::CacheBackend; +use kitsune_core::traits::Resolver; use kitsune_db::{model::post::Post, schema::posts}; use kitsune_type::ap::Object; use scoped_futures::ScopedFutureExt; @@ -13,7 +14,10 @@ use scoped_futures::ScopedFutureExt; // Setting this to >=40 would cause the `fetch_infinitely_long_reply_chain` test to run into stack overflow pub const MAX_FETCH_DEPTH: u32 = 30; -impl Fetcher { +impl Fetcher +where + R: Resolver, +{ #[async_recursion] pub(crate) async fn fetch_object_inner( &self, diff --git a/crates/kitsune-activitypub/src/lib.rs b/crates/kitsune-activitypub/src/lib.rs index a502d5ee6..3b7b61b3b 100644 --- a/crates/kitsune-activitypub/src/lib.rs +++ b/crates/kitsune-activitypub/src/lib.rs @@ -11,6 +11,7 @@ use diesel_async::{AsyncPgConnection, RunQueryDsl}; use futures_util::{future::try_join_all, FutureExt, TryFutureExt}; use http::Uri; use iso8601_timestamp::Timestamp; +use kitsune_core::traits::Resolver; use kitsune_db::{ model::{ account::Account, @@ -39,7 +40,7 @@ pub mod error; pub mod fetcher; pub mod inbox_resolver; -pub use self::{deliverer::Deliverer, fetcher::Fetcher}; +pub use self::{deliverer::Deliverer, fetcher::Fetcher, inbox_resolver::InboxResolver}; async fn handle_mentions( db_conn: &mut AsyncPgConnection, @@ -72,12 +73,15 @@ async fn handle_mentions( Ok(()) } -async fn handle_custom_emojis( +async fn handle_custom_emojis( db_conn: &mut AsyncPgConnection, post_id: Uuid, - fetcher: &Fetcher, + fetcher: &Fetcher, tags: &[Tag], -) -> Result<()> { +) -> Result<()> +where + R: Resolver, +{ let emoji_iter = tags.iter().filter(|tag| tag.r#type == TagType::Emoji); let emoji_count = emoji_iter.clone().count(); @@ -154,7 +158,10 @@ pub async fn process_attachments( } #[derive(TypedBuilder)] -pub struct ProcessNewObject<'a> { +pub struct ProcessNewObject<'a, R> +where + R: Resolver, +{ #[builder(default, setter(into, strip_option))] author: Option<&'a Account>, #[builder(default = 0)] @@ -162,12 +169,15 @@ pub struct ProcessNewObject<'a> { db_pool: &'a PgPool, embed_client: Option<&'a EmbedClient>, object: Box, - fetcher: &'a Fetcher, + fetcher: &'a Fetcher, search_backend: &'a AnySearchBackend, } #[derive(TypedBuilder)] -struct PreprocessedObject<'a> { +struct PreprocessedObject<'a, R> +where + R: Resolver, +{ user: CowBox<'a, Account>, visibility: Visibility, in_reply_to_id: Option, @@ -175,12 +185,12 @@ struct PreprocessedObject<'a> { content_lang: Language, db_pool: &'a PgPool, object: Box, - fetcher: &'a Fetcher, + fetcher: &'a Fetcher, search_backend: &'a AnySearchBackend, } #[allow(clippy::missing_panics_doc)] -async fn preprocess_object( +async fn preprocess_object( ProcessNewObject { author, call_depth, @@ -189,8 +199,11 @@ async fn preprocess_object( mut object, fetcher, search_backend, - }: ProcessNewObject<'_>, -) -> Result> { + }: ProcessNewObject<'_, R>, +) -> Result> +where + R: Resolver, +{ let attributed_to = object.attributed_to().ok_or(Error::InvalidDocument)?; let user = if let Some(author) = author { CowBox::borrowed(author) @@ -253,7 +266,10 @@ async fn preprocess_object( } #[allow(clippy::missing_panics_doc)] -pub async fn process_new_object(process_data: ProcessNewObject<'_>) -> Result { +pub async fn process_new_object(process_data: ProcessNewObject<'_, R>) -> Result +where + R: Resolver, +{ let PreprocessedObject { user, visibility, @@ -327,7 +343,10 @@ pub async fn process_new_object(process_data: ProcessNewObject<'_>) -> Result) -> Result { +pub async fn update_object(process_data: ProcessNewObject<'_, R>) -> Result +where + R: Resolver, +{ let PreprocessedObject { user, visibility, diff --git a/crates/kitsune-core/Cargo.toml b/crates/kitsune-core/Cargo.toml index 112f69c6b..b4513a043 100644 --- a/crates/kitsune-core/Cargo.toml +++ b/crates/kitsune-core/Cargo.toml @@ -18,14 +18,12 @@ kitsune-config = { path = "../kitsune-config" } kitsune-db = { path = "../kitsune-db" } kitsune-email = { path = "../kitsune-email" } kitsune-embed = { path = "../kitsune-embed" } -kitsune-http-client = { path = "../kitsune-http-client" } kitsune-messaging = { path = "../kitsune-messaging" } kitsune-search = { path = "../kitsune-search" } kitsune-storage = { path = "../kitsune-storage" } kitsune-type = { path = "../kitsune-type" } kitsune-util = { path = "../kitsune-util" } mime = "0.3.17" -post-process = { path = "../../lib/post-process" } pulldown-cmark = { version = "0.9.3", default-features = false, features = [ "simd", ] } diff --git a/crates/kitsune-core/src/resolve/mod.rs b/crates/kitsune-core/src/resolve/mod.rs deleted file mode 100644 index 45488eb84..000000000 --- a/crates/kitsune-core/src/resolve/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod post; - -pub use self::post::PostResolver; diff --git a/crates/kitsune-core/src/resolve/post.rs b/crates/kitsune-core/src/resolve/post.rs deleted file mode 100644 index 8b1378917..000000000 --- a/crates/kitsune-core/src/resolve/post.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/crates/kitsune-core/src/traits/fetcher.rs b/crates/kitsune-core/src/traits/fetcher.rs index 8b8b0d3f7..f118a6513 100644 --- a/crates/kitsune-core/src/traits/fetcher.rs +++ b/crates/kitsune-core/src/traits/fetcher.rs @@ -28,7 +28,7 @@ impl<'a> From<&'a str> for AccountFetchOptions<'a> { } } -pub trait Fetcher { +pub trait Fetcher: Send + Sync { type Error: Into; fn fetch_account( diff --git a/crates/kitsune-core/src/traits/resolver.rs b/crates/kitsune-core/src/traits/resolver.rs index b934cf08a..f1a028a8e 100644 --- a/crates/kitsune-core/src/traits/resolver.rs +++ b/crates/kitsune-core/src/traits/resolver.rs @@ -13,7 +13,7 @@ pub struct AccountResource { pub domain: String, } -pub trait Resolver { +pub trait Resolver: Send + Sync { type Error: Into; fn resolve_account( diff --git a/crates/kitsune-jobs/Cargo.toml b/crates/kitsune-jobs/Cargo.toml index 7f557d54c..20b2ec3ac 100644 --- a/crates/kitsune-jobs/Cargo.toml +++ b/crates/kitsune-jobs/Cargo.toml @@ -3,21 +3,19 @@ name = "kitsune-jobs" edition.workspace = true version.workspace = true -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] -athena = { version = "0.0.1-pre.4", path = "../../lib/athena" } +athena = { path = "../../lib/athena" } diesel = "2.1.3" diesel-async = "0.4.1" eyre = "0.6.8" futures-util = "0.3.29" iso8601-timestamp = "0.2.12" -kitsune-db = { version = "0.0.1-pre.4", path = "../kitsune-db" } -kitsune-type = { version = "0.0.1-pre.4", path = "../kitsune-type" } -kitsune-util = { version = "0.0.1-pre.4", path = "../kitsune-util" } +kitsune-db = { path = "../kitsune-db" } +kitsune-type = { path = "../kitsune-type" } +kitsune-util = { path = "../kitsune-util" } scoped-futures = "0.1.3" serde = { version = "1.0.192", features = ["derive"] } -speedy-uuid = { version = "0.0.1-pre.4", path = "../../lib/speedy-uuid" } +speedy-uuid = { path = "../../lib/speedy-uuid" } thiserror = "1.0.50" tracing = "0.1.40" typed-builder = "0.18.0"