From 0d340185cf7ebad20184f6a3c74b625c880f37c9 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 12 Apr 2024 19:35:01 +0200 Subject: [PATCH] feat: improved addressing select --- src/main.rs | 1 - src/model/addressing.rs | 49 ++++++++++++++++++++++++++++++++++++++++- src/tools.rs | 41 ---------------------------------- 3 files changed, 48 insertions(+), 43 deletions(-) delete mode 100644 src/tools.rs diff --git a/src/main.rs b/src/main.rs index 6178aa9..3c12cff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ pub mod server; pub mod model; pub mod routes; -pub mod tools; pub mod errors; #[cfg(feature = "migrations")] diff --git a/src/model/addressing.rs b/src/model/addressing.rs index 478acee..b5c552e 100644 --- a/src/model/addressing.rs +++ b/src/model/addressing.rs @@ -1,4 +1,7 @@ -use sea_orm::entity::prelude::*; +use apb::{ActivityMut, Node}; +use sea_orm::{entity::prelude::*, FromQueryResult, Iterable, QuerySelect, SelectColumns}; + +use crate::routes::activitypub::{activity::ap_activity, object::ap_object}; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "addressing")] @@ -55,3 +58,47 @@ impl Related for Entity { } impl ActiveModelBehavior for ActiveModel {} + +#[derive(Debug)] +pub struct EmbeddedActivity { + pub activity: crate::model::activity::Model, + pub object: Option, +} + +impl From for serde_json::Value { + fn from(value: EmbeddedActivity) -> Self { + match value.object { + Some(o) => ap_activity(value.activity).set_object(Node::object(ap_object(o))), + None => ap_activity(value.activity) + } + } +} + +impl FromQueryResult for EmbeddedActivity { + fn from_query_result(res: &sea_orm::QueryResult, _pre: &str) -> Result { + 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 }) + } +} + +impl Entity { + pub fn find_activities() -> Select { + let mut select = Entity::find() + .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() { + select = select.select_column_as(col, format!("{}{}", crate::model::activity::Entity.table_name(), col.to_string())); + } + + for col in crate::model::object::Column::iter() { + select = select.select_column_as(col, format!("{}{}", crate::model::object::Entity.table_name(), col.to_string())); + } + + select + } +} diff --git a/src/tools.rs b/src/tools.rs deleted file mode 100644 index e6cee04..0000000 --- a/src/tools.rs +++ /dev/null @@ -1,41 +0,0 @@ -// yanked from https://github.com/SeaQL/sea-orm/discussions/1502 -use sea_orm::{prelude::*, FromQueryResult}; -use sea_orm::sea_query::{Alias, IntoIden, SelectExpr, SelectStatement}; -use sea_orm::{EntityTrait, QueryTrait}; - -pub struct Prefixer> { - pub selector: S, -} - -impl> Prefixer { - pub fn new(selector: S) -> Self { - Self { selector } - } - pub fn add_columns(mut self, entity: T) -> Self { - for col in ::iter() { - let alias = format!("{}{}", entity.table_name(), col.to_string()); // we use entity.table_name() as prefix - self.selector.query().expr(SelectExpr { - expr: col.select_as(col.into_expr()), - alias: Some(Alias::new(&alias).into_iden()), - window: None, - }); - } - self - } -} - -// adapted from https://github.com/SeaQL/sea-orm/discussions/1502 -#[derive(Debug)] -pub struct ActivityWithObject { - pub activity: crate::model::activity::Model, - pub object: Option, -} - -impl FromQueryResult for ActivityWithObject { - fn from_query_result(res: &sea_orm::QueryResult, _pre: &str) -> Result { - 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 }) - } -}