Remove some uses of async_trait

This commit is contained in:
Felix Ableitner 2025-02-17 14:45:32 +01:00
parent 1a94bfa8bb
commit 51bf4b332e
8 changed files with 29 additions and 26 deletions

View file

@ -26,7 +26,7 @@ pub enum SearchableObjects {
Note(Note)
}
#[async_trait::async_trait]
impl Object for SearchableDbObjects {
type DataType = DbConnection;
type Kind = SearchableObjects;

View file

@ -63,7 +63,6 @@ pub struct Person {
public_key: PublicKey,
}
#[async_trait::async_trait]
impl Object for DbUser {
type DataType = DatabaseHandle;
type Kind = Person;

View file

@ -44,7 +44,6 @@ pub struct Mention {
pub kind: MentionType,
}
#[async_trait::async_trait]
impl Object for DbPost {
type DataType = DatabaseHandle;
type Kind = Note;

View file

@ -128,7 +128,6 @@ impl DbUser {
}
}
#[async_trait::async_trait]
impl Object for DbUser {
type DataType = DatabaseHandle;
type Kind = Person;

View file

@ -41,7 +41,6 @@ pub struct Note {
content: String,
}
#[async_trait::async_trait]
impl Object for DbPost {
type DataType = DatabaseHandle;
type Kind = Note;

View file

@ -109,6 +109,7 @@ async fn fetch_object_http_with_accept<T: Clone, Kind: DeserializeOwned>(
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);
}

View file

@ -144,7 +144,7 @@ where
data: &Data<<Kind as Object>::DataType>,
) -> Result<Option<Kind>, <Kind as Object>::Error> {
let id = self.0.clone();
Object::read_from_id(*id, data).await
<Kind as Object>::read_from_id(*id, data).await
}
/// Fetch object from origin instance over HTTP, then verify and parse it.

View file

@ -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<Self::DataType>,
) -> Result<Option<Self>, Self::Error>;
) -> impl std::future::Future<Output = Result<Option<Self>, 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<Self::DataType>) -> Result<(), Self::Error> {
Ok(())
fn delete(
self,
_data: &Data<Self::DataType>,
) -> impl std::future::Future<Output = Result<(), Self::Error>> + 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<Self::DataType>) -> Result<Self::Kind, Self::Error>;
fn into_json(
self,
data: &Data<Self::DataType>,
) -> impl std::future::Future<Output = Result<Self::Kind, Self::Error>> + 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<Self::DataType>,
) -> Result<(), Self::Error>;
) -> impl std::future::Future<Output = Result<(), Self::Error>> + 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<Self::DataType>) -> Result<Self, Self::Error>;
fn from_json(
json: Self::Kind,
data: &Data<Self::DataType>,
) -> impl std::future::Future<Output = Result<Self, Self::Error>> + 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<Self::DataType>,
) -> Result<Self::Kind, Self::Error>;
) -> impl std::future::Future<Output = Result<Self::Kind, Self::Error>> + 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<Self::DataType>,
) -> Result<(), Self::Error>;
) -> impl std::future::Future<Output = Result<(), Self::Error>> + 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<Self::DataType>,
) -> Result<Self, Self::Error>;
) -> impl std::future::Future<Output = Result<Self, Self::Error>> + 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;