mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 18:12:59 +08:00
Update for search/filter split
This commit is contained in:
parent
5b312bc280
commit
6eb2c9e574
|
@ -9,7 +9,6 @@
|
|||
|
||||
use Flarum\Api\Serializer\BasicUserSerializer;
|
||||
use Flarum\Api\Serializer\UserSerializer;
|
||||
use Flarum\Event\ConfigureUserGambits;
|
||||
use Flarum\Extend;
|
||||
use Flarum\Suspend\Access\UserPolicy;
|
||||
use Flarum\Suspend\AddUserSuspendAttributes;
|
||||
|
@ -19,10 +18,11 @@ use Flarum\Suspend\Listener;
|
|||
use Flarum\Suspend\Notification\UserSuspendedBlueprint;
|
||||
use Flarum\Suspend\Notification\UserUnsuspendedBlueprint;
|
||||
use Flarum\Suspend\RevokeAccessFromSuspendedUsers;
|
||||
use Flarum\Suspend\Search\Gambit\SuspendedGambit;
|
||||
use Flarum\Suspend\Query\SuspendedFilterGambit;
|
||||
use Flarum\User\Event\Saving;
|
||||
use Flarum\User\Filter\UserFilterer;
|
||||
use Flarum\User\Search\UserSearcher;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
|
||||
return [
|
||||
(new Extend\Frontend('forum'))
|
||||
|
@ -56,9 +56,9 @@ return [
|
|||
(new Extend\User())
|
||||
->permissionGroups(RevokeAccessFromSuspendedUsers::class),
|
||||
|
||||
function (Dispatcher $events) {
|
||||
$events->listen(ConfigureUserGambits::class, function (ConfigureUserGambits $event) {
|
||||
$event->gambits->add(SuspendedGambit::class);
|
||||
});
|
||||
}
|
||||
(new Extend\Filter(UserFilterer::class))
|
||||
->addFilter(SuspendedFilterGambit::class),
|
||||
|
||||
(new Extend\SimpleFlarumSearch(UserSearcher::class))
|
||||
->addGambit(SuspendedFilterGambit::class)
|
||||
];
|
||||
|
|
|
@ -7,23 +7,19 @@
|
|||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Suspend\Search\Gambit;
|
||||
namespace Flarum\Suspend\Query;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Filter\FilterInterface;
|
||||
use Flarum\Filter\FilterState;
|
||||
use Flarum\Search\AbstractRegexGambit;
|
||||
use Flarum\Search\AbstractSearch;
|
||||
use Flarum\Search\SearchState;
|
||||
use Flarum\User\Guest;
|
||||
use Flarum\User\Search\UserSearch;
|
||||
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
|
||||
*/
|
||||
|
@ -37,10 +33,14 @@ class SuspendedGambit extends AbstractRegexGambit
|
|||
$this->users = $users;
|
||||
}
|
||||
|
||||
protected function getGambitPattern() {
|
||||
return 'is:suspended';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function apply(AbstractSearch $search, $bit)
|
||||
public function apply(SearchState $search, $bit)
|
||||
{
|
||||
if (! $search->getActor()->can('suspend', new Guest())) {
|
||||
return false;
|
||||
|
@ -52,13 +52,27 @@ class SuspendedGambit extends AbstractRegexGambit
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function conditions(AbstractSearch $search, array $matches, $negate)
|
||||
protected function conditions(SearchState $search, array $matches, $negate)
|
||||
{
|
||||
if (! $search instanceof UserSearch) {
|
||||
throw new LogicException('This gambit can only be applied on a DiscussionSearch');
|
||||
$this->constrain($search->getQuery(), $negate);
|
||||
}
|
||||
|
||||
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) {
|
||||
$query->where('suspended_until', null)->orWhere('suspended_until', '<', Carbon::now());
|
||||
} else {
|
Loading…
Reference in New Issue
Block a user