2024-04-12 19:35:01 +02:00
|
|
|
use apb::{ActivityMut, Node};
|
|
|
|
use sea_orm::{entity::prelude::*, FromQueryResult, Iterable, QuerySelect, SelectColumns};
|
|
|
|
|
2024-03-24 04:03:44 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
|
|
|
#[sea_orm(table_name = "addressing")]
|
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
2024-03-24 05:55:50 +01:00
|
|
|
pub id: i64,
|
2024-03-24 04:03:44 +01:00
|
|
|
pub actor: String,
|
2024-03-25 02:26:47 +01:00
|
|
|
pub server: String,
|
2024-04-21 15:41:29 +02:00
|
|
|
pub activity: Option<String>,
|
2024-03-24 04:03:44 +01:00
|
|
|
pub object: Option<String>,
|
2024-03-24 04:58:49 +01:00
|
|
|
pub published: ChronoDateTimeUtc,
|
2024-03-24 04:03:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
|
|
|
pub enum Relation {
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::user::Entity",
|
|
|
|
from = "Column::Actor",
|
|
|
|
to = "super::user::Column::Id"
|
|
|
|
)]
|
|
|
|
User,
|
|
|
|
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::activity::Entity",
|
|
|
|
from = "Column::Activity",
|
|
|
|
to = "super::activity::Column::Id"
|
|
|
|
)]
|
|
|
|
Activity,
|
|
|
|
|
|
|
|
#[sea_orm(
|
|
|
|
belongs_to = "super::object::Entity",
|
|
|
|
from = "Column::Object",
|
|
|
|
to = "super::object::Column::Id"
|
|
|
|
)]
|
|
|
|
Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::user::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::User.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::activity::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Activity.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Related<super::object::Entity> for Entity {
|
|
|
|
fn to() -> RelationDef {
|
|
|
|
Relation::Object.def()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
2024-04-12 19:35:01 +02:00
|
|
|
|
2024-04-21 15:41:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-12 19:35:01 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct EmbeddedActivity {
|
|
|
|
pub activity: crate::model::activity::Model,
|
|
|
|
pub object: Option<crate::model::object::Model>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<EmbeddedActivity> for serde_json::Value {
|
|
|
|
fn from(value: EmbeddedActivity) -> Self {
|
2024-04-19 03:28:39 +02:00
|
|
|
let a = value.activity.ap();
|
2024-04-12 19:35:01 +02:00
|
|
|
match value.object {
|
2024-04-19 03:28:39 +02:00
|
|
|
None => a,
|
|
|
|
Some(o) => a.set_object(Node::object(o.ap())),
|
2024-04-12 19:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromQueryResult for EmbeddedActivity {
|
|
|
|
fn from_query_result(res: &sea_orm::QueryResult, _pre: &str) -> Result<Self, sea_orm::DbErr> {
|
|
|
|
let activity = crate::model::activity::Model::from_query_result(res, crate::model::activity::Entity.table_name())?;
|
|
|
|
let object = crate::model::object::Model::from_query_result(res, crate::model::object::Entity.table_name()).ok();
|
|
|
|
Ok(Self { activity, object })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:41:29 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct WrappedObject {
|
|
|
|
pub activity: Option<crate::model::activity::Model>,
|
|
|
|
pub object: crate::model::object::Model,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<WrappedObject> for serde_json::Value {
|
|
|
|
fn from(value: WrappedObject) -> Self {
|
|
|
|
match value.activity {
|
|
|
|
None => value.object.ap(),
|
|
|
|
Some(a) => a.ap().set_object(
|
|
|
|
Node::object(value.object.ap())
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromQueryResult for WrappedObject {
|
|
|
|
fn from_query_result(res: &sea_orm::QueryResult, _pre: &str) -> Result<Self, sea_orm::DbErr> {
|
|
|
|
let activity = crate::model::activity::Model::from_query_result(res, crate::model::activity::Entity.table_name()).ok();
|
|
|
|
let object = crate::model::object::Model::from_query_result(res, crate::model::object::Entity.table_name())?;
|
|
|
|
Ok(Self { activity, object })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-12 19:35:01 +02:00
|
|
|
impl Entity {
|
|
|
|
pub fn find_activities() -> Select<Entity> {
|
|
|
|
let mut select = Entity::find()
|
2024-04-15 22:10:49 +02:00
|
|
|
.distinct()
|
2024-04-12 19:35:01 +02:00
|
|
|
.select_only()
|
|
|
|
.join(sea_orm::JoinType::InnerJoin, Relation::Activity.def())
|
|
|
|
// INNERJOIN: filter out addressings for which we don't have an activity anymore
|
|
|
|
// TODO we could in theory return just the link or fetch them again, just ignoring them is mehh
|
|
|
|
.join(sea_orm::JoinType::LeftJoin, crate::model::activity::Relation::Object.def());
|
|
|
|
|
|
|
|
for col in crate::model::activity::Column::iter() {
|
2024-04-19 06:21:26 +02:00
|
|
|
select = select.select_column_as(col, format!("{}{}", crate::model::activity::Entity.table_name(), col.to_string()));
|
2024-04-12 19:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for col in crate::model::object::Column::iter() {
|
2024-04-19 06:21:26 +02:00
|
|
|
select = select.select_column_as(col, format!("{}{}", crate::model::object::Entity.table_name(), col.to_string()));
|
2024-04-12 19:35:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
select
|
|
|
|
}
|
2024-04-12 22:21:23 +02:00
|
|
|
|
|
|
|
pub fn find_objects() -> Select<Entity> {
|
|
|
|
let mut select = Entity::find()
|
2024-04-15 22:10:49 +02:00
|
|
|
.distinct()
|
2024-04-12 22:21:23 +02:00
|
|
|
.select_only()
|
2024-04-21 15:41:29 +02:00
|
|
|
.join(sea_orm::JoinType::InnerJoin, Relation::Object.def())
|
2024-04-19 05:02:59 +02:00
|
|
|
// INNERJOIN: filter out addressings for which we don't have an object anymore
|
2024-04-12 22:21:23 +02:00
|
|
|
// TODO we could in theory return just the link or fetch them again, just ignoring them is mehh
|
2024-04-21 15:41:29 +02:00
|
|
|
.join(sea_orm::JoinType::LeftJoin, crate::model::object::Relation::Activity.def().rev());
|
2024-04-12 22:21:23 +02:00
|
|
|
|
|
|
|
for col in crate::model::object::Column::iter() {
|
2024-04-21 15:41:29 +02:00
|
|
|
select = select.select_column_as(col, format!("{}{}", crate::model::object::Entity.table_name(), col.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for col in crate::model::activity::Column::iter() {
|
|
|
|
select = select.select_column_as(col, format!("{}{}", crate::model::activity::Entity.table_name(), col.to_string()));
|
2024-04-12 22:21:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
select
|
|
|
|
}
|
2024-04-12 19:35:01 +02:00
|
|
|
}
|