Update for search/filter split

This commit is contained in:
Alexander Skvortsov 2021-03-13 15:49:23 -05:00
parent 5b312bc280
commit 6eb2c9e574
2 changed files with 37 additions and 23 deletions

View File

@ -9,7 +9,6 @@
use Flarum\Api\Serializer\BasicUserSerializer; use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\UserSerializer; use Flarum\Api\Serializer\UserSerializer;
use Flarum\Event\ConfigureUserGambits;
use Flarum\Extend; use Flarum\Extend;
use Flarum\Suspend\Access\UserPolicy; use Flarum\Suspend\Access\UserPolicy;
use Flarum\Suspend\AddUserSuspendAttributes; use Flarum\Suspend\AddUserSuspendAttributes;
@ -19,10 +18,11 @@ use Flarum\Suspend\Listener;
use Flarum\Suspend\Notification\UserSuspendedBlueprint; use Flarum\Suspend\Notification\UserSuspendedBlueprint;
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint; use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
use Flarum\Suspend\RevokeAccessFromSuspendedUsers; use Flarum\Suspend\RevokeAccessFromSuspendedUsers;
use Flarum\Suspend\Search\Gambit\SuspendedGambit; use Flarum\Suspend\Query\SuspendedFilterGambit;
use Flarum\User\Event\Saving; use Flarum\User\Event\Saving;
use Flarum\User\Filter\UserFilterer;
use Flarum\User\Search\UserSearcher;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
return [ return [
(new Extend\Frontend('forum')) (new Extend\Frontend('forum'))
@ -56,9 +56,9 @@ return [
(new Extend\User()) (new Extend\User())
->permissionGroups(RevokeAccessFromSuspendedUsers::class), ->permissionGroups(RevokeAccessFromSuspendedUsers::class),
function (Dispatcher $events) { (new Extend\Filter(UserFilterer::class))
$events->listen(ConfigureUserGambits::class, function (ConfigureUserGambits $event) { ->addFilter(SuspendedFilterGambit::class),
$event->gambits->add(SuspendedGambit::class);
}); (new Extend\SimpleFlarumSearch(UserSearcher::class))
} ->addGambit(SuspendedFilterGambit::class)
]; ];

View File

@ -7,23 +7,19 @@
* LICENSE file that was distributed with this source code. * LICENSE file that was distributed with this source code.
*/ */
namespace Flarum\Suspend\Search\Gambit; namespace Flarum\Suspend\Query;
use Carbon\Carbon; use Carbon\Carbon;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Search\AbstractRegexGambit; use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch; use Flarum\Search\SearchState;
use Flarum\User\Guest; use Flarum\User\Guest;
use Flarum\User\Search\UserSearch;
use Flarum\User\UserRepository; use Flarum\User\UserRepository;
use LogicException; use Illuminate\Database\Query\Builder;
class SuspendedGambit extends AbstractRegexGambit class SuspendedFilterGambit extends AbstractRegexGambit implements FilterInterface
{ {
/**
* {@inheritdoc}
*/
protected $pattern = 'is:suspended';
/** /**
* @var \Flarum\User\UserRepository * @var \Flarum\User\UserRepository
*/ */
@ -37,10 +33,14 @@ class SuspendedGambit extends AbstractRegexGambit
$this->users = $users; $this->users = $users;
} }
protected function getGambitPattern() {
return 'is:suspended';
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function apply(AbstractSearch $search, $bit) public function apply(SearchState $search, $bit)
{ {
if (! $search->getActor()->can('suspend', new Guest())) { if (! $search->getActor()->can('suspend', new Guest())) {
return false; return false;
@ -52,13 +52,27 @@ class SuspendedGambit extends AbstractRegexGambit
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function conditions(AbstractSearch $search, array $matches, $negate) protected function conditions(SearchState $search, array $matches, $negate)
{ {
if (! $search instanceof UserSearch) { $this->constrain($search->getQuery(), $negate);
throw new LogicException('This gambit can only be applied on a DiscussionSearch'); }
public function getFilterKey(): string {
return 'suspended';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
if (! $filterState->getActor()->can('suspend', new Guest())) {
return false;
} }
$search->getQuery()->where(function ($query) use ($negate) { $this->constrain($filterState->getQuery(), $negate);
}
protected function constrain(Builder $query, bool $negate)
{
$query->where(function ($query) use ($negate) {
if ($negate) { if ($negate) {
$query->where('suspended_until', null)->orWhere('suspended_until', '<', Carbon::now()); $query->where('suspended_until', null)->orWhere('suspended_until', '<', Carbon::now());
} else { } else {