From 99311188a2247713811c01d2c9c6037ed91c4fae Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Thu, 11 Mar 2021 23:12:49 -0500 Subject: [PATCH 1/3] Make filter mutator API consistent with search mutator API. This is inline with the docblock for the Filter extender, and is much more sensible. --- framework/core/src/Filter/AbstractFilterer.php | 2 +- framework/core/tests/integration/extenders/FilterTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/framework/core/src/Filter/AbstractFilterer.php b/framework/core/src/Filter/AbstractFilterer.php index c36f4a92e..736568901 100644 --- a/framework/core/src/Filter/AbstractFilterer.php +++ b/framework/core/src/Filter/AbstractFilterer.php @@ -77,7 +77,7 @@ abstract class AbstractFilterer // END DEPRECATED BC LAYER foreach ($this->filterMutators as $mutator) { - $mutator($query, $actor, $criteria->query, $criteria->sort); + $mutator($filterState, $criteria); } // Execute the filter query and retrieve the results. We get one more diff --git a/framework/core/tests/integration/extenders/FilterTest.php b/framework/core/tests/integration/extenders/FilterTest.php index e6ec34fb5..07f94e873 100644 --- a/framework/core/tests/integration/extenders/FilterTest.php +++ b/framework/core/tests/integration/extenders/FilterTest.php @@ -84,8 +84,8 @@ class FilterTest extends TestCase */ public function filter_mutator_has_effect_if_added() { - $this->extend((new Extend\Filter(DiscussionFilterer::class))->addFilterMutator(function ($query, $actor, $filters, $sort) { - $query->getQuery()->whereRaw('1=0'); + $this->extend((new Extend\Filter(DiscussionFilterer::class))->addFilterMutator(function ($filterState, $criteria) { + $filterState->getQuery()->whereRaw('1=0'); })); $this->prepDb(); @@ -127,8 +127,8 @@ class NoResultFilter implements FilterInterface class CustomFilterMutator { - public function __invoke($query, $actor, $filters, $sort) + public function __invoke($filterState, $criteria) { - $query->getQuery()->whereRaw('1=0'); + $filterState->getQuery()->whereRaw('1=0'); } } From 38dc12ef0d83eb8f036f18412dad61f49a6c3730 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Fri, 12 Mar 2021 15:30:14 -0500 Subject: [PATCH 2/3] Provide active filters to filter state --- .../core/src/Filter/AbstractFilterer.php | 1 + framework/core/src/Filter/FilterState.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/framework/core/src/Filter/AbstractFilterer.php b/framework/core/src/Filter/AbstractFilterer.php index 736568901..5f07fdbbb 100644 --- a/framework/core/src/Filter/AbstractFilterer.php +++ b/framework/core/src/Filter/AbstractFilterer.php @@ -62,6 +62,7 @@ abstract class AbstractFilterer $filterKey = substr($filterKey, 1); } foreach (Arr::get($this->filters, $filterKey, []) as $filter) { + $filterState->addActiveFilter($filter); $filter->filter($filterState, $filterValue, $negate); } } diff --git a/framework/core/src/Filter/FilterState.php b/framework/core/src/Filter/FilterState.php index b9e22611e..5597964da 100644 --- a/framework/core/src/Filter/FilterState.php +++ b/framework/core/src/Filter/FilterState.php @@ -13,4 +13,29 @@ use Flarum\Query\AbstractQueryState; class FilterState extends AbstractQueryState { + /** + * @var FilterInterface[] + */ + protected $activeFilters = []; + + /** + * Get a list of the filters that are active. + * + * @return FilterInterface[] + */ + public function getActiveFilters() + { + return $this->activeFilters; + } + + /** + * Add a filter as being active. + * + * @param FilterInterface $filter + * @return void + */ + public function addActiveFilter(FilterInterface $filter) + { + $this->activeFilters[] = $filter; + } } From 2f0192d47d1965f4f163e326807939ad85b74bf1 Mon Sep 17 00:00:00 2001 From: Alexander Skvortsov Date: Fri, 12 Mar 2021 15:30:36 -0500 Subject: [PATCH 3/3] Fix Index content, only use search when applicable. --- framework/core/src/Forum/Content/Index.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Forum/Content/Index.php b/framework/core/src/Forum/Content/Index.php index 79ccc28cc..f5a80eade 100644 --- a/framework/core/src/Forum/Content/Index.php +++ b/framework/core/src/Forum/Content/Index.php @@ -75,10 +75,14 @@ class Index $params = [ 'sort' => $sort && isset($sortMap[$sort]) ? $sortMap[$sort] : '', - 'filter' => compact('q'), + 'filter' => [], 'page' => ['offset' => ($page - 1) * 20, 'limit' => 20] ]; + if ($q) { + $params['filter']['q'] = $q; + } + $apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params); $defaultRoute = $this->settings->get('default_route');