From 3a53c6a2ed16f5b9620aa4635c9a1deb9e9f2fb7 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 19 Mar 2024 06:49:21 +0100 Subject: [PATCH] feat: added back ToJson trait for db entities --- src/model/activity.rs | 44 +++++++++++++++++++++++++++++------------- src/model/mod.rs | 10 +++++++--- src/model/object.rs | 45 +++++++++++++++++++++++++++++-------------- src/model/user.rs | 33 ++++++++++++++++++++++--------- src/server.rs | 14 ++++++-------- 5 files changed, 99 insertions(+), 47 deletions(-) diff --git a/src/model/activity.rs b/src/model/activity.rs index d7753d4e..047ce4ab 100644 --- a/src/model/activity.rs +++ b/src/model/activity.rs @@ -1,6 +1,6 @@ use sea_orm::entity::prelude::*; -use crate::activitystream::{self, ObjectType, BaseType}; +use crate::activitystream::{node::{InsertStr, Node}, object::{activity::{Activity, ActivityType}, actor::Actor, Object, ObjectType}, Base, BaseType}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "activities")] @@ -9,7 +9,7 @@ pub struct Model { /// must be https://instance.org/users/:user , even if local! TODO bad design... pub id: String, - pub activity_type: activitystream::types::ActivityType, + pub activity_type: ActivityType, pub actor: String, // TODO relates to USER pub object: Option, // TODO relates to NOTES maybe????? maybe other tables?????? pub target: Option, // TODO relates to USER maybe?? @@ -23,31 +23,36 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} -impl activitystream::Object for Model { +impl Base for Model { fn id(&self) -> Option<&str> { Some(&self.id) } - fn full_type(&self) -> Option { + fn base_type(&self) -> Option { Some(BaseType::Object(ObjectType::Activity(self.activity_type))) } +} +impl Object for Model { fn published(&self) -> Option> { Some(self.published) } } -impl activitystream::Activity for Model { - fn activity_type(&self) -> Option { +impl Activity for Model { + fn activity_type(&self) -> Option { Some(self.activity_type) } - fn actor_id(&self) -> Option<&str> { - Some(&self.actor) + fn actor(&self) -> Node { + Node::::Link(Box::new(self.actor.clone())) } - fn object_id(&self) -> Option<&str> { - self.object.as_deref() + fn object(&self) -> Node { + match &self.object { + None => Node::Empty::, + Some(x) => Node::Link(Box::new(x.clone())), + } } fn target(&self) -> Option<&str> { @@ -56,14 +61,27 @@ impl activitystream::Activity for Model { } impl Model { - pub fn new(activity: &impl activitystream::Activity) -> Result { + pub fn new(activity: &impl Activity) -> Result { Ok(Model { id: activity.id().ok_or(super::FieldError("id"))?.to_string(), 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()), + 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()), published: activity.published().ok_or(super::FieldError("published"))?, }) } } + +impl super::ToJson for Model { + fn json(&self) -> serde_json::Value { + let mut map = serde_json::Map::new(); + map.insert_str("id", Some(&self.id)); + map.insert_str("type", Some(self.activity_type.as_ref())); + map.insert_str("actor", Some(&self.actor)); + map.insert_str("object", self.object.as_deref()); + map.insert_str("target", self.target.as_deref()); + map.insert_timestr("published", Some(self.published)); + serde_json::Value::Object(map) + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index c015798a..3b0d1464 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -2,6 +2,10 @@ pub mod user; pub mod object; pub mod activity; +pub trait ToJson { + fn json(&self) -> serde_json::Value; +} + #[derive(Debug, Clone, thiserror::Error)] #[error("missing required field: '{0}'")] pub struct FieldError(pub &'static str); @@ -12,13 +16,13 @@ pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbEr user::Entity::insert(user::ActiveModel { id: sea_orm::Set("http://localhost:3000/users/root".into()), name: sea_orm::Set("root".into()), - actor_type: sea_orm::Set(super::activitystream::types::ActorType::Person), + actor_type: sea_orm::Set(super::activitystream::object::actor::ActorType::Person), }).exec(db).await?; object::Entity::insert(object::ActiveModel { id: sea_orm::Set("http://localhost:3000/objects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into()), name: sea_orm::Set(None), - object_type: sea_orm::Set(crate::activitystream::types::StatusType::Note), + object_type: sea_orm::Set(crate::activitystream::object::ObjectType::Note), attributed_to: sea_orm::Set(Some("http://localhost:3000/users/root".into())), summary: sea_orm::Set(None), content: sea_orm::Set(Some("Hello world!".into())), @@ -27,7 +31,7 @@ pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbEr activity::Entity::insert(activity::ActiveModel { id: sea_orm::Set("http://localhost:3000/activities/ebac57e1-9828-438c-be34-a44a52de7641".into()), - activity_type: sea_orm::Set(crate::activitystream::types::ActivityType::Create), + activity_type: sea_orm::Set(crate::activitystream::object::activity::ActivityType::Create), actor: sea_orm::Set("http://localhost:3000/users/root".into()), object: sea_orm::Set(Some("http://localhost:3000/obkects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into())), target: sea_orm::Set(None), diff --git a/src/model/object.rs b/src/model/object.rs index cd458896..3ded3d79 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -1,6 +1,6 @@ use sea_orm::entity::prelude::*; -use crate::activitystream::{Object, types::{BaseType, ObjectType, StatusType}}; +use crate::activitystream::{node::InsertStr, object::{Actor, Object, ObjectType}, Base, BaseType, Node}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "objects")] @@ -8,7 +8,7 @@ pub struct Model { #[sea_orm(primary_key)] /// must be full uri!!! maybe not great? pub id: String, - pub object_type: StatusType, + pub object_type: ObjectType, pub attributed_to: Option, pub name: Option, pub summary: Option, @@ -21,24 +21,30 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} -impl crate::activitystream::Object for Model { +impl Base for Model { fn id(&self) -> Option<&str> { Some(&self.id) } - fn full_type(&self) -> Option { - Some(BaseType::Object(ObjectType::Status(self.object_type))) + fn base_type(&self) -> Option { + Some(BaseType::Object(self.object_type)) + } +} + +impl Object for Model { + fn object_type(&self) -> Option { + Some(self.object_type) } - fn attributed_to (&self) -> Option<&str> { - self.attributed_to.as_deref() + fn attributed_to(&self) -> Node { + Node::::from(self.attributed_to.as_deref()) } - fn name (&self) -> Option<&str> { + fn name(&self) -> Option<&str> { self.name.as_deref() } - fn summary (&self) -> Option<&str> { + fn summary(&self) -> Option<&str> { self.summary.as_deref() } @@ -53,13 +59,10 @@ impl crate::activitystream::Object for Model { impl Model { pub fn new(object: &impl Object) -> Result { - let Some(BaseType::Object(ObjectType::Status(t))) = object.full_type() else { - return Err(super::FieldError("type")); // TODO maybe just wrong? better errors! - }; Ok(Model { id: object.id().ok_or(super::FieldError("id"))?.to_string(), - object_type: t, - attributed_to: object.attributed_to().map(|x| x.to_string()), + object_type: object.object_type().ok_or(super::FieldError("type"))?, + attributed_to: object.attributed_to().id().map(|x| x.to_string()), name: object.name().map(|x| x.to_string()), summary: object.summary().map(|x| x.to_string()), content: object.content().map(|x| x.to_string()), @@ -67,3 +70,17 @@ impl Model { }) } } + +impl super::ToJson for Model { + fn json(&self) -> serde_json::Value { + let mut map = serde_json::Map::new(); + map.insert_str("id", Some(&self.id)); + map.insert_str("type", Some(self.object_type.as_ref())); + map.insert_str("attributedTo", self.attributed_to.as_deref()); + map.insert_str("name", self.name.as_deref()); + map.insert_str("summary", self.summary.as_deref()); + map.insert_str("content", self.content.as_deref()); + map.insert_timestr("published", Some(self.published)); + serde_json::Value::Object(map) + } +} diff --git a/src/model/user.rs b/src/model/user.rs index 00a64320..b2059d29 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,6 +1,6 @@ use sea_orm::entity::prelude::*; -use crate::activitystream::{self, types::ActorType}; +use crate::activitystream::{node::InsertStr, object::{Actor, ActorType}, Base, BaseType, Object, ObjectType}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "users")] @@ -19,29 +19,44 @@ pub enum Relation {} impl ActiveModelBehavior for ActiveModel {} -impl activitystream::Object for Model { +impl Base for Model { fn id(&self) -> Option<&str> { Some(&self.id) } - fn full_type(&self) -> Option { - Some(activitystream::BaseType::Object(activitystream::ObjectType::Actor(self.actor_type))) + fn base_type(&self) -> Option { + Some(BaseType::Object(ObjectType::Actor(self.actor_type))) } +} +impl Object for Model { fn name (&self) -> Option<&str> { Some(&self.name) } } +impl Actor for Model { + fn actor_type(&self) -> Option { + Some(self.actor_type) + } +} + impl Model { - pub fn new(object: &impl activitystream::Object) -> Result { - let Some(activitystream::BaseType::Object(activitystream::ObjectType::Actor(t))) = object.full_type() else { - return Err(super::FieldError("type")); // TODO maybe just wrong? better errors! - }; + pub fn new(object: &impl Actor) -> Result { Ok(Model { id: object.id().ok_or(super::FieldError("id"))?.to_string(), - actor_type: t, + actor_type: object.actor_type().ok_or(super::FieldError("type"))?, name: object.name().ok_or(super::FieldError("name"))?.to_string(), }) } } + +impl super::ToJson for Model { + fn json(&self) -> serde_json::Value { + let mut map = serde_json::Map::new(); + map.insert_str("id", Some(&self.id)); + map.insert_str("type", Some(self.actor_type.as_ref())); + map.insert_str("name", Some(&self.name)); + serde_json::Value::Object(map) + } +} diff --git a/src/server.rs b/src/server.rs index 6444541f..e848b144 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,11 +1,9 @@ use std::ops::Deref; use std::sync::Arc; -use crate::activitystream::ObjectType; -use crate::activitystream::ToJson; -use crate::activitystream::Activity; -use crate::activitystream::{types::ActivityType, Object, BaseType, LinkedObject}; -use crate::model::{activity, object, user}; +use crate::activitystream::object::{Activity, ActivityType}; +use crate::activitystream::{Base, BaseType, Node, ObjectType}; +use crate::model::{activity, object, user, ToJson}; use axum::{extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router}; use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel}; @@ -28,7 +26,7 @@ pub async fn serve(db: DatabaseConnection) { } async fn inbox(State(db) : State>, Json(object): Json) -> Result, StatusCode> { - match object.full_type() { + match object.base_type() { None => { Err(StatusCode::BAD_REQUEST) }, Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY), @@ -38,8 +36,8 @@ async fn inbox(State(db) : State>, Json(object): Json