feat: add constructors for entities

This commit is contained in:
əlemi 2024-03-16 20:09:06 +01:00
parent 32d6e80820
commit 0adeb667c4
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 49 additions and 2 deletions

View file

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

View file

@ -54,3 +54,16 @@ impl activitystream::Activity for Model {
self.target.as_deref()
}
}
impl Model {
pub fn new(activity: &impl activitystream::Activity) -> Result<Self, super::FieldError> {
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"))?,
})
}
}

View file

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

View file

@ -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<Self, super::FieldError> {
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"))?,
})
}
}

View file

@ -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<Self, super::FieldError> {
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(),
})
}
}