forked from alemi/upub
feat: added back ToJson trait for db entities
This commit is contained in:
parent
106380d3b7
commit
3a53c6a2ed
5 changed files with 99 additions and 47 deletions
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use crate::activitystream::{self, ObjectType, BaseType};
|
use crate::activitystream::{node::{InsertStr, Node}, object::{activity::{Activity, ActivityType}, actor::Actor, Object, ObjectType}, Base, BaseType};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "activities")]
|
#[sea_orm(table_name = "activities")]
|
||||||
|
@ -9,7 +9,7 @@ pub struct Model {
|
||||||
/// must be https://instance.org/users/:user , even if local! TODO bad design...
|
/// must be https://instance.org/users/:user , even if local! TODO bad design...
|
||||||
pub id: String,
|
pub id: String,
|
||||||
|
|
||||||
pub activity_type: activitystream::types::ActivityType,
|
pub activity_type: ActivityType,
|
||||||
pub actor: String, // TODO relates to USER
|
pub actor: String, // TODO relates to USER
|
||||||
pub object: Option<String>, // TODO relates to NOTES maybe????? maybe other tables??????
|
pub object: Option<String>, // TODO relates to NOTES maybe????? maybe other tables??????
|
||||||
pub target: Option<String>, // TODO relates to USER maybe??
|
pub target: Option<String>, // TODO relates to USER maybe??
|
||||||
|
@ -23,31 +23,36 @@ pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
impl activitystream::Object for Model {
|
impl Base for Model {
|
||||||
fn id(&self) -> Option<&str> {
|
fn id(&self) -> Option<&str> {
|
||||||
Some(&self.id)
|
Some(&self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full_type(&self) -> Option<activitystream::BaseType> {
|
fn base_type(&self) -> Option<BaseType> {
|
||||||
Some(BaseType::Object(ObjectType::Activity(self.activity_type)))
|
Some(BaseType::Object(ObjectType::Activity(self.activity_type)))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Object for Model {
|
||||||
fn published(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
fn published(&self) -> Option<chrono::DateTime<chrono::Utc>> {
|
||||||
Some(self.published)
|
Some(self.published)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl activitystream::Activity for Model {
|
impl Activity for Model {
|
||||||
fn activity_type(&self) -> Option<activitystream::types::ActivityType> {
|
fn activity_type(&self) -> Option<ActivityType> {
|
||||||
Some(self.activity_type)
|
Some(self.activity_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn actor_id(&self) -> Option<&str> {
|
fn actor(&self) -> Node<impl Actor> {
|
||||||
Some(&self.actor)
|
Node::<serde_json::Value>::Link(Box::new(self.actor.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn object_id(&self) -> Option<&str> {
|
fn object(&self) -> Node<impl Object> {
|
||||||
self.object.as_deref()
|
match &self.object {
|
||||||
|
None => Node::Empty::<serde_json::Value>,
|
||||||
|
Some(x) => Node::Link(Box::new(x.clone())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn target(&self) -> Option<&str> {
|
fn target(&self) -> Option<&str> {
|
||||||
|
@ -56,14 +61,27 @@ impl activitystream::Activity for Model {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Model {
|
impl Model {
|
||||||
pub fn new(activity: &impl activitystream::Activity) -> Result<Self, super::FieldError> {
|
pub fn new(activity: &impl Activity) -> Result<Self, super::FieldError> {
|
||||||
Ok(Model {
|
Ok(Model {
|
||||||
id: activity.id().ok_or(super::FieldError("id"))?.to_string(),
|
id: activity.id().ok_or(super::FieldError("id"))?.to_string(),
|
||||||
activity_type: activity.activity_type().ok_or(super::FieldError("type"))?,
|
activity_type: activity.activity_type().ok_or(super::FieldError("type"))?,
|
||||||
actor: activity.actor_id().ok_or(super::FieldError("actor"))?.to_string(),
|
actor: activity.actor().id().ok_or(super::FieldError("actor"))?.to_string(),
|
||||||
object: activity.object_id().map(|x| x.to_string()),
|
object: activity.object().id().map(|x| x.to_string()),
|
||||||
target: activity.target().map(|x| x.to_string()),
|
target: activity.target().map(|x| x.to_string()),
|
||||||
published: activity.published().ok_or(super::FieldError("published"))?,
|
published: activity.published().ok_or(super::FieldError("published"))?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl super::ToJson for Model {
|
||||||
|
fn json(&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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,10 @@ pub mod user;
|
||||||
pub mod object;
|
pub mod object;
|
||||||
pub mod activity;
|
pub mod activity;
|
||||||
|
|
||||||
|
pub trait ToJson {
|
||||||
|
fn json(&self) -> serde_json::Value;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, thiserror::Error)]
|
#[derive(Debug, Clone, thiserror::Error)]
|
||||||
#[error("missing required field: '{0}'")]
|
#[error("missing required field: '{0}'")]
|
||||||
pub struct FieldError(pub &'static str);
|
pub struct FieldError(pub &'static str);
|
||||||
|
@ -12,13 +16,13 @@ pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbEr
|
||||||
user::Entity::insert(user::ActiveModel {
|
user::Entity::insert(user::ActiveModel {
|
||||||
id: sea_orm::Set("http://localhost:3000/users/root".into()),
|
id: sea_orm::Set("http://localhost:3000/users/root".into()),
|
||||||
name: sea_orm::Set("root".into()),
|
name: sea_orm::Set("root".into()),
|
||||||
actor_type: sea_orm::Set(super::activitystream::types::ActorType::Person),
|
actor_type: sea_orm::Set(super::activitystream::object::actor::ActorType::Person),
|
||||||
}).exec(db).await?;
|
}).exec(db).await?;
|
||||||
|
|
||||||
object::Entity::insert(object::ActiveModel {
|
object::Entity::insert(object::ActiveModel {
|
||||||
id: sea_orm::Set("http://localhost:3000/objects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into()),
|
id: sea_orm::Set("http://localhost:3000/objects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into()),
|
||||||
name: sea_orm::Set(None),
|
name: sea_orm::Set(None),
|
||||||
object_type: sea_orm::Set(crate::activitystream::types::StatusType::Note),
|
object_type: sea_orm::Set(crate::activitystream::object::ObjectType::Note),
|
||||||
attributed_to: sea_orm::Set(Some("http://localhost:3000/users/root".into())),
|
attributed_to: sea_orm::Set(Some("http://localhost:3000/users/root".into())),
|
||||||
summary: sea_orm::Set(None),
|
summary: sea_orm::Set(None),
|
||||||
content: sea_orm::Set(Some("Hello world!".into())),
|
content: sea_orm::Set(Some("Hello world!".into())),
|
||||||
|
@ -27,7 +31,7 @@ pub async fn faker(db: &sea_orm::DatabaseConnection) -> Result<(), sea_orm::DbEr
|
||||||
|
|
||||||
activity::Entity::insert(activity::ActiveModel {
|
activity::Entity::insert(activity::ActiveModel {
|
||||||
id: sea_orm::Set("http://localhost:3000/activities/ebac57e1-9828-438c-be34-a44a52de7641".into()),
|
id: sea_orm::Set("http://localhost:3000/activities/ebac57e1-9828-438c-be34-a44a52de7641".into()),
|
||||||
activity_type: sea_orm::Set(crate::activitystream::types::ActivityType::Create),
|
activity_type: sea_orm::Set(crate::activitystream::object::activity::ActivityType::Create),
|
||||||
actor: sea_orm::Set("http://localhost:3000/users/root".into()),
|
actor: sea_orm::Set("http://localhost:3000/users/root".into()),
|
||||||
object: sea_orm::Set(Some("http://localhost:3000/obkects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into())),
|
object: sea_orm::Set(Some("http://localhost:3000/obkects/4e28d30b-33c1-4336-918b-6fbe592bdd44".into())),
|
||||||
target: sea_orm::Set(None),
|
target: sea_orm::Set(None),
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use crate::activitystream::{Object, types::{BaseType, ObjectType, StatusType}};
|
use crate::activitystream::{node::InsertStr, object::{Actor, Object, ObjectType}, Base, BaseType, Node};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "objects")]
|
#[sea_orm(table_name = "objects")]
|
||||||
|
@ -8,7 +8,7 @@ pub struct Model {
|
||||||
#[sea_orm(primary_key)]
|
#[sea_orm(primary_key)]
|
||||||
/// must be full uri!!! maybe not great?
|
/// must be full uri!!! maybe not great?
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub object_type: StatusType,
|
pub object_type: ObjectType,
|
||||||
pub attributed_to: Option<String>,
|
pub attributed_to: Option<String>,
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
pub summary: Option<String>,
|
pub summary: Option<String>,
|
||||||
|
@ -21,17 +21,23 @@ pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
impl crate::activitystream::Object for Model {
|
impl Base for Model {
|
||||||
fn id(&self) -> Option<&str> {
|
fn id(&self) -> Option<&str> {
|
||||||
Some(&self.id)
|
Some(&self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full_type(&self) -> Option<crate::activitystream::BaseType> {
|
fn base_type(&self) -> Option<BaseType> {
|
||||||
Some(BaseType::Object(ObjectType::Status(self.object_type)))
|
Some(BaseType::Object(self.object_type))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn attributed_to (&self) -> Option<&str> {
|
impl Object for Model {
|
||||||
self.attributed_to.as_deref()
|
fn object_type(&self) -> Option<ObjectType> {
|
||||||
|
Some(self.object_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn attributed_to(&self) -> Node<impl Actor> {
|
||||||
|
Node::<serde_json::Value>::from(self.attributed_to.as_deref())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> Option<&str> {
|
fn name(&self) -> Option<&str> {
|
||||||
|
@ -53,13 +59,10 @@ impl crate::activitystream::Object for Model {
|
||||||
|
|
||||||
impl Model {
|
impl Model {
|
||||||
pub fn new(object: &impl Object) -> Result<Self, super::FieldError> {
|
pub fn new(object: &impl Object) -> Result<Self, super::FieldError> {
|
||||||
let Some(BaseType::Object(ObjectType::Status(t))) = object.full_type() else {
|
|
||||||
return Err(super::FieldError("type")); // TODO maybe just wrong? better errors!
|
|
||||||
};
|
|
||||||
Ok(Model {
|
Ok(Model {
|
||||||
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
||||||
object_type: t,
|
object_type: object.object_type().ok_or(super::FieldError("type"))?,
|
||||||
attributed_to: object.attributed_to().map(|x| x.to_string()),
|
attributed_to: object.attributed_to().id().map(|x| x.to_string()),
|
||||||
name: object.name().map(|x| x.to_string()),
|
name: object.name().map(|x| x.to_string()),
|
||||||
summary: object.summary().map(|x| x.to_string()),
|
summary: object.summary().map(|x| x.to_string()),
|
||||||
content: object.content().map(|x| x.to_string()),
|
content: object.content().map(|x| x.to_string()),
|
||||||
|
@ -67,3 +70,17 @@ impl Model {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl super::ToJson for Model {
|
||||||
|
fn json(&self) -> serde_json::Value {
|
||||||
|
let mut map = serde_json::Map::new();
|
||||||
|
map.insert_str("id", Some(&self.id));
|
||||||
|
map.insert_str("type", Some(self.object_type.as_ref()));
|
||||||
|
map.insert_str("attributedTo", self.attributed_to.as_deref());
|
||||||
|
map.insert_str("name", self.name.as_deref());
|
||||||
|
map.insert_str("summary", self.summary.as_deref());
|
||||||
|
map.insert_str("content", self.content.as_deref());
|
||||||
|
map.insert_timestr("published", Some(self.published));
|
||||||
|
serde_json::Value::Object(map)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
use crate::activitystream::{self, types::ActorType};
|
use crate::activitystream::{node::InsertStr, object::{Actor, ActorType}, Base, BaseType, Object, ObjectType};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||||
#[sea_orm(table_name = "users")]
|
#[sea_orm(table_name = "users")]
|
||||||
|
@ -19,29 +19,44 @@ pub enum Relation {}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
impl activitystream::Object for Model {
|
impl Base for Model {
|
||||||
fn id(&self) -> Option<&str> {
|
fn id(&self) -> Option<&str> {
|
||||||
Some(&self.id)
|
Some(&self.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn full_type(&self) -> Option<activitystream::BaseType> {
|
fn base_type(&self) -> Option<BaseType> {
|
||||||
Some(activitystream::BaseType::Object(activitystream::ObjectType::Actor(self.actor_type)))
|
Some(BaseType::Object(ObjectType::Actor(self.actor_type)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Object for Model {
|
||||||
fn name (&self) -> Option<&str> {
|
fn name (&self) -> Option<&str> {
|
||||||
Some(&self.name)
|
Some(&self.name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Actor for Model {
|
||||||
|
fn actor_type(&self) -> Option<ActorType> {
|
||||||
|
Some(self.actor_type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Model {
|
impl Model {
|
||||||
pub fn new(object: &impl activitystream::Object) -> Result<Self, super::FieldError> {
|
pub fn new(object: &impl Actor) -> Result<Self, super::FieldError> {
|
||||||
let Some(activitystream::BaseType::Object(activitystream::ObjectType::Actor(t))) = object.full_type() else {
|
|
||||||
return Err(super::FieldError("type")); // TODO maybe just wrong? better errors!
|
|
||||||
};
|
|
||||||
Ok(Model {
|
Ok(Model {
|
||||||
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
id: object.id().ok_or(super::FieldError("id"))?.to_string(),
|
||||||
actor_type: t,
|
actor_type: object.actor_type().ok_or(super::FieldError("type"))?,
|
||||||
name: object.name().ok_or(super::FieldError("name"))?.to_string(),
|
name: object.name().ok_or(super::FieldError("name"))?.to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl super::ToJson for Model {
|
||||||
|
fn json(&self) -> serde_json::Value {
|
||||||
|
let mut map = serde_json::Map::new();
|
||||||
|
map.insert_str("id", Some(&self.id));
|
||||||
|
map.insert_str("type", Some(self.actor_type.as_ref()));
|
||||||
|
map.insert_str("name", Some(&self.name));
|
||||||
|
serde_json::Value::Object(map)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::activitystream::ObjectType;
|
use crate::activitystream::object::{Activity, ActivityType};
|
||||||
use crate::activitystream::ToJson;
|
use crate::activitystream::{Base, BaseType, Node, ObjectType};
|
||||||
use crate::activitystream::Activity;
|
use crate::model::{activity, object, user, ToJson};
|
||||||
use crate::activitystream::{types::ActivityType, Object, BaseType, LinkedObject};
|
|
||||||
use crate::model::{activity, object, user};
|
|
||||||
use axum::{extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router};
|
use axum::{extract::{Path, State}, http::StatusCode, routing::{get, post}, Json, Router};
|
||||||
use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel};
|
use sea_orm::{DatabaseConnection, EntityTrait, IntoActiveModel};
|
||||||
|
|
||||||
|
@ -28,7 +26,7 @@ pub async fn serve(db: DatabaseConnection) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn inbox(State(db) : State<Arc<DatabaseConnection>>, Json(object): Json<serde_json::Value>) -> Result<Json<serde_json::Value>, StatusCode> {
|
async fn inbox(State(db) : State<Arc<DatabaseConnection>>, Json(object): Json<serde_json::Value>) -> Result<Json<serde_json::Value>, StatusCode> {
|
||||||
match object.full_type() {
|
match object.base_type() {
|
||||||
None => { Err(StatusCode::BAD_REQUEST) },
|
None => { Err(StatusCode::BAD_REQUEST) },
|
||||||
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
|
Some(BaseType::Link(_x)) => Err(StatusCode::UNPROCESSABLE_ENTITY), // we could but not yet
|
||||||
Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
Some(BaseType::Object(ObjectType::Activity(ActivityType::Activity))) => Err(StatusCode::UNPROCESSABLE_ENTITY),
|
||||||
|
@ -38,8 +36,8 @@ async fn inbox(State(db) : State<Arc<DatabaseConnection>>, Json(object): Json<se
|
||||||
let Ok(activity_entity) = activity::Model::new(&object) else {
|
let Ok(activity_entity) = activity::Model::new(&object) else {
|
||||||
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
};
|
};
|
||||||
let Some(LinkedObject::Object(obj)) = object.object() else {
|
let Node::Object(obj) = object.object() else {
|
||||||
// TODO we could process non-embedded activities but im lazy rn
|
// TODO we could process non-embedded activities or arrays but im lazy rn
|
||||||
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
return Err(StatusCode::UNPROCESSABLE_ENTITY);
|
||||||
};
|
};
|
||||||
let Ok(obj_entity) = object::Model::new(&obj) else {
|
let Ok(obj_entity) = object::Model::new(&obj) else {
|
||||||
|
|
Loading…
Reference in a new issue