forked from alemi/upub
feat: add constructors for entities
This commit is contained in:
parent
32d6e80820
commit
0adeb667c4
5 changed files with 49 additions and 2 deletions
|
@ -13,6 +13,7 @@ sea-orm = { version = "0.12.14", features = ["macros", "sqlx-sqlite", "runtime-t
|
||||||
sea-orm-migration = "0.12.15"
|
sea-orm-migration = "0.12.15"
|
||||||
serde = { version = "1.0.193", features = ["derive"] }
|
serde = { version = "1.0.193", features = ["derive"] }
|
||||||
serde_json = "1.0.108"
|
serde_json = "1.0.108"
|
||||||
|
thiserror = "1.0.58"
|
||||||
tokio = { version = "1.35.1", features = ["full"] }
|
tokio = { version = "1.35.1", features = ["full"] }
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
tracing-subscriber = "0.3.18"
|
tracing-subscriber = "0.3.18"
|
||||||
|
|
|
@ -54,3 +54,16 @@ impl activitystream::Activity for Model {
|
||||||
self.target.as_deref()
|
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"))?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,10 @@ pub mod user;
|
||||||
pub mod object;
|
pub mod object;
|
||||||
pub mod activity;
|
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> {
|
pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbErr> {
|
||||||
use sea_orm::EntityTrait;
|
use sea_orm::EntityTrait;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use crate::activitystream::types::ObjectType;
|
use crate::activitystream::{self, types::ObjectType};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "objects")]
|
#[sea_orm(table_name = "objects")]
|
||||||
|
@ -50,3 +50,20 @@ impl crate::activitystream::Object for Model {
|
||||||
Some(self.published)
|
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"))?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use crate::activitystream::{self, types::ActorType};
|
use crate::activitystream::{self, types::ActorType};
|
||||||
|
@ -33,3 +32,16 @@ impl activitystream::Object for Model {
|
||||||
Some(&self.name)
|
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(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue