Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 12, 2023
1 parent 86455b7 commit 0233cd7
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 106 deletions.
2 changes: 1 addition & 1 deletion crates/kitsune-activitypub/src/fetcher/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use autometrics::autometrics;
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use kitsune_cache::CacheBackend;
use kitsune_core::traits::{AccountFetchOptions, Resolver};
use kitsune_core::traits::{fetcher::AccountFetchOptions, Resolver};
use kitsune_db::{
model::account::{Account, AccountConflictChangeset, NewAccount, UpdateAccountMedia},
schema::accounts,
Expand Down
2 changes: 1 addition & 1 deletion 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::{AccountFetchOptions, Fetcher as FetcherTrait};
use kitsune_core::traits::{fetcher::AccountFetchOptions, Fetcher as FetcherTrait};
use kitsune_db::{
model::{account::Account, custom_emoji::CustomEmoji, post::Post},
PgPool,
Expand Down
103 changes: 0 additions & 103 deletions crates/kitsune-core/src/traits.rs

This file was deleted.

40 changes: 40 additions & 0 deletions crates/kitsune-core/src/traits/deliverer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::error::BoxError;
use kitsune_db::model::{account::Account, post::Post};
use serde::{Deserialize, Serialize};
use std::future::Future;

#[derive(Clone, Deserialize, Serialize)]
pub enum Action {
Create(Post),
Delete(Post),
Favourite(Post),
Repost(Post),
Unfavourite(Post),
Unrepost(Post),
UpdateAccount(Account),
UpdatePost(Post),
}

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

fn deliver(&self, action: Action) -> impl Future<Output = Result<(), Self::Error>> + Send;
}

impl<T> Deliverer for [T]
where
T: Deliverer,
{
type Error = BoxError;

async fn deliver(&self, action: Action) -> Result<(), Self::Error> {
for deliverer in self {
deliverer
.deliver(action.clone())
.await
.map_err(Into::into)?;
}

Ok(())
}
}
45 changes: 45 additions & 0 deletions crates/kitsune-core/src/traits/fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::error::BoxError;
use kitsune_db::model::{account::Account, custom_emoji::CustomEmoji, post::Post};
use std::future::Future;
use typed_builder::TypedBuilder;

#[derive(Clone, Debug, TypedBuilder)]
/// Options passed to the fetcher
pub struct AccountFetchOptions<'a> {
/// Prefetched WebFinger `acct` URI
#[builder(default, setter(strip_option))]
pub acct: Option<(&'a str, &'a str)>,

/// Refetch the ActivityPub entity
///
/// This is mainly used to refresh possibly stale actors
///
/// Default: false
#[builder(default = false)]
pub refetch: bool,

/// URL of the ActivityPub entity
pub url: &'a str,
}

impl<'a> From<&'a str> for AccountFetchOptions<'a> {
fn from(value: &'a str) -> Self {
Self::builder().url(value).build()
}
}

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

fn fetch_account(
&self,
opts: AccountFetchOptions<'_>,
) -> impl Future<Output = Result<Account, Self::Error>> + Send;

fn fetch_emoji(
&self,
url: &str,
) -> impl Future<Output = Result<CustomEmoji, Self::Error>> + Send;

fn fetch_post(&self, url: &str) -> impl Future<Output = Result<Post, Self::Error>> + Send;
}
5 changes: 5 additions & 0 deletions crates/kitsune-core/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod deliverer;
pub mod fetcher;
pub mod resolver;

pub use self::{deliverer::Deliverer, fetcher::Fetcher, resolver::Resolver};
24 changes: 24 additions & 0 deletions crates/kitsune-core/src/traits/resolver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::error::BoxError;
use serde::{Deserialize, Serialize};
use std::future::Future;

/// Description of a resolved account
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct AccountResource {
/// The `self` link (the account's URI)
pub uri: String,
/// The username part of the canonical `acct:` URI
pub username: String,
/// The host component of the canonical `acct:` URI
pub domain: String,
}

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

fn resolve_account(
&self,
username: &str,
domain: &str,
) -> impl Future<Output = Result<Option<AccountResource>, Self::Error>> + Send;
}
2 changes: 1 addition & 1 deletion crates/kitsune-webfinger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures_util::future::{FutureExt, OptionFuture};
use http::{HeaderValue, StatusCode};
use kitsune_cache::{ArcCache, CacheBackend, RedisCache};
use kitsune_consts::USER_AGENT;
use kitsune_core::traits::{AccountResource, Resolver};
use kitsune_core::traits::{resolver::AccountResource, Resolver};
use kitsune_http_client::Client;
use kitsune_type::webfinger::Resource;
use kitsune_util::try_join;
Expand Down

0 comments on commit 0233cd7

Please sign in to comment.