From e37f7d589a672130778997bb2ef51dcc35a519f0 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Fri, 22 Sep 2023 20:26:33 +0100 Subject: [PATCH] refactor: adapt extenders --- extensions/likes/extend.php | 6 +- extensions/lock/extend.php | 5 +- extensions/mentions/extend.php | 2 +- extensions/nicknames/extend.php | 2 +- extensions/sticky/extend.php | 8 +- extensions/subscriptions/extend.php | 8 +- extensions/suspend/extend.php | 6 +- extensions/tags/extend.php | 12 +-- framework/core/src/Extend/Filter.php | 82 ------------------- .../core/src/Extend/SimpleFlarumSearch.php | 51 ++++++------ .../extenders/SimpleFlarumSearchTest.php | 2 +- 11 files changed, 43 insertions(+), 141 deletions(-) delete mode 100644 framework/core/src/Extend/Filter.php diff --git a/extensions/likes/extend.php b/extensions/likes/extend.php index 138b3bc9fd..434df72cdc 100644 --- a/extensions/likes/extend.php +++ b/extensions/likes/extend.php @@ -21,7 +21,7 @@ use Flarum\Likes\Query\LikedFilter; use Flarum\Post\Filter\PostSearcher; use Flarum\Post\Post; -use Flarum\User\Filter\UserFilterer; +use Flarum\User\Search\UserSearcher; use Flarum\User\User; return [ @@ -76,10 +76,10 @@ ->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class) ->subscribe(Listener\SaveLikesToDatabase::class), - (new Extend\Filter(PostSearcher::class)) + (new Extend\SimpleFlarumSearch(PostSearcher::class)) ->addFilter(LikedByFilter::class), - (new Extend\Filter(UserFilterer::class)) + (new Extend\SimpleFlarumSearch(UserSearcher::class)) ->addFilter(LikedFilter::class), (new Extend\Settings()) diff --git a/extensions/lock/extend.php b/extensions/lock/extend.php index 99e9e2d80f..e2cc6035c3 100644 --- a/extensions/lock/extend.php +++ b/extensions/lock/extend.php @@ -57,9 +57,6 @@ (new Extend\Policy()) ->modelPolicy(Discussion::class, Access\DiscussionPolicy::class), - (new Extend\Filter(DiscussionFilterer::class)) - ->addFilter(LockedFilter::class), - (new Extend\SimpleFlarumSearch(DiscussionSearcher::class)) - ->addGambit(LockedFilter::class), + ->addFilter(LockedFilter::class), ]; diff --git a/extensions/mentions/extend.php b/extensions/mentions/extend.php index 19d04a87c7..20c7798eff 100644 --- a/extensions/mentions/extend.php +++ b/extensions/mentions/extend.php @@ -115,7 +115,7 @@ ->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class) ->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class), - (new Extend\Filter(PostSearcher::class)) + (new Extend\SimpleFlarumSearch(PostSearcher::class)) ->addFilter(Filter\MentionedFilter::class) ->addFilter(Filter\MentionedPostFilter::class), diff --git a/extensions/nicknames/extend.php b/extensions/nicknames/extend.php index 99fb7ab7bf..853499e099 100644 --- a/extensions/nicknames/extend.php +++ b/extensions/nicknames/extend.php @@ -52,7 +52,7 @@ ->configure(AddNicknameValidation::class), (new Extend\SimpleFlarumSearch(UserSearcher::class)) - ->setFullTextGambit(NicknameFullTextGambit::class), + ->setFullTextFilter(NicknameFullTextGambit::class), (new Extend\Policy()) ->modelPolicy(User::class, UserPolicy::class), diff --git a/extensions/sticky/extend.php b/extensions/sticky/extend.php index b9d34a5042..61c55c9123 100644 --- a/extensions/sticky/extend.php +++ b/extensions/sticky/extend.php @@ -11,7 +11,6 @@ use Flarum\Api\Serializer\DiscussionSerializer; use Flarum\Discussion\Discussion; use Flarum\Discussion\Event\Saving; -use Flarum\Discussion\Filter\DiscussionFilterer; use Flarum\Discussion\Search\DiscussionSearcher; use Flarum\Extend; use Flarum\Sticky\Event\DiscussionWasStickied; @@ -54,10 +53,7 @@ ->listen(DiscussionWasStickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasStickied']) ->listen(DiscussionWasUnstickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasUnstickied']), - (new Extend\Filter(DiscussionFilterer::class)) - ->addFilter(StickyFilter::class) - ->addFilterMutator(PinStickiedDiscussionsToTop::class), - (new Extend\SimpleFlarumSearch(DiscussionSearcher::class)) - ->addGambit(StickyFilter::class), + ->addFilter(StickyFilter::class) + ->addSearchMutator(PinStickiedDiscussionsToTop::class), ]; diff --git a/extensions/subscriptions/extend.php b/extensions/subscriptions/extend.php index d6d98de26f..fa775356de 100644 --- a/extensions/subscriptions/extend.php +++ b/extensions/subscriptions/extend.php @@ -12,7 +12,6 @@ use Flarum\Approval\Event\PostWasApproved; use Flarum\Discussion\Discussion; use Flarum\Discussion\Event\Saving; -use Flarum\Discussion\Filter\DiscussionFilterer; use Flarum\Discussion\Search\DiscussionSearcher; use Flarum\Discussion\UserState; use Flarum\Extend; @@ -70,12 +69,9 @@ ->listen(Deleted::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class) ->listen(Posted::class, Listener\FollowAfterReply::class), - (new Extend\Filter(DiscussionFilterer::class)) - ->addFilter(SubscriptionFilter::class) - ->addFilterMutator(HideIgnoredFromAllDiscussionsPage::class), - (new Extend\SimpleFlarumSearch(DiscussionSearcher::class)) - ->addGambit(SubscriptionFilter::class), + ->addFilter(SubscriptionFilter::class) + ->addSearchMutator(HideIgnoredFromAllDiscussionsPage::class), (new Extend\User()) ->registerPreference('flarum-subscriptions.notify_for_all_posts', 'boolval', false), diff --git a/extensions/suspend/extend.php b/extensions/suspend/extend.php index 66fd7f5446..935ce1983b 100644 --- a/extensions/suspend/extend.php +++ b/extensions/suspend/extend.php @@ -20,7 +20,6 @@ use Flarum\Suspend\Query\SuspendedFilter; use Flarum\Suspend\RevokeAccessFromSuspendedUsers; use Flarum\User\Event\Saving; -use Flarum\User\Filter\UserFilterer; use Flarum\User\Search\UserSearcher; use Flarum\User\User; @@ -58,11 +57,8 @@ (new Extend\User()) ->permissionGroups(RevokeAccessFromSuspendedUsers::class), - (new Extend\Filter(UserFilterer::class)) - ->addFilter(SuspendedFilter::class), - (new Extend\SimpleFlarumSearch(UserSearcher::class)) - ->addGambit(SuspendedFilter::class), + ->addFilter(SuspendedFilter::class), (new Extend\View()) ->namespace('flarum-suspend', __DIR__.'/views'), diff --git a/extensions/tags/extend.php b/extensions/tags/extend.php index cea7cdbc11..cb82bc7128 100644 --- a/extensions/tags/extend.php +++ b/extensions/tags/extend.php @@ -13,7 +13,6 @@ use Flarum\Api\Serializer\ForumSerializer; use Flarum\Discussion\Discussion; use Flarum\Discussion\Event\Saving; -use Flarum\Discussion\Filter\DiscussionFilterer; use Flarum\Discussion\Search\DiscussionSearcher; use Flarum\Extend; use Flarum\Flags\Api\Controller\ListFlagsController; @@ -135,18 +134,15 @@ ->listen(DiscussionWasTagged::class, Listener\CreatePostWhenTagsAreChanged::class) ->subscribe(Listener\UpdateTagMetadata::class), - (new Extend\Filter(PostSearcher::class)) + (new Extend\SimpleFlarumSearch(PostSearcher::class)) ->addFilter(PostTagFilter::class), - (new Extend\Filter(DiscussionFilterer::class)) - ->addFilter(TagFilter::class) - ->addFilterMutator(HideHiddenTagsFromAllDiscussionsPage::class), - (new Extend\SimpleFlarumSearch(DiscussionSearcher::class)) - ->addGambit(TagFilter::class), + ->addFilter(TagFilter::class) + ->addSearchMutator(HideHiddenTagsFromAllDiscussionsPage::class), (new Extend\SimpleFlarumSearch(TagSearcher::class)) - ->setFullTextGambit(FullTextGambit::class), + ->setFullTextFilter(FullTextGambit::class), (new Extend\ModelUrl(Tag::class)) ->addSlugDriver('default', Utf8SlugDriver::class), diff --git a/framework/core/src/Extend/Filter.php b/framework/core/src/Extend/Filter.php deleted file mode 100644 index cfc54c4760..0000000000 --- a/framework/core/src/Extend/Filter.php +++ /dev/null @@ -1,82 +0,0 @@ - $filtererClass: The ::class attribute of the filterer to extend. - */ - public function __construct( - private readonly string $filtererClass - ) { - } - - /** - * Add a filter to run when the filtererClass is filtered. - * - * @param string $filterClass: The ::class attribute of the filter you are adding. - * @return self - */ - public function addFilter(string $filterClass): self - { - $this->filters[] = $filterClass; - - return $this; - } - - /** - * Add a callback through which to run all filter queries after filters have been applied. - * - * @param (callable(FilterState $filter, QueryCriteria $criteria): void)|class-string $callback - * - * The callback can be a closure or an invokable class, and should accept: - * - Flarum\Filter\FilterState $filter - * - Flarum\Query\QueryCriteria $criteria - * - * The callable should return void. - * - * @return self - */ - public function addFilterMutator(callable|string $callback): self - { - $this->filterMutators[] = $callback; - - return $this; - } - - public function extend(Container $container, Extension $extension = null): void - { - $container->extend('flarum.filter.filters', function ($originalFilters) { - foreach ($this->filters as $filter) { - $originalFilters[$this->filtererClass][] = $filter; - } - - return $originalFilters; - }); - - $container->extend('flarum.filter.filter_mutators', function ($originalMutators) { - foreach ($this->filterMutators as $mutator) { - $originalMutators[$this->filtererClass][] = $mutator; - } - - return $originalMutators; - }); - } -} diff --git a/framework/core/src/Extend/SimpleFlarumSearch.php b/framework/core/src/Extend/SimpleFlarumSearch.php index 0e1924c37f..bfd4455177 100644 --- a/framework/core/src/Extend/SimpleFlarumSearch.php +++ b/framework/core/src/Extend/SimpleFlarumSearch.php @@ -11,16 +11,16 @@ use Flarum\Extension\Extension; use Flarum\Query\QueryCriteria; -use Flarum\Search\AbstractRegexGambit; +use Flarum\Search\AbstractFulltextFilter; use Flarum\Search\AbstractSearcher; -use Flarum\Search\GambitInterface; +use Flarum\Search\FilterInterface; use Flarum\Search\SearchState; use Illuminate\Contracts\Container\Container; class SimpleFlarumSearch implements ExtenderInterface { - private ?string $fullTextGambit = null; - private array $gambits = []; + private ?string $fullTextFilter = null; + private array $filters = []; private array $searchMutators = []; /** @@ -33,35 +33,35 @@ public function __construct( } /** - * Add a gambit to this searcher. Gambits are used to filter search queries. + * Add a filter to this searcher. Filters are used to filter search queries. * - * @param class-string $gambitClass: The ::class attribute of the gambit you are adding. - * This gambit must extend \Flarum\Search\AbstractRegexGambit + * @param class-string $filterClass: The ::class attribute of the filter you are adding. + * This filter must implement \Flarum\Search\FilterInterface * @return self */ - public function addGambit(string $gambitClass): self + public function addFilter(string $filterClass): self { - $this->gambits[] = $gambitClass; + $this->filters[] = $filterClass; return $this; } /** - * Set the full text gambit for this searcher. The full text gambit actually executes the search. + * Set the full text filter for this searcher. The full text filter actually executes the search. * - * @param class-string $gambitClass: The ::class attribute of the full test gambit you are adding. - * This gambit must implement \Flarum\Search\GambitInterface + * @param class-string $fulltextClass: The ::class attribute of the full test filter you are adding. + * This filter must implement \Flarum\Search\FilterInterface * @return self */ - public function setFullTextGambit(string $gambitClass): self + public function setFullTextFilter(string $fulltextClass): self { - $this->fullTextGambit = $gambitClass; + $this->fullTextFilter = $fulltextClass; return $this; } /** - * Add a callback through which to run all search queries after gambits have been applied. + * Add a callback through which to run all search queries after filters have been applied. * * @param (callable(SearchState $search, QueryCriteria $criteria): void)|class-string $callback * @@ -82,23 +82,26 @@ public function addSearchMutator(callable|string $callback): self public function extend(Container $container, Extension $extension = null): void { - if (! is_null($this->fullTextGambit)) { - $container->extend('flarum.simple_search.fulltext_gambits', function ($oldFulltextGambits) { - $oldFulltextGambits[$this->searcher] = $this->fullTextGambit; + if (! is_null($this->fullTextFilter)) { + $container->extend('flarum.simple_search.fulltext_filters', function (array $oldFulltextFilters) { + $oldFulltextFilters[$this->searcher] = $this->fullTextFilter; - return $oldFulltextGambits; + return $oldFulltextFilters; }); } - $container->extend('flarum.simple_search.gambits', function ($oldGambits) { - foreach ($this->gambits as $gambit) { - $oldGambits[$this->searcher][] = $gambit; + $container->extend('flarum.simple_search.filters', function (array $oldFilters) { + // We need the key to be set, even if there are no filters, so that the searcher is registered. + $oldFilters[$this->searcher] = $oldFilters[$this->searcher] ?? []; + + foreach ($this->filters as $filter) { + $oldFilters[$this->searcher][] = $filter; } - return $oldGambits; + return $oldFilters; }); - $container->extend('flarum.simple_search.search_mutators', function ($oldMutators) { + $container->extend('flarum.simple_search.search_mutators', function (array $oldMutators) { foreach ($this->searchMutators as $mutator) { $oldMutators[$this->searcher][] = $mutator; } diff --git a/framework/core/tests/integration/extenders/SimpleFlarumSearchTest.php b/framework/core/tests/integration/extenders/SimpleFlarumSearchTest.php index 095e13124c..cca743988f 100644 --- a/framework/core/tests/integration/extenders/SimpleFlarumSearchTest.php +++ b/framework/core/tests/integration/extenders/SimpleFlarumSearchTest.php @@ -96,7 +96,7 @@ public function works_as_expected_with_no_modifications() */ public function custom_full_text_gambit_has_effect_if_added() { - $this->extend((new Extend\SimpleFlarumSearch(DiscussionSearcher::class))->setFullTextGambit(NoResultFullTextFilter::class)); + $this->extend((new Extend\SimpleFlarumSearch(DiscussionSearcher::class))->setFullTextFilter(NoResultFullTextFilter::class)); $this->assertEquals('[]', json_encode($this->searchDiscussions('in text', 5))); }