diff --git a/docs/10_fetching_objects_with_unknown_type.md b/docs/10_fetching_objects_with_unknown_type.md index 96392d4..8a15953 100644 --- a/docs/10_fetching_objects_with_unknown_type.md +++ b/docs/10_fetching_objects_with_unknown_type.md @@ -26,7 +26,7 @@ pub enum SearchableObjects { Note(Note) } -#[async_trait::async_trait] + impl Object for SearchableDbObjects { type DataType = DbConnection; type Kind = SearchableObjects; diff --git a/examples/live_federation/objects/person.rs b/examples/live_federation/objects/person.rs index d9439ea..e7e334b 100644 --- a/examples/live_federation/objects/person.rs +++ b/examples/live_federation/objects/person.rs @@ -63,7 +63,6 @@ pub struct Person { public_key: PublicKey, } -#[async_trait::async_trait] impl Object for DbUser { type DataType = DatabaseHandle; type Kind = Person; diff --git a/examples/live_federation/objects/post.rs b/examples/live_federation/objects/post.rs index 1b19fac..31edeb1 100644 --- a/examples/live_federation/objects/post.rs +++ b/examples/live_federation/objects/post.rs @@ -44,7 +44,6 @@ pub struct Mention { pub kind: MentionType, } -#[async_trait::async_trait] impl Object for DbPost { type DataType = DatabaseHandle; type Kind = Note; diff --git a/examples/local_federation/objects/person.rs b/examples/local_federation/objects/person.rs index 0ae402f..57b4ec2 100644 --- a/examples/local_federation/objects/person.rs +++ b/examples/local_federation/objects/person.rs @@ -128,7 +128,6 @@ impl DbUser { } } -#[async_trait::async_trait] impl Object for DbUser { type DataType = DatabaseHandle; type Kind = Person; diff --git a/examples/local_federation/objects/post.rs b/examples/local_federation/objects/post.rs index cbdf8e8..7868e07 100644 --- a/examples/local_federation/objects/post.rs +++ b/examples/local_federation/objects/post.rs @@ -41,7 +41,6 @@ pub struct Note { content: String, } -#[async_trait::async_trait] impl Object for DbPost { type DataType = DatabaseHandle; type Kind = Note; diff --git a/src/fetch/mod.rs b/src/fetch/mod.rs index 424a115..0365b3e 100644 --- a/src/fetch/mod.rs +++ b/src/fetch/mod.rs @@ -109,6 +109,7 @@ async fn fetch_object_http_with_accept( let mut counter = data.request_counter.fetch_add(1, Ordering::SeqCst); // fetch_add returns old value so we need to increment manually here counter += 1; + url.to_string(); if counter > config.http_fetch_limit { return Err(Error::RequestLimit); } diff --git a/src/fetch/object_id.rs b/src/fetch/object_id.rs index 136ec38..7a3b165 100644 --- a/src/fetch/object_id.rs +++ b/src/fetch/object_id.rs @@ -144,7 +144,7 @@ where data: &Data<::DataType>, ) -> Result, ::Error> { let id = self.0.clone(); - Object::read_from_id(*id, data).await + ::read_from_id(*id, data).await } /// Fetch object from origin instance over HTTP, then verify and parse it. diff --git a/src/traits.rs b/src/traits.rs index 7ca20d5..de318ff 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -43,7 +43,7 @@ use url::Url; /// content: String, /// } /// -/// #[async_trait::async_trait] +/// /// impl Object for DbPost { /// type DataType = DbConnection; /// type Kind = Note; @@ -93,7 +93,6 @@ use url::Url; /// } /// /// } -#[async_trait] pub trait Object: Sized + Debug { /// App data type passed to handlers. Must be identical to /// [crate::config::FederationConfigBuilder::app_data] type. @@ -119,23 +118,29 @@ pub trait Object: Sized + Debug { /// Try to read the object with given `id` from local database. /// /// Should return `Ok(None)` if not found. - async fn read_from_id( + fn read_from_id( object_id: Url, data: &Data, - ) -> Result, Self::Error>; + ) -> impl std::future::Future, Self::Error>> + Send; /// Mark remote object as deleted in local database. /// /// Called when a `Delete` activity is received, or if fetch returns a `Tombstone` object. - async fn delete(self, _data: &Data) -> Result<(), Self::Error> { - Ok(()) + fn delete( + self, + _data: &Data, + ) -> impl std::future::Future> + Send { + async { Ok(()) } } /// Convert database type to Activitypub type. /// /// Called when a local object gets fetched by another instance over HTTP, or when an object /// gets sent in an activity. - async fn into_json(self, data: &Data) -> Result; + fn into_json( + self, + data: &Data, + ) -> impl std::future::Future> + Send; /// Verifies that the received object is valid. /// @@ -144,18 +149,21 @@ pub trait Object: Sized + Debug { /// /// It is necessary to use a separate method for this, because it might be used for activities /// like `Delete/Note`, which shouldn't perform any database write for the inner `Note`. - async fn verify( + fn verify( json: &Self::Kind, expected_domain: &Url, data: &Data, - ) -> Result<(), Self::Error>; + ) -> impl std::future::Future> + Send; /// Convert object from ActivityPub type to database type. /// /// Called when an object is received from HTTP fetch or as part of an activity. This method /// should write the received object to database. Note that there is no distinction between /// create and update, so an `upsert` operation should be used. - async fn from_json(json: Self::Kind, data: &Data) -> Result; + fn from_json( + json: Self::Kind, + data: &Data, + ) -> impl std::future::Future> + Send; } /// Handler for receiving incoming activities. @@ -292,7 +300,6 @@ where } /// Trait for federating collections -#[async_trait] pub trait Collection: Sized { /// Actor or object that this collection belongs to type Owner; @@ -305,31 +312,31 @@ pub trait Collection: Sized { type Error; /// Reads local collection from database and returns it as Activitypub JSON. - async fn read_local( + fn read_local( owner: &Self::Owner, data: &Data, - ) -> Result; + ) -> impl std::future::Future> + Send; /// Verifies that the received object is valid. /// /// You should check here that the domain of id matches `expected_domain`. Additionally you /// should perform any application specific checks. - async fn verify( + fn verify( json: &Self::Kind, expected_domain: &Url, data: &Data, - ) -> Result<(), Self::Error>; + ) -> impl std::future::Future> + Send; /// Convert object from ActivityPub type to database type. /// /// Called when an object is received from HTTP fetch or as part of an activity. This method /// should also write the received object to database. Note that there is no distinction /// between create and update, so an `upsert` operation should be used. - async fn from_json( + fn from_json( json: Self::Kind, owner: &Self::Owner, data: &Data, - ) -> Result; + ) -> impl std::future::Future> + Send; } /// Some impls of these traits for use in tests. Dont use this from external crates. @@ -338,7 +345,7 @@ pub trait Collection: Sized { #[doc(hidden)] #[allow(clippy::unwrap_used)] pub mod tests { - use super::{async_trait, ActivityHandler, Actor, Data, Debug, Object, PublicKey, Url}; + use super::{ActivityHandler, Actor, Data, Debug, Object, PublicKey, Url}; use crate::{ error::Error, fetch::object_id::ObjectId, @@ -346,6 +353,7 @@ pub mod tests { protocol::verification::verify_domains_match, }; use activitystreams_kinds::{activity::FollowType, actor::PersonType}; + use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::sync::LazyLock; @@ -402,7 +410,6 @@ pub mod tests { local: false, }); - #[async_trait] impl Object for DbUser { type DataType = DbConnection; type Kind = Person; @@ -506,7 +513,6 @@ pub mod tests { #[derive(Debug, Clone)] pub struct DbPost {} - #[async_trait] impl Object for DbPost { type DataType = DbConnection; type Kind = Note;