feat: @context in base, actor fields, impl activity

This commit is contained in:
əlemi 2024-03-20 11:00:01 +01:00
parent 042edaf29e
commit db369683a0
Signed by: alemi
GPG key ID: A4895B84D311642C
4 changed files with 78 additions and 18 deletions

View file

@ -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 {

View file

@ -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<ActivityType> { None }
fn actor(&self) -> Node<impl super::Actor> { Node::Empty::<serde_json::Value> }
fn object(&self) -> Node<impl super::Object> { Node::Empty::<serde_json::Value> }
fn target(&self) -> Option<&str> { None }
fn target(&self) -> Node<impl super::Object> { Node::Empty::<serde_json::Value> }
fn result(&self) -> Node<impl super::Object> { Node::Empty::<serde_json::Value> }
fn origin(&self) -> Node<impl super::Object> { Node::Empty::<serde_json::Value> }
fn instrument(&self) -> Node<impl super::Object> { Node::Empty::<serde_json::Value> }
@ -55,15 +55,28 @@ pub trait ActivityMut : super::ObjectMut {
fn set_activity_type(&mut self, val: Option<ActivityType>) -> &mut Self;
fn set_actor(&mut self, val: Node<impl super::Actor>) -> &mut Self;
fn set_object(&mut self, val: Node<impl super::Object>) -> &mut Self;
fn set_target(&mut self, val: Option<&str>) -> &mut Self;
fn set_target(&mut self, val: Node<impl super::Object>) -> &mut Self;
fn set_result(&mut self, val: Node<impl super::Object>) -> &mut Self;
fn set_origin(&mut self, val: Node<impl super::Object>) -> &mut Self;
fn set_instrument(&mut self, val: Node<impl super::Object>) -> &mut Self;
}
impl Activity for serde_json::Value {
fn activity_type(&self) -> Option<ActivityType> {
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 }
}

View file

@ -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<ActorType> { None }
fn preferred_username(&self) -> Option<&str> { None }
fn inbox(&self) -> Node<impl Collection>;
fn outbox(&self) -> Node<impl Collection>;
fn following(&self) -> Node<impl Collection> { Node::Empty::<serde_json::Value> }
fn followers(&self) -> Node<impl Collection> { Node::Empty::<serde_json::Value> }
fn liked(&self) -> Node<impl Collection> { Node::Empty::<serde_json::Value> }
fn streams(&self) -> Node<impl Collection> { Node::Empty::<serde_json::Value> }
fn endpoints(&self) -> Option<serde_json::Map<String, String>> { None }
}
pub trait ActorMut : super::ObjectMut {
fn set_actor_type(&mut self, val: Option<ActorType>) -> &mut Self;
fn set_preferred_username(&mut self, val: Option<&str>) -> &mut Self;
fn set_inbox(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_outbox(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_following(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_followers(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_liked(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_streams(&mut self, val: Node<impl Collection>) -> &mut Self;
fn set_endpoints(&mut self, val: Option<serde_json::Map<String, String>>) -> &mut Self;
}
impl Actor for serde_json::Value {
fn actor_type(&self) -> Option<ActorType> {
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<serde_json::Map<String, String>> {
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<serde_json::Map<String, String>>) -> &mut Self {
todo!()
}
}

View file

@ -66,8 +66,11 @@ impl Activity for Model {
}
}
fn target(&self) -> Option<&str> {
self.target.as_deref()
fn target(&self) -> Node<impl Object> {
match &self.target {
None => Node::Empty::<serde_json::Value>,
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"))?,
})
}