diff --git a/apb/Cargo.toml b/apb/Cargo.toml index 2ca8916e..be8270af 100644 --- a/apb/Cargo.toml +++ b/apb/Cargo.toml @@ -18,6 +18,7 @@ chrono = { version = "0.4", features = ["serde"] } thiserror = "1" paste = "1.0" tracing = "0.1" +async-trait = "0.1" serde_json = { version = "1", optional = true } sea-orm = { version = "0.12", optional = true } reqwest = { version = "0.12", features = ["json"], optional = true } diff --git a/apb/src/lib.rs b/apb/src/lib.rs index 3f726545..f7c0bdce 100644 --- a/apb/src/lib.rs +++ b/apb/src/lib.rs @@ -93,34 +93,35 @@ pub(crate) use macros::{strenum, getter, setter}; mod node; pub use node::Node; -mod link; -pub use link::{Link, LinkMut, LinkType}; +pub mod server; +pub mod target; mod key; pub use key::{PublicKey, PublicKeyMut}; -mod base; -pub use base::{Base, BaseMut, BaseType}; - -mod object; -pub use object::{ - Object, ObjectMut, ObjectType, - activity::{ - Activity, ActivityMut, ActivityType, - accept::{Accept, AcceptMut, AcceptType}, - ignore::{Ignore, IgnoreMut, IgnoreType}, - intransitive::{IntransitiveActivity, IntransitiveActivityMut, IntransitiveActivityType}, - offer::{Offer, OfferMut, OfferType}, - reject::{Reject, RejectMut, RejectType}, +mod types; +pub use types::{ + base::{Base, BaseMut, BaseType}, + link::{Link, LinkMut, LinkType}, + object::{ + Object, ObjectMut, ObjectType, + activity::{ + Activity, ActivityMut, ActivityType, + accept::{Accept, AcceptMut, AcceptType}, + ignore::{Ignore, IgnoreMut, IgnoreType}, + intransitive::{IntransitiveActivity, IntransitiveActivityMut, IntransitiveActivityType}, + offer::{Offer, OfferMut, OfferType}, + reject::{Reject, RejectMut, RejectType}, + }, + actor::{Actor, ActorMut, ActorType}, + collection::{ + Collection, CollectionMut, CollectionType, + page::{CollectionPage, CollectionPageMut} + }, + document::{Document, DocumentMut, DocumentType}, + place::{Place, PlaceMut}, + // profile::Profile, + relationship::{Relationship, RelationshipMut}, + tombstone::{Tombstone, TombstoneMut}, }, - actor::{Actor, ActorMut, ActorType}, - collection::{ - Collection, CollectionMut, CollectionType, - page::{CollectionPage, CollectionPageMut} - }, - document::{Document, DocumentMut, DocumentType}, - place::{Place, PlaceMut}, - // profile::Profile, - relationship::{Relationship, RelationshipMut}, - tombstone::{Tombstone, TombstoneMut}, }; diff --git a/apb/src/server.rs b/apb/src/server.rs new file mode 100644 index 00000000..322e9415 --- /dev/null +++ b/apb/src/server.rs @@ -0,0 +1,29 @@ +#[async_trait::async_trait] +pub trait Outbox { + type Object: crate::Object; + type Activity: crate::Activity; + type Error: std::error::Error; + + async fn create_note(&self, uid: String, object: Self::Object) -> Result; + async fn create(&self, uid: String, activity: Self::Activity) -> Result; + async fn like(&self, uid: String, activity: Self::Activity) -> Result; + async fn follow(&self, uid: String, activity: Self::Activity) -> Result; + async fn accept(&self, uid: String, activity: Self::Activity) -> Result; + async fn reject(&self, _uid: String, _activity: Self::Activity) -> Result; + async fn undo(&self, uid: String, activity: Self::Activity) -> Result; +} + +#[async_trait::async_trait] +pub trait Inbox { + type Activity: crate::Activity; + type Error: std::error::Error; + + async fn create(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn like(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn follow(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn accept(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn reject(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn undo(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn delete(&self, activity: Self::Activity) -> Result<(), Self::Error>; + async fn update(&self, activity: Self::Activity) -> Result<(), Self::Error>; +} diff --git a/apb/src/target.rs b/apb/src/target.rs new file mode 100644 index 00000000..2a8073dd --- /dev/null +++ b/apb/src/target.rs @@ -0,0 +1,18 @@ +use crate::{Object, Link}; + +pub const PUBLIC : &str = "https://www.w3.org/ns/activitystreams#Public"; + +pub trait Addressed : Object { + fn addressed(&self) -> Vec; +} + +#[cfg(feature = "unstructured")] +impl Addressed for serde_json::Value { + fn addressed(&self) -> Vec { + let mut to : Vec = self.to().map(|x| x.href().to_string()).collect(); + to.append(&mut self.bto().map(|x| x.href().to_string()).collect()); + to.append(&mut self.cc().map(|x| x.href().to_string()).collect()); + to.append(&mut self.bcc().map(|x| x.href().to_string()).collect()); + to + } +} diff --git a/apb/src/base.rs b/apb/src/types/base.rs similarity index 100% rename from apb/src/base.rs rename to apb/src/types/base.rs diff --git a/apb/src/link.rs b/apb/src/types/link.rs similarity index 97% rename from apb/src/link.rs rename to apb/src/types/link.rs index 6991e155..31de5d79 100644 --- a/apb/src/link.rs +++ b/apb/src/types/link.rs @@ -5,7 +5,7 @@ crate::strenum! { }; } -pub trait Link : super::Base { +pub trait Link : crate::Base { fn href(&self) -> &str; fn rel(&self) -> Option<&str> { None } fn link_media_type(&self) -> Option<&str> { None } // also in obj @@ -16,7 +16,7 @@ pub trait Link : super::Base { fn link_preview(&self) -> Option<&str> { None } // also in obj } -pub trait LinkMut : super::BaseMut { +pub trait LinkMut : crate::BaseMut { fn set_href(self, href: &str) -> Self; fn set_rel(self, val: Option<&str>) -> Self; fn set_link_media_type(self, val: Option<&str>) -> Self; // also in obj diff --git a/apb/src/types/mod.rs b/apb/src/types/mod.rs new file mode 100644 index 00000000..ca6f1169 --- /dev/null +++ b/apb/src/types/mod.rs @@ -0,0 +1,3 @@ +pub mod base; +pub mod link; +pub mod object; diff --git a/apb/src/object/activity/accept.rs b/apb/src/types/object/activity/accept.rs similarity index 100% rename from apb/src/object/activity/accept.rs rename to apb/src/types/object/activity/accept.rs diff --git a/apb/src/object/activity/ignore.rs b/apb/src/types/object/activity/ignore.rs similarity index 100% rename from apb/src/object/activity/ignore.rs rename to apb/src/types/object/activity/ignore.rs diff --git a/apb/src/object/activity/intransitive.rs b/apb/src/types/object/activity/intransitive.rs similarity index 100% rename from apb/src/object/activity/intransitive.rs rename to apb/src/types/object/activity/intransitive.rs diff --git a/apb/src/object/activity/mod.rs b/apb/src/types/object/activity/mod.rs similarity index 98% rename from apb/src/object/activity/mod.rs rename to apb/src/types/object/activity/mod.rs index 1386178c..7b24bbe5 100644 --- a/apb/src/object/activity/mod.rs +++ b/apb/src/types/object/activity/mod.rs @@ -4,7 +4,7 @@ pub mod intransitive; pub mod offer; pub mod reject; -use crate::{Node, object::{Object, ObjectMut}}; +use crate::{Node, Object, ObjectMut}; use accept::AcceptType; use reject::RejectType; use offer::OfferType; diff --git a/apb/src/object/activity/offer.rs b/apb/src/types/object/activity/offer.rs similarity index 100% rename from apb/src/object/activity/offer.rs rename to apb/src/types/object/activity/offer.rs diff --git a/apb/src/object/activity/reject.rs b/apb/src/types/object/activity/reject.rs similarity index 100% rename from apb/src/object/activity/reject.rs rename to apb/src/types/object/activity/reject.rs diff --git a/apb/src/object/actor.rs b/apb/src/types/object/actor.rs similarity index 98% rename from apb/src/object/actor.rs rename to apb/src/types/object/actor.rs index 5a931c07..a5a529b5 100644 --- a/apb/src/object/actor.rs +++ b/apb/src/types/object/actor.rs @@ -1,6 +1,6 @@ use crate::Node; -use super::{Object, ObjectMut, super::key::PublicKey}; +use crate::{Object, ObjectMut, PublicKey}; crate::strenum! { pub enum ActorType { diff --git a/apb/src/object/collection/mod.rs b/apb/src/types/object/collection/mod.rs similarity index 97% rename from apb/src/object/collection/mod.rs rename to apb/src/types/object/collection/mod.rs index 06abac7f..92d961f0 100644 --- a/apb/src/object/collection/mod.rs +++ b/apb/src/types/object/collection/mod.rs @@ -1,7 +1,7 @@ pub mod page; pub use page::CollectionPage; -use crate::{Node, Object, object::ObjectMut}; +use crate::{Node, Object, ObjectMut}; crate::strenum! { pub enum CollectionType { diff --git a/apb/src/object/collection/page.rs b/apb/src/types/object/collection/page.rs similarity index 100% rename from apb/src/object/collection/page.rs rename to apb/src/types/object/collection/page.rs diff --git a/apb/src/object/document.rs b/apb/src/types/object/document.rs similarity index 100% rename from apb/src/object/document.rs rename to apb/src/types/object/document.rs diff --git a/apb/src/object/mod.rs b/apb/src/types/object/mod.rs similarity index 99% rename from apb/src/object/mod.rs rename to apb/src/types/object/mod.rs index 739855f2..840d7ab5 100644 --- a/apb/src/object/mod.rs +++ b/apb/src/types/object/mod.rs @@ -7,7 +7,7 @@ pub mod place; pub mod profile; pub mod relationship; -use super::{Base, BaseMut, Link, Node}; +use crate::{Base, BaseMut, Link, Node}; use actor::{Actor, ActorType}; use document::{Document, DocumentType}; diff --git a/apb/src/object/place.rs b/apb/src/types/object/place.rs similarity index 100% rename from apb/src/object/place.rs rename to apb/src/types/object/place.rs diff --git a/apb/src/object/profile.rs b/apb/src/types/object/profile.rs similarity index 100% rename from apb/src/object/profile.rs rename to apb/src/types/object/profile.rs diff --git a/apb/src/object/relationship.rs b/apb/src/types/object/relationship.rs similarity index 100% rename from apb/src/object/relationship.rs rename to apb/src/types/object/relationship.rs diff --git a/apb/src/object/tombstone.rs b/apb/src/types/object/tombstone.rs similarity index 100% rename from apb/src/object/tombstone.rs rename to apb/src/types/object/tombstone.rs