1
0
mirror of https://github.com/flarum/core.git synced 2025-10-18 18:26:07 +02:00

Use filterer for ListPostsController (#2479)

This commit is contained in:
Alexander Skvortsov
2021-02-28 14:06:07 -05:00
committed by GitHub
parent ea840ba594
commit 458a5cc6be
12 changed files with 483 additions and 107 deletions

View File

@@ -0,0 +1,45 @@
<?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\Post\Filter;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\User\UserRepository;
class AuthorFilter implements FilterInterface
{
/**
* @var \Flarum\User\UserRepository
*/
protected $users;
/**
* @param \Flarum\User\UserRepository $users
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
public function getFilterKey(): string
{
return 'author';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$usernames = trim($filterValue, '"');
$usernames = explode(',', $usernames);
$ids = $this->users->query()->whereIn('username', $usernames)->pluck('id');
$filterState->getQuery()->whereIn('posts.user_id', $ids, 'and', $negate);
}
}

View File

@@ -0,0 +1,28 @@
<?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\Post\Filter;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
class DiscussionFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'discussion';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$discussionId = trim($filterValue, '"');
$filterState->getQuery()->where('posts.discussion_id', $negate ? '!=' : '=', $discussionId);
}
}

View File

@@ -0,0 +1,29 @@
<?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\Post\Filter;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
class IdFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'id';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$idString = trim($filterValue, '"');
$ids = explode(',', $idString);
$filterState->getQuery()->whereIn('posts.id', $ids, 'and', $negate);
}
}

View File

@@ -0,0 +1,28 @@
<?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\Post\Filter;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
class NumberFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'number';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$number = trim($filterValue, '"');
$filterState->getQuery()->where('posts.number', $negate ? '!=' : '=', $number);
}
}

View File

@@ -0,0 +1,40 @@
<?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\Post\Filter;
use Flarum\Filter\AbstractFilterer;
use Flarum\Post\PostRepository;
use Flarum\User\User;
use Illuminate\Database\Eloquent\Builder;
class PostFilterer extends AbstractFilterer
{
/**
* @var PostRepository
*/
protected $posts;
/**
* @param PostRepository $posts
* @param array $filters
* @param array $filterMutators
*/
public function __construct(PostRepository $posts, array $filters, array $filterMutators)
{
parent::__construct($filters, $filterMutators);
$this->posts = $posts;
}
protected function getQuery(User $actor): Builder
{
return $this->posts->query()->whereVisibleTo($actor);
}
}

View File

@@ -0,0 +1,28 @@
<?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\Post\Filter;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
class TypeFilter implements FilterInterface
{
public function getFilterKey(): string
{
return 'type';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
$type = trim($filterValue, '"');
$filterState->getQuery()->where('posts.type', $negate ? '!=' : '=', $type);
}
}

View File

@@ -80,32 +80,6 @@ class PostRepository
return $query->get();
}
/**
* Find posts by their IDs, optionally making sure they are visible to a
* certain user.
*
* @param array $ids
* @param \Flarum\User\User|null $actor
* @return \Illuminate\Database\Eloquent\Collection
*/
public function findByIds(array $ids, User $actor = null)
{
$posts = $this->queryIds($ids, $actor)->get();
$posts = $posts->sort(function ($a, $b) use ($ids) {
$aPos = array_search($a->id, $ids);
$bPos = array_search($b->id, $ids);
if ($aPos === $bPos) {
return 0;
}
return $aPos < $bPos ? -1 : 1;
});
return $posts;
}
/**
* Filter a list of post IDs to only include posts that are visible to a
* certain user.