Skip to content

Commit

Permalink
Merge gambits into "FilterGambit"
Browse files Browse the repository at this point in the history
  • Loading branch information
askvortsov1 committed Feb 1, 2021
1 parent 7961e4a commit 583ed0d
Show file tree
Hide file tree
Showing 17 changed files with 285 additions and 492 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

use Flarum\Filter\FilterInterface;
use Flarum\Filter\WrappedFilter;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
use Flarum\User\UserRepository;
use Illuminate\Database\Query\Builder;

class AuthorFilter implements FilterInterface
class AuthorFilterGambit extends AbstractRegexGambit implements FilterInterface
{
/**
* @var \Flarum\User\UserRepository
Expand All @@ -28,21 +31,39 @@ public function __construct(UserRepository $users)
$this->users = $users;
}

/**
* {@inheritdoc}
*/
protected $pattern = 'author:(.+)';

/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $matches[1], $negate);
}

public function getFilterKey(): string
{
return 'author';
}

public function filter(WrappedFilter $wrappedFilter, string $filterValue, bool $negate)
{
$usernames = trim($filterValue, '"');
$this->constrain($wrappedFilter->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);
}

$wrappedFilter->getQuery()->whereIn('discussions.user_id', $ids, 'and', $negate);
$query->whereIn('discussions.user_id', $ids, 'and', $negate);
}
}
38 changes: 0 additions & 38 deletions src/Discussion/Filter/CreatedFilter.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Discussion/Filter/CreatedFilterGambit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\WrappedFilter;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
use Illuminate\Database\Query\Builder;

class CreatedFilterGambit extends AbstractRegexGambit implements FilterInterface
{
/**
* {@inheritdoc}
*/
protected $pattern = 'created:(\d{4}\-\d\d\-\d\d)(\.\.(\d{4}\-\d\d\-\d\d))?';

/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $matches[1], $matches[3], $negate);
}

public function getFilterKey(): string
{
return 'created';
}

public function filter(WrappedFilter $wrappedFilter, string $filterValue, bool $negate)
{
preg_match('/^'.$this->pattern.'$/i', 'created:'.$filterValue, $matches);

$this->constrain($wrappedFilter->getQuery(), $matches[1], $matches[3], $negate);
}

public function constrain(Builder $query, $firstDate, $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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,38 @@

use Flarum\Filter\FilterInterface;
use Flarum\Filter\WrappedFilter;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
use Illuminate\Database\Query\Builder;

class HiddenFilter implements FilterInterface
class HiddenFilterGambit extends AbstractRegexGambit implements FilterInterface
{
/**
* {@inheritdoc}
*/
protected $pattern = 'is:hidden';

/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $negate);
}

public function getFilterKey(): string
{
return 'hidden';
}

public function filter(WrappedFilter $wrappedFilter, string $filterValue, bool $negate)
{
$wrappedFilter->getQuery()->where(function ($query) use ($negate) {
$this->constrain($wrappedFilter->getQuery(), $negate);
}

protected function constrain(Builder $query, bool $negate)
{
$query->where(function ($query) use ($negate) {
if ($negate) {
$query->whereNull('hidden_at')->where('comment_count', '>', 0);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
use Flarum\Discussion\DiscussionRepository;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\WrappedFilter;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
use Flarum\User\User;
use Illuminate\Database\Query\Builder;

class UnreadFilter implements FilterInterface
class UnreadFilterGambit extends AbstractRegexGambit implements FilterInterface
{
public function getFilterKey(): string
{
return 'unread';
}

/**
* @var \Flarum\Discussion\DiscussionRepository
*/
Expand All @@ -33,14 +32,35 @@ public function __construct(DiscussionRepository $discussions)
$this->discussions = $discussions;
}

/**
* {@inheritdoc}
*/
protected $pattern = 'is:unread';

/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $search->getActor(), $negate);
}

public function getFilterKey(): string
{
return 'unread';
}

public function filter(WrappedFilter $wrappedFilter, string $filterValue, bool $negate)
{
$actor = $wrappedFilter->getActor();
$this->constrain($wrappedFilter->getQuery(), $wrappedFilter->getActor(), $negate);
}

protected function constrain(Builder $query, User $actor, bool $negate)
{
if ($actor->exists) {
$readIds = $this->discussions->getReadIds($actor);

$wrappedFilter->getQuery()->where(function ($query) use ($readIds, $negate, $actor) {
$query->where(function ($query) use ($readIds, $negate, $actor) {
if (! $negate) {
$query->whereNotIn('id', $readIds)->where('last_posted_at', '>', $actor->marked_all_as_read_at ?: 0);
} else {
Expand Down
57 changes: 0 additions & 57 deletions src/Discussion/Search/Gambit/AuthorGambit.php

This file was deleted.

43 changes: 0 additions & 43 deletions src/Discussion/Search/Gambit/CreatedGambit.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/Discussion/Search/Gambit/HiddenGambit.php

This file was deleted.

Loading

0 comments on commit 583ed0d

Please sign in to comment.