2024-03-16 05:45:58 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
|
2024-03-20 05:44:10 +01:00
|
|
|
use crate::activitystream::{Node, macros::InsertValue, object::{activity::{Activity, ActivityType}, actor::Actor, Object, ObjectType}, Base, BaseType};
|
2024-03-16 05:45:58 +01:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
|
|
#[sea_orm(table_name = "activities")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
|
|
|
/// must be https://instance.org/users/:user , even if local! TODO bad design...
|
|
|
|
pub id: String,
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
pub activity_type: ActivityType,
|
2024-03-16 05:45:58 +01:00
|
|
|
pub actor: String, // TODO relates to USER
|
|
|
|
pub object: Option<String>, // TODO relates to NOTES maybe????? maybe other tables??????
|
|
|
|
pub target: Option<String>, // TODO relates to USER maybe??
|
|
|
|
pub published: ChronoDateTimeUtc,
|
|
|
|
|
|
|
|
// TODO: origin, result, instrument
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
|
|
pub enum Relation {}
|
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
impl 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> {
|
2024-03-19 01:00:44 +01:00
|
|
|
Some(BaseType::Object(ObjectType::Activity(self.activity_type)))
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
2024-03-20 05:44:10 +01:00
|
|
|
|
|
|
|
fn underlying_json_object(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)
|
|
|
|
}
|
2024-03-19 06:49:21 +01:00
|
|
|
}
|
2024-03-16 05:45:58 +01:00
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
impl Object for Model {
|
2024-03-16 05:45:58 +01:00
|
|
|
fn published(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
|
|
|
Some(self.published)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
impl Activity for Model {
|
|
|
|
fn activity_type(&self) -> Option<ActivityType> {
|
2024-03-16 05:45:58 +01:00
|
|
|
Some(self.activity_type)
|
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn actor(&self) -> Node<impl Actor> {
|
|
|
|
Node::<serde_json::Value>::Link(Box::new(self.actor.clone()))
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 06:49:21 +01:00
|
|
|
fn object(&self) -> Node<impl Object> {
|
|
|
|
match &self.object {
|
|
|
|
None => Node::Empty::<serde_json::Value>,
|
|
|
|
Some(x) => Node::Link(Box::new(x.clone())),
|
|
|
|
}
|
2024-03-16 05:45:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn target(&self) -> Option<&str> {
|
|
|
|
self.target.as_deref()
|
|
|
|
}
|
|
|
|
}
|
2024-03-16 20:09:06 +01:00
|
|
|
|
|
|
|
impl Model {
|
2024-03-19 06:49:21 +01:00
|
|
|
pub fn new(activity: &impl Activity) -> Result<Self, super::FieldError> {
|
2024-03-16 20:09:06 +01:00
|
|
|
Ok(Model {
|
|
|
|
id: activity.id().ok_or(super::FieldError("id"))?.to_string(),
|
|
|
|
activity_type: activity.activity_type().ok_or(super::FieldError("type"))?,
|
2024-03-19 06:49:21 +01:00
|
|
|
actor: activity.actor().id().ok_or(super::FieldError("actor"))?.to_string(),
|
|
|
|
object: activity.object().id().map(|x| x.to_string()),
|
2024-03-16 20:09:06 +01:00
|
|
|
target: activity.target().map(|x| x.to_string()),
|
|
|
|
published: activity.published().ok_or(super::FieldError("published"))?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|