From 72ad7758406e9ba8f7123ef716030d3e6ad57a69 Mon Sep 17 00:00:00 2001 From: SychO9 Date: Fri, 12 Mar 2021 15:01:08 +0100 Subject: [PATCH] Update to beta.16 --- extensions/sticky/extend.php | 14 +++-- extensions/sticky/src/Gambit/StickyGambit.php | 29 ----------- .../PinStickiedDiscussionsToTop.php | 52 ++++++++----------- .../sticky/src/Query/StickyFilterGambit.php | 43 +++++++++++++++ 4 files changed, 74 insertions(+), 64 deletions(-) delete mode 100644 extensions/sticky/src/Gambit/StickyGambit.php rename extensions/sticky/src/{Listener => }/PinStickiedDiscussionsToTop.php (64%) create mode 100644 extensions/sticky/src/Query/StickyFilterGambit.php diff --git a/extensions/sticky/extend.php b/extensions/sticky/extend.php index a6687305f..a540764e7 100644 --- a/extensions/sticky/extend.php +++ b/extensions/sticky/extend.php @@ -10,12 +10,16 @@ use Flarum\Api\Controller\ListDiscussionsController; use Flarum\Api\Serializer\DiscussionSerializer; use Flarum\Discussion\Event\Saving; +use Flarum\Discussion\Filter\DiscussionFilterer; +use Flarum\Discussion\Search\DiscussionSearcher; use Flarum\Extend; use Flarum\Sticky\Event\DiscussionWasStickied; use Flarum\Sticky\Event\DiscussionWasUnstickied; use Flarum\Sticky\Listener; use Flarum\Sticky\Listener\SaveStickyToDatabase; use Flarum\Sticky\Post\DiscussionStickiedPost; +use Flarum\Sticky\PinStickiedDiscussionsToTop; +use Flarum\Sticky\Query\StickyFilterGambit; use Illuminate\Contracts\Events\Dispatcher; return [ @@ -47,8 +51,10 @@ return [ ->listen(DiscussionWasStickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasStickied']) ->listen(DiscussionWasUnstickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasUnstickied']), - function (Dispatcher $events) { - // Replace with Filter extender before stable - $events->subscribe(Listener\PinStickiedDiscussionsToTop::class); - }, + (new Extend\Filter(DiscussionFilterer::class)) + ->addFilter(StickyFilterGambit::class) + ->addFilterMutator(PinStickiedDiscussionsToTop::class), + + (new Extend\SimpleFlarumSearch(DiscussionSearcher::class)) + ->addGambit(StickyFilterGambit::class), ]; diff --git a/extensions/sticky/src/Gambit/StickyGambit.php b/extensions/sticky/src/Gambit/StickyGambit.php deleted file mode 100644 index f6d7606f6..000000000 --- a/extensions/sticky/src/Gambit/StickyGambit.php +++ /dev/null @@ -1,29 +0,0 @@ -getQuery()->where('is_sticky', ! $negate); - } -} diff --git a/extensions/sticky/src/Listener/PinStickiedDiscussionsToTop.php b/extensions/sticky/src/PinStickiedDiscussionsToTop.php similarity index 64% rename from extensions/sticky/src/Listener/PinStickiedDiscussionsToTop.php rename to extensions/sticky/src/PinStickiedDiscussionsToTop.php index 69fd678f0..9c7cf0328 100755 --- a/extensions/sticky/src/Listener/PinStickiedDiscussionsToTop.php +++ b/extensions/sticky/src/PinStickiedDiscussionsToTop.php @@ -7,48 +7,38 @@ * LICENSE file that was distributed with this source code. */ -namespace Flarum\Sticky\Listener; +namespace Flarum\Sticky; -use Flarum\Discussion\Event\Searching; -use Flarum\Event\ConfigureDiscussionGambits; -use Flarum\Sticky\Gambit\StickyGambit; -use Flarum\Tags\Gambit\TagGambit; -use Illuminate\Contracts\Events\Dispatcher; +use Flarum\Api\Controller\ListDiscussionsController; +use Flarum\Filter\FilterState; +use Flarum\Query\QueryCriteria; +use Flarum\Tags\Query\TagFilterGambit; class PinStickiedDiscussionsToTop { /** - * @param Dispatcher $events + * Used to get the default sort. + * + * @var ListDiscussionsController */ - public function subscribe(Dispatcher $events) - { - $events->listen(ConfigureDiscussionGambits::class, [$this, 'addStickyGambit']); - $events->listen(Searching::class, [$this, 'reorderSearch']); + protected $listDiscussionsController; + + public function __construct(ListDiscussionsController $listDiscussionsController) { + $this->listDiscussionsController = $listDiscussionsController; } - /** - * @param ConfigureDiscussionGambits $event - */ - public function addStickyGambit(ConfigureDiscussionGambits $event) + public function __invoke(FilterState $filterState, QueryCriteria $criteria) { - $event->gambits->add(StickyGambit::class); - } - - /** - * @param Searching $event - */ - public function reorderSearch(Searching $event) - { - if ($event->criteria->sort === null) { - $search = $event->search; - $query = $search->getQuery(); + // TODO: This should take modifications by extensions into account. + if ($criteria->sort == $this->listDiscussionsController->sort) { + $query = $filterState->getQuery(); // If we are viewing a specific tag, then pin all stickied // discussions to the top no matter what. - $gambits = $search->getActiveGambits(); + $filters = $filterState->getActiveFilters(); - if ($count = count($gambits)) { - if ($count === 1 && $gambits[0] instanceof TagGambit) { + if ($count = count($filters)) { + if ($count === 1 && $filters[0] instanceof TagFilterGambit) { if (! is_array($query->orders)) { $query->orders = []; } @@ -74,14 +64,14 @@ class PinStickiedDiscussionsToTop ->selectRaw(1) ->from('discussion_user as sticky') ->whereColumn('sticky.discussion_id', 'id') - ->where('sticky.user_id', '=', $search->getActor()->id) + ->where('sticky.user_id', '=', $filterState->getActor()->id) ->whereColumn('sticky.last_read_post_number', '>=', 'last_post_number'); // Add the bindings manually (rather than as the second // argument in orderByRaw) for now due to a bug in Laravel which // would add the bindings in the wrong order. $query->orderByRaw('is_sticky and not exists ('.$read->toSql().') and last_posted_at > ? desc') - ->addBinding(array_merge($read->getBindings(), [$search->getActor()->read_time ?: 0]), 'union'); + ->addBinding(array_merge($read->getBindings(), [$filterState->getActor()->read_time ?: 0]), 'union'); $query->unionOrders = array_merge($query->unionOrders, $query->orders); $query->unionLimit = $query->limit; diff --git a/extensions/sticky/src/Query/StickyFilterGambit.php b/extensions/sticky/src/Query/StickyFilterGambit.php new file mode 100644 index 000000000..4a45e3f82 --- /dev/null +++ b/extensions/sticky/src/Query/StickyFilterGambit.php @@ -0,0 +1,43 @@ +constrain($search->getQuery(), $negate); + } + + public function getFilterKey(): string + { + return 'sticky'; + } + + public function filter(FilterState $filterState, string $filterValue, $negate) + { + $this->constrain($filterState->getQuery(), $negate); + } + + protected function constrain(Builder $query, bool $negate) { + $query->where('is_sticky', !$negate); + } +}