2024-03-16 05:45:58 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
|
2024-03-21 19:15:19 +01:00
|
|
|
use crate::{activitypub::jsonld::LD, activitystream::{object::{ObjectMut, ObjectType}, BaseMut, BaseType, Node}};
|
2024-03-16 05:45:58 +01:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
|
|
#[sea_orm(table_name = "objects")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
|
|
|
/// must be full uri!!! maybe not great?
|
|
|
|
pub id: String,
|
2024-03-19 06:49:21 +01:00
|
|
|
pub object_type: ObjectType,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub attributed_to: Option<String>,
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub summary: Option<String>,
|
|
|
|
pub content: Option<String>,
|
2024-03-21 19:25:37 +01:00
|
|
|
pub context: Option<String>,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub published: ChronoDateTimeUtc,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
2024-03-21 01:09:33 +01:00
|
|
|
pub enum Relation {
|
|
|
|
#[sea_orm(has_many = "super::activity::Entity")]
|
|
|
|
Activity,
|
|
|
|
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::user::Entity",
|
|
|
|
from = "Column::AttributedTo",
|
|
|
|
to = "super::user::Column::Id",
|
|
|
|
)]
|
|
|
|
User,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::activity::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Activity.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::user::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::User.def()
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 05:45:58 +01:00
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
impl crate::activitystream::Base for Model {
|
2024-03-16 05:45:58 +01:00
|
|
|
fn id(&self) -> Option<&str> {
|
|
|
|
Some(&self.id)
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn base_type(&self) -> Option<BaseType> {
|
|
|
|
Some(BaseType::Object(self.object_type))
|
|
|
|
}
|
2024-03-20 05:44:10 +01:00
|
|
|
|
|
|
|
fn underlying_json_object(self) -> serde_json::Value {
|
2024-03-21 19:15:19 +01:00
|
|
|
serde_json::Value::new_object()
|
2024-03-21 01:09:33 +01:00
|
|
|
.set_id(Some(&self.id))
|
|
|
|
.set_object_type(Some(self.object_type))
|
|
|
|
.set_attributed_to(Node::maybe_link(self.attributed_to))
|
|
|
|
.set_name(self.name.as_deref())
|
|
|
|
.set_summary(self.summary.as_deref())
|
|
|
|
.set_content(self.content.as_deref())
|
2024-03-21 19:25:37 +01:00
|
|
|
.set_context(Node::maybe_link(self.context.clone()))
|
2024-03-21 01:09:33 +01:00
|
|
|
.set_published(Some(self.published))
|
2024-03-20 05:44:10 +01:00
|
|
|
}
|
2024-03-19 06:49:21 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
impl crate::activitystream::object::Object for Model {
|
2024-03-19 06:49:21 +01:00
|
|
|
fn object_type(&self) -> Option<ObjectType> {
|
|
|
|
Some(self.object_type)
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 01:09:33 +01:00
|
|
|
fn attributed_to(&self) -> Node<impl crate::activitystream::object::actor::Actor> {
|
2024-03-19 06:49:21 +01:00
|
|
|
Node::<serde_json::Value>::from(self.attributed_to.as_deref())
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn name(&self) -> Option<&str> {
|
2024-03-16 05:45:58 +01:00
|
|
|
self.name.as_deref()
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn summary(&self) -> Option<&str> {
|
2024-03-16 05:45:58 +01:00
|
|
|
self.summary.as_deref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn content(&self) -> Option<&str> {
|
|
|
|
self.content.as_deref()
|
|
|
|
}
|
|
|
|
|
2024-03-21 19:25:37 +01:00
|
|
|
fn context(&self) -> Node<impl crate::activitystream::Object> {
|
|
|
|
Node::maybe_link(self.context.clone())
|
|
|
|
}
|
|
|
|
|
2024-03-16 05:45:58 +01:00
|
|
|
fn published (&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
|
|
|
Some(self.published)
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 20:09:06 +01:00
|
|
|
|
|
|
|
impl Model {
|
2024-03-21 01:09:33 +01:00
|
|
|
pub fn new(object: &impl crate::activitystream::object::Object) -> Result<Self, super::FieldError> {
|
2024-03-16 20:09:06 +01:00
|
|
|
Ok(Model {
|
|
|
|
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
2024-03-19 06:49:21 +01:00
|
|
|
object_type: object.object_type().ok_or(super::FieldError("type"))?,
|
|
|
|
attributed_to: object.attributed_to().id().map(|x| x.to_string()),
|
2024-03-16 20:09:06 +01:00
|
|
|
name: object.name().map(|x| x.to_string()),
|
|
|
|
summary: object.summary().map(|x| x.to_string()),
|
|
|
|
content: object.content().map(|x| x.to_string()),
|
2024-03-21 19:25:37 +01:00
|
|
|
context: object.context().id().map(|x| x.to_string()),
|
2024-03-16 20:09:06 +01:00
|
|
|
published: object.published().ok_or(super::FieldError("published"))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|