Skip to content

Commit

Permalink
refactor: merge filterer & searcher concept
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Sep 22, 2023
1 parent ad3e2a1 commit 4b23928
Show file tree
Hide file tree
Showing 71 changed files with 409 additions and 1,167 deletions.
4 changes: 2 additions & 2 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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\User;
Expand Down Expand Up @@ -76,7 +76,7 @@
->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class)
->subscribe(Listener\SaveLikesToDatabase::class),

(new Extend\Filter(PostFilterer::class))
(new Extend\Filter(PostSearcher::class))
->addFilter(LikedByFilter::class),

(new Extend\Filter(UserFilterer::class))
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: 4 additions & 4 deletions extensions/lock/src/Filter/LockedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Flarum\Lock\Filter;

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

class LockedFilter implements FilterInterface
Expand All @@ -20,9 +20,9 @@ public function getFilterKey(): string
return 'locked';
}

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

protected function constrain(Builder $query, bool $negate): void
Expand Down
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\Filter(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
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;
}
}
14 changes: 7 additions & 7 deletions extensions/sticky/src/PinStickiedDiscussionsToTop.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

namespace Flarum\Sticky;

use Flarum\Filter\FilterState;
use Flarum\Search\SearchState;
use Flarum\Query\QueryCriteria;
use Flarum\Tags\Filter\TagFilter;

class PinStickiedDiscussionsToTop
{
public function __invoke(FilterState $filterState, QueryCriteria $criteria): void
public function __invoke(SearchState $state, QueryCriteria $criteria): void
{
if ($criteria->sortIsDefault) {
$query = $filterState->getQuery();
if ($criteria->sortIsDefault && ! $state->isFulltextSearch()) {
$query = $state->getQuery();

// If we are viewing a specific tag, then pin all stickied
// discussions to the top no matter what.
$filters = $filterState->getActiveFilters();
$filters = $state->getActiveFilters();

if ($count = count($filters)) {
if ($count === 1 && $filters[0] instanceof TagFilter) {
Expand Down Expand Up @@ -51,14 +51,14 @@ public function __invoke(FilterState $filterState, QueryCriteria $criteria): voi
->selectRaw('1')
->from('discussion_user as sticky')
->whereColumn('sticky.discussion_id', 'id')
->where('sticky.user_id', '=', $filterState->getActor()->id)
->where('sticky.user_id', '=', $state->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(), [$filterState->getActor()->marked_all_as_read_at ?: 0]), 'union');
->addBinding(array_merge($read->getBindings(), [$state->getActor()->marked_all_as_read_at ?: 0]), 'union');

$query->unionOrders = array_merge($query->unionOrders, $query->orders);
$query->unionLimit = $query->limit;
Expand Down
8 changes: 4 additions & 4 deletions extensions/sticky/src/Query/StickyFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace Flarum\Sticky\Query;

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

class StickyFilter implements FilterInterface
Expand All @@ -20,9 +20,9 @@ public function getFilterKey(): string
return 'sticky';
}

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

protected function constrain(Builder $query, bool $negate): void
Expand Down
14 changes: 7 additions & 7 deletions extensions/subscriptions/src/Filter/SubscriptionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

namespace Flarum\Subscriptions\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;
use Flarum\User\User;
use Illuminate\Database\Query\Builder;

Expand All @@ -24,13 +24,13 @@ public function getFilterKey(): string
return 'subscription';
}

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

preg_match('/^(follow|ignor)(?:ing|ed)$/i', $filterValue, $matches);
preg_match('/^(follow|ignor)(?:ing|ed)$/i', $value, $matches);

$this->constrain($filterState->getQuery(), $filterState->getActor(), $matches[1], $negate);
$this->constrain($state->getQuery(), $state->getActor(), $matches[1], $negate);
}

protected function constrain(Builder $query, User $actor, string $subscriptionType, bool $negate): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@

namespace Flarum\Subscriptions;

use Flarum\Filter\FilterState;
use Flarum\Query\QueryCriteria;
use Flarum\Search\SearchState;

class HideIgnoredFromAllDiscussionsPage
{
public function __invoke(FilterState $filterState, QueryCriteria $criteria): void
public function __invoke(SearchState $state, QueryCriteria $criteria): void
{
// We only want to hide on the "all discussions" page.
if (count($filterState->getActiveFilters()) === 0) {
if (count($state->getActiveFilters()) === 0 && ! $state->isFulltextSearch()) {
// TODO: might be better as `id IN (subquery)`?
$actor = $filterState->getActor();
$filterState->getQuery()->whereNotExists(function ($query) use ($actor) {
$actor = $state->getActor();
$state->getQuery()->whereNotExists(function ($query) use ($actor) {
$query->selectRaw(1)
->from('discussion_user')
->whereColumn('discussions.id', 'discussion_id')
Expand Down
10 changes: 5 additions & 5 deletions extensions/suspend/src/Query/SuspendedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace Flarum\Suspend\Query;

use Carbon\Carbon;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Search\FilterInterface;
use Flarum\Search\SearchState;
use Flarum\User\Guest;
use Flarum\User\UserRepository;
use Illuminate\Database\Query\Builder;
Expand All @@ -28,13 +28,13 @@ public function getFilterKey(): string
return 'suspended';
}

public function filter(FilterState $filterState, string|array $filterValue, bool $negate): void
public function filter(SearchState $state, string|array $value, bool $negate): void
{
if (! $filterState->getActor()->can('suspend', new Guest())) {
if (! $state->getActor()->can('suspend', new Guest())) {
return;
}

$this->constrain($filterState->getQuery(), $negate);
$this->constrain($state->getQuery(), $negate);
}

protected function constrain(Builder $query, bool $negate): void
Expand Down
4 changes: 2 additions & 2 deletions extensions/tags/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Flarum\Extend;
use Flarum\Flags\Api\Controller\ListFlagsController;
use Flarum\Http\RequestUtil;
use Flarum\Post\Filter\PostFilterer;
use Flarum\Post\Filter\PostSearcher;
use Flarum\Post\Post;
use Flarum\Tags\Access;
use Flarum\Tags\Api\Controller;
Expand Down Expand Up @@ -135,7 +135,7 @@
->listen(DiscussionWasTagged::class, Listener\CreatePostWhenTagsAreChanged::class)
->subscribe(Listener\UpdateTagMetadata::class),

(new Extend\Filter(PostFilterer::class))
(new Extend\Filter(PostSearcher::class))
->addFilter(PostTagFilter::class),

(new Extend\Filter(DiscussionFilterer::class))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

namespace Flarum\Tags\Filter;

use Flarum\Filter\FilterState;
use Flarum\Query\QueryCriteria;
use Flarum\Search\SearchState;
use Flarum\Tags\Tag;

class HideHiddenTagsFromAllDiscussionsPage
{
public function __invoke(FilterState $filter, QueryCriteria $queryCriteria): void
public function __invoke(SearchState $state, QueryCriteria $queryCriteria): void
{
if (count($filter->getActiveFilters()) > 0) {
if (count($state->getActiveFilters()) > 0 || $state->isFulltextSearch()) {
return;
}

$filter->getQuery()->whereNotIn('discussions.id', function ($query) {
$state->getQuery()->whereNotIn('discussions.id', function ($query) {
return $query->select('discussion_id')
->from('discussion_tag')
->whereIn('tag_id', Tag::where('is_hidden', 1)->pluck('id'));
Expand Down
Loading

0 comments on commit 4b23928

Please sign in to comment.