Skip to content

Commit

Permalink
add federation crate
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 25, 2023
1 parent b36e273 commit e79fea0
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 21 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [
"crates/kitsune-db",
"crates/kitsune-email",
"crates/kitsune-embed",
"crates/kitsune-federation",
"crates/kitsune-federation-filter",
"crates/kitsune-http-client",
"crates/kitsune-http-signatures",
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-activitypub/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub enum Error {
}

impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!();
fn from(err: Infallible) -> Self {
match err {}
}
}

Expand Down
2 changes: 0 additions & 2 deletions crates/kitsune-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ extern crate tracing;
pub mod error;
pub mod event;
//pub mod mapping;
//pub mod resolve;
//pub mod state;
pub mod traits;
//pub mod util;

/*
use self::{
Expand Down
6 changes: 5 additions & 1 deletion crates/kitsune-core/src/traits/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use kitsune_db::model::{account::Account, custom_emoji::CustomEmoji, post::Post}
use std::future::Future;
use typed_builder::TypedBuilder;

#[derive(Clone, Debug, TypedBuilder)]
#[derive(Clone, Copy, Debug, TypedBuilder)]
/// Options passed to the fetcher
pub struct AccountFetchOptions<'a> {
/// Prefetched WebFinger `acct` URI
Expand Down Expand Up @@ -43,3 +43,7 @@ pub trait Fetcher: Send + Sync + 'static {

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

// TODO: How can we implement this trait for an array?
// Probably by changing the return type to a `Result<Option>` and then just fatally failing on actual errors
// Would make the most sense.
15 changes: 0 additions & 15 deletions crates/kitsune-core/src/util.rs

This file was deleted.

12 changes: 12 additions & 0 deletions crates/kitsune-federation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "kitsune-federation"
edition.workspace = true
version.workspace = true

[dependencies]
derive_more = { version = "1.0.0-beta.6", features = ["from"] }
futures-util = "0.3.29"
kitsune-activitypub = { path = "../kitsune-activitypub" }
kitsune-core = { path = "../kitsune-core" }
kitsune-db = { path = "../kitsune-db" }
kitsune-webfinger = { path = "../kitsune-webfinger" }
70 changes: 70 additions & 0 deletions crates/kitsune-federation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use derive_more::From;
use futures_util::{future::BoxFuture, FutureExt, TryFutureExt};
use kitsune_activitypub::{Deliverer as ActivityPubDeliverer, Fetcher as ActivityPubFetcher};
use kitsune_core::{
error::BoxError,
traits::{deliverer, fetcher::AccountFetchOptions, Deliverer, Fetcher},
};
use kitsune_db::model::{account::Account, custom_emoji::CustomEmoji, post::Post};
use kitsune_webfinger::Webfinger;
use std::sync::Arc;

pub struct Federator {
pub deliverer: Vec<AnyDeliverer>,
pub fetcher: Vec<AnyFetcher>,
}

#[derive(Clone, From)]
pub enum AnyDeliverer {
ActivityPub(Arc<ActivityPubDeliverer>),
}

impl Deliverer for AnyDeliverer {
type Error = BoxError;

fn deliver(&self, action: deliverer::Action) -> BoxFuture<'_, Result<(), Self::Error>> {
match self {
Self::ActivityPub(deliverer) => deliverer.deliver(action).map_err(Into::into).boxed(),
}
}
}

#[derive(Clone, From)]
pub enum AnyFetcher {
ActivityPub(ActivityPubFetcher<Webfinger>),
}

impl Fetcher for AnyFetcher {
type Error = BoxError;

async fn fetch_account(&self, opts: AccountFetchOptions<'_>) -> Result<Account, Self::Error> {
match self {
Self::ActivityPub(fetcher) => fetcher.fetch_account(opts).await.map_err(Into::into),
}
}

async fn fetch_emoji(&self, url: &str) -> Result<CustomEmoji, Self::Error> {
match self {
Self::ActivityPub(fetcher) => fetcher.fetch_emoji(url).await.map_err(Into::into),
}
}

async fn fetch_post(&self, url: &str) -> Result<Post, Self::Error> {
match self {
Self::ActivityPub(fetcher) => fetcher.fetch_post(url).await.map_err(Into::into),
}
}
}

fn prepare_activitypub() -> (ActivityPubFetcher<Webfinger>, Arc<ActivityPubDeliverer>) {
todo!();
}

pub fn prepare_federator() -> Federator {
let (fetcher, deliverer) = prepare_activitypub();

Federator {
deliverer: vec![deliverer.into()],
fetcher: vec![fetcher.into()],
}
}
1 change: 1 addition & 0 deletions crates/kitsune-service/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ where
.acct((&webfinger_actor.username, &webfinger_actor.domain))
.url(&webfinger_actor.uri)
.build();

self.fetcher
.fetch_account(opts)
.await
Expand Down
6 changes: 5 additions & 1 deletion kitsune-job-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ pub async fn run_dispatcher(
let deliverer = Deliverer::builder()
.federation_filter(state.service.federation_filter.clone())
.build();
let ctx = Arc::new(JobRunnerContext { deliverer, state });

let ctx = Arc::new(JobRunnerContext {
db_pool: state.db_pool,
deliverer,
});

let mut job_joinset = JoinSet::new();
loop {
Expand Down

0 comments on commit e79fea0

Please sign in to comment.