From 35f3781a32b6dca1b606434d4dfd49a7b178ec48 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 27 Feb 2022 21:12:51 +0000 Subject: [PATCH] [COMPONENT][Collection] Add mechanism for specifying the ordering of note and actor queries --- components/Collection/Collection.php | 17 ++++++++++++++--- .../Collection/Util/Controller/Collection.php | 4 ++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/components/Collection/Collection.php b/components/Collection/Collection.php index bceafa02c5..6dbc9a1999 100644 --- a/components/Collection/Collection.php +++ b/components/Collection/Collection.php @@ -23,20 +23,31 @@ class Collection extends Component * Supports a variety of query terms and is used both in feeds and * in search. Uses query builders to allow for extension */ - public static function query(string $query, int $page, ?string $locale = null, ?Actor $actor = null): array + public static function query(string $query, int $page, ?string $locale = null, ?Actor $actor = null, array $note_order_by = [], array $actor_order_by = []): array { $note_criteria = null; $actor_criteria = null; if (!empty($query = trim($query))) { [$note_criteria, $actor_criteria] = Parser::parse($query, $locale, $actor); } + $note_qb = DB::createQueryBuilder(); $actor_qb = DB::createQueryBuilder(); // TODO consider selecting note related stuff, to avoid separate queries (though they're cached, so maybe it's okay) - $note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->addOrderBy('note.id', 'DESC'); - $actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->addOrderBy('actor.id', 'DESC'); + $note_qb->select('note')->from('App\Entity\Note', 'note'); + $actor_qb->select('actor')->from('App\Entity\Actor', 'actor'); Event::handle('CollectionQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]); + // Handle ordering + $note_order_by = !empty($note_order_by) ? $note_order_by : ['note.created' => 'DESC', 'note.id' => 'DESC']; + $actor_order_by = !empty($actor_order_by) ? $actor_order_by : ['actor.created' => 'DESC', 'actor.id' => 'DESC']; + foreach ($note_order_by as $field => $order) { + $note_qb->addOrderBy($field, $order); + } + foreach ($actor_order_by as $field => $order) { + $actor_qb->addOrderBy($field, $order); + } + $notes = []; $actors = []; if (!\is_null($note_criteria)) { diff --git a/components/Collection/Util/Controller/Collection.php b/components/Collection/Util/Controller/Collection.php index aac4e8f54f..a43e33056d 100644 --- a/components/Collection/Util/Controller/Collection.php +++ b/components/Collection/Util/Controller/Collection.php @@ -11,10 +11,10 @@ use Component\Collection\Collection as CollectionModule; class Collection extends Controller { - public function query(string $query, ?string $locale = null, ?Actor $actor = null): array + public function query(string $query, ?string $locale = null, ?Actor $actor = null, array $note_order_by = [], array $actor_order_by = []): array { $actor ??= Common::actor(); $locale ??= Common::currentLanguage()->getLocale(); - return CollectionModule::query($query, $this->int('page') ?? 1, $locale, $actor); + return CollectionModule::query($query, $this->int('page') ?? 1, $locale, $actor, $note_order_by, $actor_order_by); } }