From 0e309e143c79382ed845aaa3ee2764a92610f806 Mon Sep 17 00:00:00 2001 From: alemi Date: Tue, 13 Aug 2024 20:25:05 +0200 Subject: [PATCH] fix: allow querying distinct by object --- upub/core/src/selector/query.rs | 18 +++++++++++++----- upub/routes/src/activitypub/activity.rs | 2 +- upub/routes/src/activitypub/object/context.rs | 2 +- upub/routes/src/activitypub/object/mod.rs | 4 ++-- upub/routes/src/activitypub/object/replies.rs | 2 +- upub/routes/src/builders.rs | 4 ++-- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/upub/core/src/selector/query.rs b/upub/core/src/selector/query.rs index 828ca8d..7d95c72 100644 --- a/upub/core/src/selector/query.rs +++ b/upub/core/src/selector/query.rs @@ -4,11 +4,15 @@ use crate::model; pub struct Query; impl Query { - pub fn feed(my_id: Option) -> Select { + pub fn feed(my_id: Option, by_object: bool) -> Select { let mut select = model::addressing::Entity::find() .distinct_on([ (model::addressing::Entity, model::addressing::Column::Published).into_column_ref(), - (model::activity::Entity, model::activity::Column::Internal).into_column_ref(), + if by_object { + (model::object::Entity, model::object::Column::Internal).into_column_ref() + } else { + (model::activity::Entity, model::activity::Column::Internal).into_column_ref() + }, ]) .join(sea_orm::JoinType::LeftJoin, model::addressing::Relation::Activities.def()) .join(sea_orm::JoinType::LeftJoin, model::addressing::Relation::Objects.def()) @@ -18,9 +22,13 @@ impl Query { .add(model::activity::Column::Id.is_not_null()) .add(model::object::Column::Id.is_not_null()) ) - .order_by(model::addressing::Column::Published, Order::Desc) - .order_by(model::activity::Column::Internal, Order::Desc) - .select_only(); + .order_by(model::addressing::Column::Published, Order::Desc); + + select = if by_object { + select.order_by(model::object::Column::Internal, Order::Desc).select_only() + } else { + select.order_by(model::activity::Column::Internal, Order::Desc).select_only() + }; for col in model::activity::Column::iter() { select = select.select_column_as(col, format!("{}{}", model::activity::Entity.table_name(), col.to_string())); diff --git a/upub/routes/src/activitypub/activity.rs b/upub/routes/src/activitypub/activity.rs index 4eb23cf..aac5847 100644 --- a/upub/routes/src/activitypub/activity.rs +++ b/upub/routes/src/activitypub/activity.rs @@ -23,7 +23,7 @@ pub async fn view( } } - let row = upub::Query::feed(auth.my_id()) + let row = upub::Query::feed(auth.my_id(), false) .filter(model::activity::Column::Id.eq(&aid)) .filter(auth.filter()) .into_model::() diff --git a/upub/routes/src/activitypub/object/context.rs b/upub/routes/src/activitypub/object/context.rs index 1ed0a94..661e774 100644 --- a/upub/routes/src/activitypub/object/context.rs +++ b/upub/routes/src/activitypub/object/context.rs @@ -11,7 +11,7 @@ pub async fn get( ) -> crate::ApiResult> { let context = ctx.oid(&id); - let count = upub::Query::feed(auth.my_id()) + let count = upub::Query::feed(auth.my_id(), true) .filter(auth.filter()) .filter(model::object::Column::Context.eq(&context)) .count(ctx.db()) diff --git a/upub/routes/src/activitypub/object/mod.rs b/upub/routes/src/activitypub/object/mod.rs index de74e4c..76d5543 100644 --- a/upub/routes/src/activitypub/object/mod.rs +++ b/upub/routes/src/activitypub/object/mod.rs @@ -27,7 +27,7 @@ pub async fn view( } } - let item = upub::Query::feed(auth.my_id()) + let item = upub::Query::feed(auth.my_id(), true) .filter(model::object::Column::Id.eq(&oid)) .filter(auth.filter()) .into_model::() @@ -44,7 +44,7 @@ pub async fn view( let mut replies = apb::Node::Empty; if ctx.cfg().security.show_reply_ids { - let replies_ids = upub::Query::feed(auth.my_id()) + let replies_ids = upub::Query::feed(auth.my_id(), true) .filter(model::object::Column::InReplyTo.eq(oid)) .filter(auth.filter()) .select_only() diff --git a/upub/routes/src/activitypub/object/replies.rs b/upub/routes/src/activitypub/object/replies.rs index 24505e3..263fa66 100644 --- a/upub/routes/src/activitypub/object/replies.rs +++ b/upub/routes/src/activitypub/object/replies.rs @@ -15,7 +15,7 @@ pub async fn get( // ctx.fetch_thread(&oid).await?; // } - let replies_ids = upub::Query::feed(auth.my_id()) + let replies_ids = upub::Query::feed(auth.my_id(), true) .filter(model::object::Column::InReplyTo.eq(ctx.oid(&id))) .filter(auth.filter()) .select_only() diff --git a/upub/routes/src/builders.rs b/upub/routes/src/builders.rs index 49125e7..e7bfee7 100644 --- a/upub/routes/src/builders.rs +++ b/upub/routes/src/builders.rs @@ -5,7 +5,7 @@ use upub::selector::{BatchFillable, RichActivity}; use crate::activitypub::Pagination; -#[deprecated = "just query directly maybe?"] +//#[deprecated = "just query directly maybe?"] pub async fn paginate_feed( id: String, filter: Condition, @@ -17,7 +17,7 @@ pub async fn paginate_feed( let limit = page.batch.unwrap_or(20).min(50); let offset = page.offset.unwrap_or(0); - let mut select = upub::Query::feed(my_id); + let mut select = upub::Query::feed(my_id, false); if with_users { select = select