Skip to content

Commit

Permalink
refactor: merge filterer and searcher concepts (#3892)
Browse files Browse the repository at this point in the history
* chore: drop remaining backend regex gambits
* refactor: merge filterer & searcher concept
* refactor: adapt extenders
* refactor: no longer need to push gambits to `q`
* refactor: filters to gambits
* refactor: drop shred `Query` namespace
* Apply fixes from StyleCI
* chore: cleanup
* Apply fixes from StyleCI
  • Loading branch information
SychO9 authored Sep 26, 2023
1 parent 336ce00 commit 1a01c97
Show file tree
Hide file tree
Showing 103 changed files with 777 additions and 1,677 deletions.
8 changes: 4 additions & 4 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Likes\Query\LikedByFilter;
use Flarum\Likes\Query\LikedFilter;
use Flarum\Post\Filter\PostFilterer;
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(PostFilterer::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
12 changes: 6 additions & 6 deletions extensions/likes/src/Query/LikedByFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Flarum\Likes\Query;

use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

class LikedByFilter implements FilterInterface
{
Expand All @@ -22,11 +22,11 @@ public function getFilterKey(): string
return 'likedBy';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$likedId = $this->asInt($filterValue);
$likedId = $this->asInt($value);

$filterState
$state
->getQuery()
->whereIn('id', function ($query) use ($likedId, $negate) {
$query->select('post_id')
Expand Down
12 changes: 6 additions & 6 deletions extensions/likes/src/Query/LikedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Flarum\Likes\Query;

use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

class LikedFilter implements FilterInterface
{
Expand All @@ -22,11 +22,11 @@ public function getFilterKey(): string
return 'liked';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$likedId = $this->asString($filterValue);
$likedId = $this->asString($value);

$filterState
$state
->getQuery()
->whereIn('id', function ($query) use ($likedId) {
$query->select('user_id')
Expand Down
8 changes: 2 additions & 6 deletions extensions/lock/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
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\Lock\Access;
use Flarum\Lock\Event\DiscussionWasLocked;
use Flarum\Lock\Event\DiscussionWasUnlocked;
use Flarum\Lock\Filter\LockedFilter;
use Flarum\Lock\Listener;
use Flarum\Lock\Notification\DiscussionLockedBlueprint;
use Flarum\Lock\Post\DiscussionLockedPost;
use Flarum\Lock\Query\LockedFilterGambit;

return [
(new Extend\Frontend('forum'))
Expand Down Expand Up @@ -57,9 +56,6 @@
(new Extend\Policy())
->modelPolicy(Discussion::class, Access\DiscussionPolicy::class),

(new Extend\Filter(DiscussionFilterer::class))
->addFilter(LockedFilterGambit::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(LockedFilterGambit::class),
->addFilter(LockedFilter::class),
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class LockedGambit implements IGambit {
[key]: true,
};
}

filterKey(): string {
return 'locked';
}

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:locked`;
}
}
32 changes: 32 additions & 0 deletions extensions/lock/src/Filter/LockedFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Lock\Filter;

use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;
use Illuminate\Database\Query\Builder;

class LockedFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'locked';
}

public function filter(SearchState $state, string|array $value, bool $negate): void
{
$this->constrain($state->getQuery(), $negate);
}

protected function constrain(Builder $query, bool $negate): void
{
$query->where('is_locked', ! $negate);
}
}
44 changes: 0 additions & 44 deletions extensions/lock/src/Query/LockedFilterGambit.php

This file was deleted.

4 changes: 2 additions & 2 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Post\Event\Revised;
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Tags\Api\Serializer\TagSerializer;
use Flarum\Tags\Tag;
Expand Down Expand Up @@ -115,7 +115,7 @@
->listen(Hidden::class, Listener\UpdateMentionsMetadataWhenInvisible::class)
->listen(Deleted::class, Listener\UpdateMentionsMetadataWhenInvisible::class),

(new Extend\Filter(PostFilterer::class))
(new Extend\SimpleFlarumSearch(PostSearcher::class))
->addFilter(Filter\MentionedFilter::class)
->addFilter(Filter\MentionedPostFilter::class),

Expand Down
12 changes: 6 additions & 6 deletions extensions/mentions/src/Filter/MentionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Flarum\Mentions\Filter;

use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Filter\ValidateFilterTrait;
use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\Search\ValidateFilterTrait;

class MentionedFilter implements FilterInterface
{
Expand All @@ -22,11 +22,11 @@ public function getFilterKey(): string
return 'mentioned';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$mentionedId = $this->asInt($filterValue);
$mentionedId = $this->asInt($value);

$filterState
$state
->getQuery()
->join('post_mentions_user', 'posts.id', '=', 'post_mentions_user.post_id')
->where('post_mentions_user.mentions_user_id', $negate ? '!=' : '=', $mentionedId);
Expand Down
10 changes: 5 additions & 5 deletions extensions/mentions/src/Filter/MentionedPostFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Flarum\Mentions\Filter;

use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;

class MentionedPostFilter implements FilterInterface
{
Expand All @@ -19,11 +19,11 @@ public function getFilterKey(): string
return 'mentionedPost';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
$mentionedId = trim($filterValue, '"');
$mentionedId = trim($value, '"');

$filterState
$state
->getQuery()
->join('post_mentions_post', 'posts.id', '=', 'post_mentions_post.post_id')
->where('post_mentions_post.mentions_post_id', $negate ? '!=' : '=', $mentionedId);
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
19 changes: 5 additions & 14 deletions extensions/nicknames/src/NicknameFullTextGambit.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@

namespace Flarum\Nicknames;

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

use Flarum\Search\GambitInterface;
use Flarum\Search\AbstractFulltextFilter;
use Flarum\Search\SearchState;
use Flarum\User\UserRepository;
use Illuminate\Database\Eloquent\Builder;

class NicknameFullTextGambit implements GambitInterface
class NicknameFullTextGambit extends AbstractFulltextFilter
{
public function __construct(
protected UserRepository $users
Expand All @@ -37,14 +30,12 @@ private function getUserSearchSubQuery(string $searchValue): Builder
->orWhere('nickname', 'like', "{$searchValue}%");
}

public function apply(SearchState $search, string $bit): bool
public function search(SearchState $state, string $query): void
{
$search->getQuery()
$state->getQuery()
->whereIn(
'id',
$this->getUserSearchSubQuery($bit)
$this->getUserSearchSubQuery($query)
);

return true;
}
}
10 changes: 3 additions & 7 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 All @@ -20,7 +19,7 @@
use Flarum\Sticky\Listener\SaveStickyToDatabase;
use Flarum\Sticky\PinStickiedDiscussionsToTop;
use Flarum\Sticky\Post\DiscussionStickiedPost;
use Flarum\Sticky\Query\StickyFilterGambit;
use Flarum\Sticky\Query\StickyFilter;

return [
(new Extend\Frontend('forum'))
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(StickyFilterGambit::class)
->addFilterMutator(PinStickiedDiscussionsToTop::class),

(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(StickyFilterGambit::class),
->addFilter(StickyFilter::class)
->addSearchMutator(PinStickiedDiscussionsToTop::class),
];
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ export default class StickyGambit implements IGambit {
[key]: true,
};
}

filterKey(): string {
return 'sticky';
}

fromFilter(value: string, negate: boolean): string {
return `${negate ? '-' : ''}is:sticky`;
}
}
Loading

0 comments on commit 1a01c97

Please sign in to comment.