feat: improved addressing select

This commit is contained in:
əlemi 2024-04-12 19:35:01 +02:00
parent 3a874ed3e7
commit 0d340185cf
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 48 additions and 43 deletions

View file

@ -2,7 +2,6 @@ pub mod server;
pub mod model;
pub mod routes;
pub mod tools;
pub mod errors;
#[cfg(feature = "migrations")]

View file

@ -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<super::object::Entity> for Entity {
}
impl ActiveModelBehavior for ActiveModel {}
#[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 {
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<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 })
}
}
impl Entity {
pub fn find_activities() -> Select<Entity> {
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
}
}

View file

@ -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<S: QueryTrait<QueryStatement = SelectStatement>> {
pub selector: S,
}
impl<S: QueryTrait<QueryStatement = SelectStatement>> Prefixer<S> {
pub fn new(selector: S) -> Self {
Self { selector }
}
pub fn add_columns<T: EntityTrait>(mut self, entity: T) -> Self {
for col in <T::Column as sea_orm::entity::Iterable>::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<crate::model::object::Model>,
}
impl FromQueryResult for ActivityWithObject {
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 })
}
}