Skip to content

Commit

Permalink
remove kitsune-activitypub webfinger dependence
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 13, 2023
1 parent 7e26889 commit d79d1f4
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 44 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/kitsune-activitypub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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"] }
Expand Down
7 changes: 4 additions & 3 deletions crates/kitsune-activitypub/src/error.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,6 +51,9 @@ pub enum Error {
#[error("Missing host")]
MissingHost,

#[error(transparent)]
Resolver(BoxError),

#[error(transparent)]
Search(#[from] kitsune_search::Error),

Expand All @@ -58,9 +62,6 @@ pub enum Error {

#[error(transparent)]
UrlParse(#[from] url::ParseError),

#[error(transparent)]
Webfinger(#[from] kitsune_webfinger::error::Error),
}

impl From<Infallible> for Error {
Expand Down
8 changes: 6 additions & 2 deletions crates/kitsune-activitypub/src/fetcher/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use kitsune_util::{convert::timestamp_to_uuid, sanitize::CleanHtmlExt};
use scoped_futures::ScopedFutureExt;
use url::Url;

impl Fetcher {
impl<R> Fetcher<R>
where
R: Resolver,
{
/// Fetch an ActivityPub actor
///
/// # Panics
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion crates/kitsune-activitypub/src/fetcher/emoji.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +16,10 @@ use scoped_futures::ScopedFutureExt;
use speedy_uuid::Uuid;
use url::Url;

impl Fetcher {
impl<R> Fetcher<R>
where
R: Resolver,
{
pub(crate) async fn fetch_emoji(&self, url: &str) -> Result<CustomEmoji> {
let existing_emoji = self
.db_pool
Expand Down
20 changes: 14 additions & 6 deletions crates/kitsune-activitypub/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -25,7 +24,10 @@ mod emoji;
mod object;

#[derive(Clone, TypedBuilder)]
pub struct Fetcher {
pub struct Fetcher<R>
where
R: Resolver,
{
#[builder(default =
Client::builder()
.default_header(
Expand All @@ -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<str, Post>,
user_cache: ArcCache<str, Account>,
}

impl Fetcher {
impl<R> Fetcher<R>
where
R: Resolver,
{
async fn fetch_ap_resource<U, T>(&self, url: U) -> Result<T>
where
U: TryInto<Url>,
Expand Down Expand Up @@ -101,7 +106,10 @@ impl Fetcher {
}
}

impl FetcherTrait for Fetcher {
impl<R> FetcherTrait for Fetcher<R>
where
R: Resolver,
{
type Error = Error;

async fn fetch_account(&self, opts: AccountFetchOptions<'_>) -> Result<Account, Self::Error> {
Expand Down
6 changes: 5 additions & 1 deletion crates/kitsune-activitypub/src/fetcher/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<R> Fetcher<R>
where
R: Resolver,
{
#[async_recursion]
pub(crate) async fn fetch_object_inner(
&self,
Expand Down
45 changes: 32 additions & 13 deletions crates/kitsune-activitypub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -72,12 +73,15 @@ async fn handle_mentions(
Ok(())
}

async fn handle_custom_emojis(
async fn handle_custom_emojis<R>(
db_conn: &mut AsyncPgConnection,
post_id: Uuid,
fetcher: &Fetcher,
fetcher: &Fetcher<R>,
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();
Expand Down Expand Up @@ -154,33 +158,39 @@ 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)]
call_depth: u32,
db_pool: &'a PgPool,
embed_client: Option<&'a EmbedClient>,
object: Box<Object>,
fetcher: &'a Fetcher,
fetcher: &'a Fetcher<R>,
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<Uuid>,
link_preview_url: Option<String>,
content_lang: Language,
db_pool: &'a PgPool,
object: Box<Object>,
fetcher: &'a Fetcher,
fetcher: &'a Fetcher<R>,
search_backend: &'a AnySearchBackend,
}

#[allow(clippy::missing_panics_doc)]
async fn preprocess_object(
async fn preprocess_object<R>(
ProcessNewObject {
author,
call_depth,
Expand All @@ -189,8 +199,11 @@ async fn preprocess_object(
mut object,
fetcher,
search_backend,
}: ProcessNewObject<'_>,
) -> Result<PreprocessedObject<'_>> {
}: ProcessNewObject<'_, R>,
) -> Result<PreprocessedObject<'_, R>>
where
R: Resolver,
{
let attributed_to = object.attributed_to().ok_or(Error::InvalidDocument)?;
let user = if let Some(author) = author {
CowBox::borrowed(author)
Expand Down Expand Up @@ -253,7 +266,10 @@ async fn preprocess_object(
}

#[allow(clippy::missing_panics_doc)]
pub async fn process_new_object(process_data: ProcessNewObject<'_>) -> Result<Post> {
pub async fn process_new_object<R>(process_data: ProcessNewObject<'_, R>) -> Result<Post>
where
R: Resolver,
{
let PreprocessedObject {
user,
visibility,
Expand Down Expand Up @@ -327,7 +343,10 @@ pub async fn process_new_object(process_data: ProcessNewObject<'_>) -> Result<Po
}

#[allow(clippy::missing_panics_doc)]
pub async fn update_object(process_data: ProcessNewObject<'_>) -> Result<Post> {
pub async fn update_object<R>(process_data: ProcessNewObject<'_, R>) -> Result<Post>
where
R: Resolver,
{
let PreprocessedObject {
user,
visibility,
Expand Down
2 changes: 0 additions & 2 deletions crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand Down
3 changes: 0 additions & 3 deletions crates/kitsune-core/src/resolve/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/kitsune-core/src/resolve/post.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/kitsune-core/src/traits/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a> From<&'a str> for AccountFetchOptions<'a> {
}
}

pub trait Fetcher {
pub trait Fetcher: Send + Sync {
type Error: Into<BoxError>;

fn fetch_account(
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-core/src/traits/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct AccountResource {
pub domain: String,
}

pub trait Resolver {
pub trait Resolver: Send + Sync {
type Error: Into<BoxError>;

fn resolve_account(
Expand Down
12 changes: 5 additions & 7 deletions crates/kitsune-jobs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit d79d1f4

Please sign in to comment.