diff --git a/src/main.rs b/src/main.rs index 6178aa9c..3c12cff1 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 478aceea..b5c552ef 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 e6cee046..00000000 --- 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 }) - } -}