-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search Filter Split, Use Same Controller (#2454)
- Loading branch information
1 parent
1c578a8
commit 023871e
Showing
42 changed files
with
1,663 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?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\Discussion\Filter; | ||
|
||
use Flarum\Filter\FilterInterface; | ||
use Flarum\Filter\FilterState; | ||
use Flarum\Search\AbstractRegexGambit; | ||
use Flarum\Search\SearchState; | ||
use Flarum\User\UserRepository; | ||
use Illuminate\Database\Query\Builder; | ||
|
||
class AuthorFilterGambit extends AbstractRegexGambit implements FilterInterface | ||
{ | ||
/** | ||
* @var \Flarum\User\UserRepository | ||
*/ | ||
protected $users; | ||
|
||
/** | ||
* @param \Flarum\User\UserRepository $users | ||
*/ | ||
public function __construct(UserRepository $users) | ||
{ | ||
$this->users = $users; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getGambitPattern() | ||
{ | ||
return 'author:(.+)'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function conditions(SearchState $search, array $matches, $negate) | ||
{ | ||
$this->constrain($search->getQuery(), $matches[1], $negate); | ||
} | ||
|
||
public function getFilterKey(): string | ||
{ | ||
return 'author'; | ||
} | ||
|
||
public function filter(FilterState $filterState, string $filterValue, bool $negate) | ||
{ | ||
$this->constrain($filterState->getQuery(), $filterValue, $negate); | ||
} | ||
|
||
protected function constrain(Builder $query, $rawUsernames, $negate) | ||
{ | ||
$usernames = trim($rawUsernames, '"'); | ||
$usernames = explode(',', $usernames); | ||
|
||
$ids = []; | ||
foreach ($usernames as $username) { | ||
$ids[] = $this->users->getIdForUsername($username); | ||
} | ||
|
||
$query->whereIn('discussions.user_id', $ids, 'and', $negate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?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\Discussion\Filter; | ||
|
||
use Flarum\Filter\FilterInterface; | ||
use Flarum\Filter\FilterState; | ||
use Flarum\Search\AbstractRegexGambit; | ||
use Flarum\Search\SearchState; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Support\Arr; | ||
|
||
class CreatedFilterGambit extends AbstractRegexGambit implements FilterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getGambitPattern() | ||
{ | ||
return 'created:(\d{4}\-\d\d\-\d\d)(\.\.(\d{4}\-\d\d\-\d\d))?'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function conditions(SearchState $search, array $matches, $negate) | ||
{ | ||
$this->constrain($search->getQuery(), Arr::get($matches, 1), Arr::get($matches, 3), $negate); | ||
} | ||
|
||
public function getFilterKey(): string | ||
{ | ||
return 'created'; | ||
} | ||
|
||
public function filter(FilterState $filterState, string $filterValue, bool $negate) | ||
{ | ||
preg_match('/^'.$this->getGambitPattern().'$/i', 'created:'.$filterValue, $matches); | ||
|
||
$this->constrain($filterState->getQuery(), Arr::get($matches, 1), Arr::get($matches, 3), $negate); | ||
} | ||
|
||
public function constrain(Builder $query, ?string $firstDate, ?string $secondDate, $negate) | ||
{ | ||
// If we've just been provided with a single YYYY-MM-DD date, then find | ||
// discussions that were started on that exact date. But if we've been | ||
// provided with a YYYY-MM-DD..YYYY-MM-DD range, then find discussions | ||
// that were started during that period. | ||
if (empty($secondDate)) { | ||
$query->whereDate('created_at', $negate ? '!=' : '=', $firstDate); | ||
} else { | ||
$query->whereBetween('created_at', [$firstDate, $secondDate], 'and', $negate); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\Discussion\Filter; | ||
|
||
use Flarum\Discussion\DiscussionRepository; | ||
use Flarum\Filter\AbstractFilterer; | ||
use Flarum\User\User; | ||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class DiscussionFilterer extends AbstractFilterer | ||
{ | ||
/** | ||
* @var DiscussionRepository | ||
*/ | ||
protected $discussions; | ||
|
||
/** | ||
* @param DiscussionRepository $discussions | ||
* @param array $filters | ||
* @param array $filterMutators | ||
*/ | ||
public function __construct(DiscussionRepository $discussions, array $filters, array $filterMutators) | ||
{ | ||
parent::__construct($filters, $filterMutators); | ||
|
||
$this->discussions = $discussions; | ||
} | ||
|
||
protected function getQuery(User $actor): Builder | ||
{ | ||
return $this->discussions->query()->select('discussions.*')->whereVisibleTo($actor); | ||
} | ||
} |
Oops, something went wrong.