Skip to content

Commit

Permalink
refactor: adapt extenders
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Sep 22, 2023
1 parent 4b23928 commit e37f7d5
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 141 deletions.
6 changes: 3 additions & 3 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down Expand Up @@ -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())
Expand Down
5 changes: 1 addition & 4 deletions extensions/lock/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
2 changes: 1 addition & 1 deletion extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
2 changes: 1 addition & 1 deletion extensions/nicknames/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 2 additions & 6 deletions extensions/sticky/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
];
8 changes: 2 additions & 6 deletions extensions/subscriptions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 1 addition & 5 deletions extensions/suspend/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'),
Expand Down
12 changes: 4 additions & 8 deletions extensions/tags/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
82 changes: 0 additions & 82 deletions framework/core/src/Extend/Filter.php

This file was deleted.

51 changes: 27 additions & 24 deletions framework/core/src/Extend/SimpleFlarumSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

/**
Expand All @@ -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<AbstractRegexGambit> $gambitClass: The ::class attribute of the gambit you are adding.
* This gambit must extend \Flarum\Search\AbstractRegexGambit
* @param class-string<FilterInterface> $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<GambitInterface> $gambitClass: The ::class attribute of the full test gambit you are adding.
* This gambit must implement \Flarum\Search\GambitInterface
* @param class-string<AbstractFulltextFilter> $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
*
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
Expand Down

0 comments on commit e37f7d5

Please sign in to comment.