From db369683a0c6d69252a428fd0fef20e6e5976a7b Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 20 Mar 2024 11:00:01 +0100 Subject: [PATCH] feat: @context in base, actor fields, impl activity --- src/activitystream/mod.rs | 9 +++- src/activitystream/object/activity/mod.rs | 27 ++++++++---- src/activitystream/object/actor.rs | 51 +++++++++++++++++++---- src/model/activity.rs | 9 ++-- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/activitystream/mod.rs b/src/activitystream/mod.rs index f56a3650..ad0c7043 100644 --- a/src/activitystream/mod.rs +++ b/src/activitystream/mod.rs @@ -30,7 +30,14 @@ pub trait Base { } pub fn object() -> serde_json::Value { - serde_json::Value::Object(serde_json::Map::default()) + let mut map = serde_json::Map::default(); + map.insert( + "@context".to_string(), + serde_json::Value::Array(vec![ + serde_json::Value::String("https://www.w3.org/ns/activitystreams".into()) + ]), + ); + serde_json::Value::Object(map) } pub trait BaseMut { diff --git a/src/activitystream/object/activity/mod.rs b/src/activitystream/object/activity/mod.rs index 7d5db04a..24000996 100644 --- a/src/activitystream/object/activity/mod.rs +++ b/src/activitystream/object/activity/mod.rs @@ -5,7 +5,7 @@ pub mod offer; pub mod reject; use crate::activitystream::Node; -use crate::strenum; +use crate::{getter, setter, strenum}; use accept::AcceptType; use reject::RejectType; use offer::OfferType; @@ -45,7 +45,7 @@ pub trait Activity : super::Object { fn activity_type(&self) -> Option { None } fn actor(&self) -> Node { Node::Empty:: } fn object(&self) -> Node { Node::Empty:: } - fn target(&self) -> Option<&str> { None } + fn target(&self) -> Node { Node::Empty:: } fn result(&self) -> Node { Node::Empty:: } fn origin(&self) -> Node { Node::Empty:: } fn instrument(&self) -> Node { Node::Empty:: } @@ -55,15 +55,28 @@ pub trait ActivityMut : super::ObjectMut { fn set_activity_type(&mut self, val: Option) -> &mut Self; fn set_actor(&mut self, val: Node) -> &mut Self; fn set_object(&mut self, val: Node) -> &mut Self; - fn set_target(&mut self, val: Option<&str>) -> &mut Self; + fn set_target(&mut self, val: Node) -> &mut Self; fn set_result(&mut self, val: Node) -> &mut Self; fn set_origin(&mut self, val: Node) -> &mut Self; fn set_instrument(&mut self, val: Node) -> &mut Self; } impl Activity for serde_json::Value { - fn activity_type(&self) -> Option { - let serde_json::Value::String(t) = self.get("type")? else { return None }; - ActivityType::try_from(t.as_str()).ok() - } + getter! { activity_type -> type ActivityType } + getter! { actor -> node impl super::Actor } + getter! { object -> node impl super::Object } + getter! { target -> node impl super::Object } + getter! { result -> node impl super::Object } + getter! { origin -> node impl super::Object } + getter! { instrument -> node impl super::Object } +} + +impl ActivityMut for serde_json::Value { + setter! { activity_type -> type ActivityType } + setter! { actor -> node impl super::Actor } + setter! { object -> node impl super::Object } + setter! { target -> node impl super::Object } + setter! { result -> node impl super::Object } + setter! { origin -> node impl super::Object } + setter! { instrument -> node impl super::Object } } diff --git a/src/activitystream/object/actor.rs b/src/activitystream/object/actor.rs index 172da51c..4e493446 100644 --- a/src/activitystream/object/actor.rs +++ b/src/activitystream/object/actor.rs @@ -1,6 +1,6 @@ -use crate::{activitystream::{Base, BaseType}, strenum}; +use crate::{activitystream::Node, getter, setter, strenum}; -use super::ObjectType; +use super::collection::Collection; strenum! { pub enum ActorType { @@ -14,17 +14,54 @@ strenum! { pub trait Actor : super::Object { fn actor_type(&self) -> Option { None } + fn preferred_username(&self) -> Option<&str> { None } + fn inbox(&self) -> Node; + fn outbox(&self) -> Node; + fn following(&self) -> Node { Node::Empty:: } + fn followers(&self) -> Node { Node::Empty:: } + fn liked(&self) -> Node { Node::Empty:: } + fn streams(&self) -> Node { Node::Empty:: } + fn endpoints(&self) -> Option> { None } } pub trait ActorMut : super::ObjectMut { fn set_actor_type(&mut self, val: Option) -> &mut Self; + fn set_preferred_username(&mut self, val: Option<&str>) -> &mut Self; + fn set_inbox(&mut self, val: Node) -> &mut Self; + fn set_outbox(&mut self, val: Node) -> &mut Self; + fn set_following(&mut self, val: Node) -> &mut Self; + fn set_followers(&mut self, val: Node) -> &mut Self; + fn set_liked(&mut self, val: Node) -> &mut Self; + fn set_streams(&mut self, val: Node) -> &mut Self; + fn set_endpoints(&mut self, val: Option>) -> &mut Self; } impl Actor for serde_json::Value { - fn actor_type(&self) -> Option { - match self.base_type()? { - BaseType::Object(ObjectType::Actor(x)) => Some(x), - _ => None, - } + getter! { actor_type -> type ActorType } + getter! { preferred_username::preferredUsername -> &str } + getter! { inbox -> node impl Collection } + getter! { outbox -> node impl Collection } + getter! { following -> node impl Collection } + getter! { followers -> node impl Collection } + getter! { liked -> node impl Collection } + getter! { streams -> node impl Collection } + + fn endpoints(&self) -> Option> { + todo!() + } +} + +impl ActorMut for serde_json::Value { + setter! { actor_type -> type ActorType } + setter! { preferred_username::preferredUsername -> &str } + setter! { inbox -> node impl Collection } + setter! { outbox -> node impl Collection } + setter! { following -> node impl Collection } + setter! { followers -> node impl Collection } + setter! { liked -> node impl Collection } + setter! { streams -> node impl Collection } + + fn set_endpoints(&mut self, val: Option>) -> &mut Self { + todo!() } } diff --git a/src/model/activity.rs b/src/model/activity.rs index 25de7b21..90243be0 100644 --- a/src/model/activity.rs +++ b/src/model/activity.rs @@ -66,8 +66,11 @@ impl Activity for Model { } } - fn target(&self) -> Option<&str> { - self.target.as_deref() + fn target(&self) -> Node { + match &self.target { + None => Node::Empty::, + Some(x) => Node::Link(Box::new(x.clone())), + } } } @@ -78,7 +81,7 @@ impl Model { activity_type: activity.activity_type().ok_or(super::FieldError("type"))?, actor: activity.actor().id().ok_or(super::FieldError("actor"))?.to_string(), object: activity.object().id().map(|x| x.to_string()), - target: activity.target().map(|x| x.to_string()), + target: activity.target().id().map(|x| x.to_string()), published: activity.published().ok_or(super::FieldError("published"))?, }) }