diff --git a/Cargo.toml b/Cargo.toml index 6336b331..279d1f6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ sea-orm = { version = "0.12.14", features = ["macros", "sqlx-sqlite", "runtime-t sea-orm-migration = "0.12.15" serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" +thiserror = "1.0.58" tokio = { version = "1.35.1", features = ["full"] } tracing = "0.1.40" tracing-subscriber = "0.3.18" diff --git a/src/model/activity.rs b/src/model/activity.rs index 44f437d3..72207e56 100644 --- a/src/model/activity.rs +++ b/src/model/activity.rs @@ -54,3 +54,16 @@ impl activitystream::Activity for Model { self.target.as_deref() } } + +impl Model { + pub fn new(activity: &impl activitystream::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()), + target: activity.target().map(|x| x.to_string()), + published: activity.published().ok_or(super::FieldError("published"))?, + }) + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 6e072932..4d5cf444 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -2,6 +2,10 @@ pub mod user; pub mod object; pub mod activity; +#[derive(Debug, Clone, thiserror::Error)] +#[error("missing required field: '{0}'")] +pub struct FieldError(&'static str); + pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbErr> { use sea_orm::EntityTrait; diff --git a/src/model/object.rs b/src/model/object.rs index 49abbedd..d6c05a8f 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -1,6 +1,6 @@ use sea_orm::entity::prelude::*; -use crate::activitystream::types::ObjectType; +use crate::activitystream::{self, types::ObjectType}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "objects")] @@ -50,3 +50,20 @@ impl crate::activitystream::Object for Model { Some(self.published) } } + +impl Model { + pub fn new(object: &impl activitystream::Object) -> Result { + let Some(activitystream::Type::ObjectType(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()), + name: object.name().map(|x| x.to_string()), + summary: object.summary().map(|x| x.to_string()), + content: object.content().map(|x| x.to_string()), + published: object.published().ok_or(super::FieldError("published"))?, + }) + } +} diff --git a/src/model/user.rs b/src/model/user.rs index 755fef38..2b8475fe 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -1,4 +1,3 @@ - use sea_orm::entity::prelude::*; use crate::activitystream::{self, types::ActorType}; @@ -33,3 +32,16 @@ impl activitystream::Object for Model { Some(&self.name) } } + +impl Model { + pub fn new(object: &impl activitystream::Object) -> Result { + let Some(activitystream::Type::ActorType(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(), + actor_type: t, + name: object.name().ok_or(super::FieldError("name"))?.to_string(), + }) + } +}