2024-04-21 23:19:26 +02:00
|
|
|
use apb::{ActivityMut, ObjectMut};
|
2024-04-24 02:31:35 +02:00
|
|
|
use sea_orm::{entity::prelude::*, Condition, FromQueryResult, Iterable, Order, QueryOrder, QuerySelect, SelectColumns};
|
|
|
|
|
|
|
|
use crate::routes::activitypub::jsonld::LD;
|
2024-04-12 19:35:01 +02:00
|
|
|
|
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-24 02:31:35 +02:00
|
|
|
#[allow(clippy::large_enum_variant)] // tombstone is an outlier, not the norm! this is a beefy enum
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Event {
|
|
|
|
Tombstone,
|
|
|
|
StrayObject(crate::model::object::Model),
|
|
|
|
Activity(crate::model::activity::Model),
|
|
|
|
DeepActivity {
|
|
|
|
activity: crate::model::activity::Model,
|
|
|
|
object: crate::model::object::Model,
|
2024-04-12 19:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 15:41:29 +02:00
|
|
|
|
2024-04-24 02:31:35 +02:00
|
|
|
impl Event {
|
|
|
|
pub fn id(&self) -> &str {
|
|
|
|
match self {
|
|
|
|
Event::Tombstone => "",
|
|
|
|
Event::StrayObject(x) => x.id.as_str(),
|
|
|
|
Event::Activity(x) => x.id.as_str(),
|
|
|
|
Event::DeepActivity { activity: _, object } => object.id.as_str(),
|
|
|
|
}
|
|
|
|
}
|
2024-04-21 23:19:26 +02:00
|
|
|
|
2024-04-24 02:31:35 +02:00
|
|
|
pub fn ap(self, attachment: Option<Vec<crate::model::attachment::Model>>) -> serde_json::Value {
|
|
|
|
let attachment = match attachment {
|
|
|
|
None => apb::Node::Empty,
|
|
|
|
Some(vec) => apb::Node::array(
|
|
|
|
vec.into_iter().map(|x| x.ap()).collect()
|
|
|
|
),
|
|
|
|
};
|
|
|
|
match self {
|
|
|
|
Event::Activity(x) => x.ap(),
|
|
|
|
Event::DeepActivity { activity, object } =>
|
|
|
|
activity.ap().set_object(apb::Node::object(object.ap().set_attachment(attachment))),
|
|
|
|
Event::StrayObject(x) => serde_json::Value::new_object()
|
|
|
|
.set_activity_type(Some(apb::ActivityType::Activity))
|
|
|
|
.set_object(apb::Node::object(x.ap().set_attachment(attachment))),
|
|
|
|
Event::Tombstone => serde_json::Value::new_object()
|
|
|
|
.set_activity_type(Some(apb::ActivityType::Activity))
|
|
|
|
.set_object(apb::Node::object(
|
|
|
|
serde_json::Value::new_object()
|
|
|
|
.set_object_type(Some(apb::ObjectType::Tombstone))
|
|
|
|
)),
|
2024-04-21 15:41:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 02:31:35 +02:00
|
|
|
impl FromQueryResult for Event {
|
2024-04-21 15:41:29 +02:00
|
|
|
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();
|
2024-04-24 02:31:35 +02:00
|
|
|
let object = crate::model::object::Model::from_query_result(res, crate::model::object::Entity.table_name()).ok();
|
|
|
|
match (activity, object) {
|
|
|
|
(Some(activity), Some(object)) => Ok(Self::DeepActivity { activity, object }),
|
|
|
|
(Some(activity), None) => Ok(Self::Activity(activity)),
|
|
|
|
(None, Some(object)) => Ok(Self::StrayObject(object)),
|
|
|
|
(None, None) => Ok(Self::Tombstone),
|
|
|
|
}
|
2024-04-21 15:41:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-04-12 19:35:01 +02:00
|
|
|
impl Entity {
|
2024-04-24 02:31:35 +02:00
|
|
|
pub fn find_addressed() -> Select<Entity> {
|
2024-04-12 22:21:23 +02:00
|
|
|
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-24 02:31:35 +02:00
|
|
|
.join(sea_orm::JoinType::LeftJoin, Relation::Object.def())
|
2024-04-23 17:21:32 +02:00
|
|
|
.join(sea_orm::JoinType::LeftJoin, Relation::Activity.def())
|
2024-04-24 02:31:35 +02:00
|
|
|
.filter(
|
|
|
|
// TODO ghetto double inner join because i want to filter out tombstones
|
|
|
|
Condition::any()
|
|
|
|
.add(crate::model::activity::Column::Id.is_not_null())
|
|
|
|
.add(crate::model::object::Column::Id.is_not_null())
|
|
|
|
)
|
|
|
|
.order_by(Column::Published, Order::Desc);
|
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
|
|
|
}
|