diff --git a/upub/core/src/selector/query.rs b/upub/core/src/selector/query.rs index 7d95c72..7cfab01 100644 --- a/upub/core/src/selector/query.rs +++ b/upub/core/src/selector/query.rs @@ -4,15 +4,11 @@ use crate::model; pub struct Query; impl Query { - pub fn feed(my_id: Option, by_object: bool) -> Select { + pub fn feed(my_id: Option) -> Select { let mut select = model::addressing::Entity::find() .distinct_on([ (model::addressing::Entity, model::addressing::Column::Published).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() - }, + (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()) @@ -22,13 +18,9 @@ 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); - - 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() - }; + .order_by(model::addressing::Column::Published, Order::Desc) + .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())); @@ -56,6 +48,30 @@ impl Query { select } + pub fn objects(my_id: Option) -> Select { + let mut select = model::addressing::Entity::find() + .distinct() + .join(sea_orm::JoinType::InnerJoin, model::addressing::Relation::Objects.def()) + .order_by(model::object::Column::Published, Order::Desc) + .select_only(); + + for col in model::object::Column::iter() { + select = select.select_column_as(col, format!("{}{}", model::object::Entity.table_name(), col.to_string())); + } + + if let Some(uid) = my_id { + select = select + .join( + sea_orm::JoinType::LeftJoin, + model::object::Relation::Likes.def() + .on_condition(move |_l, _r| model::like::Column::Actor.eq(uid).into_condition()), + ) + .select_column_as(model::like::Column::Actor, format!("{}{}", model::like::Entity.table_name(), model::like::Column::Actor.to_string())); + } + + select + } + pub fn related(from: Option, to: Option, pending: bool) -> Select { let mut condition = Condition::all(); diff --git a/upub/routes/src/activitypub/activity.rs b/upub/routes/src/activitypub/activity.rs index aac5847..4eb23cf 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(), false) + let row = upub::Query::feed(auth.my_id()) .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 0e2366e..618b136 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(), true) + let count = upub::Query::objects(auth.my_id()) .filter(auth.filter()) .filter(model::object::Column::Context.eq(&context)) .count(ctx.db()) @@ -43,7 +43,7 @@ pub async fn page( let offset = page.offset.unwrap_or(0); - let items = upub::Query::feed(auth.my_id(), true) + let items = upub::Query::objects(auth.my_id()) .filter(filter) // TODO also limit to only local activities .limit(limit) diff --git a/upub/routes/src/activitypub/object/mod.rs b/upub/routes/src/activitypub/object/mod.rs index 76d5543..fd6f2e8 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(), true) + let item = upub::Query::objects(auth.my_id()) .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(), true) + let replies_ids = upub::Query::objects(auth.my_id()) .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 263fa66..b71d4dc 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(), true) + let replies_ids = upub::Query::objects(auth.my_id()) .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 e7bfee7..85ffe90 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 = "use upub::Query directly"] 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, false); + let mut select = upub::Query::feed(my_id); if with_users { select = select