Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Split Search Filter and Search #2453

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion js/src/common/Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class Store {
*/
find(type, id, query = {}, options = {}) {
let params = query;
let url = app.forum.attribute('apiUrl') + '/' + type;
let url = app.forum.attribute('apiUrl') + (query.search ? '/search/' : '/') + type;

if (id instanceof Array) {
url += '?filter[id]=' + id.join(',');
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/components/DiscussionsSearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class DiscussionsSearchSource {
include: 'mostRelevantPost',
};

return app.store.find('discussions', params).then((results) => (this.results[query] = results));
return app.store.find('discussions', params, { search: query }).then((results) => (this.results[query] = results));
}

view(query) {
Expand Down
12 changes: 8 additions & 4 deletions js/src/forum/components/UsersSearchSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ export default class UsersSearchResults {

search(query) {
return app.store
.find('users', {
filter: { q: query },
page: { limit: 5 },
})
.find(
'users',
{
filter: { q: query },
page: { limit: 5 },
},
{ search: query }
)
.then((results) => {
this.results[query] = results;
m.redraw();
Expand Down
2 changes: 1 addition & 1 deletion js/src/forum/states/DiscussionListState.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default class DiscussionListState {
params.page = { offset };
params.include = params.include.join(',');

return this.app.store.find('discussions', params);
return this.app.store.find('discussions', params, { search: params.filter.q });
}

/**
Expand Down
26 changes: 16 additions & 10 deletions src/Api/Controller/ListDiscussionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\DiscussionRepository;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Filter\Filterer;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

Expand Down Expand Up @@ -49,9 +49,14 @@ class ListDiscussionsController extends AbstractListController
public $sortFields = ['lastPostedAt', 'commentCount', 'createdAt'];

/**
* @var DiscussionSearcher
* @var DiscussionRepository
*/
protected $searcher;
protected $discussions;

/**
* @var Filterer
*/
protected $filterer;

/**
* @var UrlGenerator
Expand All @@ -62,9 +67,10 @@ class ListDiscussionsController extends AbstractListController
* @param DiscussionSearcher $searcher
* @param UrlGenerator $url
*/
public function __construct(DiscussionSearcher $searcher, UrlGenerator $url)
public function __construct(DiscussionRepository $discussions, Filterer $filterer, UrlGenerator $url)
{
$this->searcher = $searcher;
$this->discussions = $discussions;
$this->filterer = $filterer;
$this->url = $url;
}

Expand All @@ -74,16 +80,16 @@ public function __construct(DiscussionSearcher $searcher, UrlGenerator $url)
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');
$query = Arr::get($this->extractFilter($request), 'q');
$sort = $this->extractSort($request);

$criteria = new SearchCriteria($actor, $query, $sort);
$filters = $this->extractFilter($request);
$sort = $this->extractSort($request);
$query = $this->discussions->query();

$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$load = array_merge($this->extractInclude($request), ['state']);

$results = $this->searcher->search($criteria, $limit, $offset);
$results = $this->filterer->filter($actor, $query, $filters, $sort, $limit, $offset, $load);

$document->addPaginationLinks(
$this->url->to('api')->route('discussions.index'),
Expand Down
30 changes: 18 additions & 12 deletions src/Api/Controller/ListUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
namespace Flarum\Api\Controller;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\Filter\Filterer;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Flarum\User\Search\UserSearcher;
use Illuminate\Support\Arr;
use Flarum\User\UserRepository;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

Expand Down Expand Up @@ -41,23 +40,30 @@ class ListUsersController extends AbstractListController
];

/**
* @var UserSearcher
* @var Filterer
*/
protected $searcher;
protected $filterer;

/**
* @var UrlGenerator
*/
protected $url;

/**
* @param UserSearcher $searcher
* @var UserRepository
*/
protected $users;

/**
* @param Filterer $filterer
* @param UrlGenerator $url
* @param UserRepository $users
*/
public function __construct(UserSearcher $searcher, UrlGenerator $url)
public function __construct(Filterer $filterer, UrlGenerator $url, UserRepository $users)
{
$this->searcher = $searcher;
$this->filterer = $filterer;
$this->url = $url;
$this->users = $users;
}

/**
Expand All @@ -69,16 +75,16 @@ protected function data(ServerRequestInterface $request, Document $document)

$actor->assertCan('viewUserList');

$query = Arr::get($this->extractFilter($request), 'q');
$sort = $this->extractSort($request);
$query = $this->users->query();

$criteria = new SearchCriteria($actor, $query, $sort);
$filters = $this->extractFilter($request);
$sort = $this->extractSort($request);

$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$load = $this->extractInclude($request);

$results = $this->searcher->search($criteria, $limit, $offset, $load);
$results = $this->filterer->filter($actor, $query, $filters, $sort, $limit, $offset, $load);

$document->addPaginationLinks(
$this->url->to('api')->route('users.index'),
Expand Down
112 changes: 112 additions & 0 deletions src/Api/Controller/SearchDiscussionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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\Api\Controller;

use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class SearchDiscussionsController extends AbstractListController
{
/**
* {@inheritdoc}
*/
public $serializer = DiscussionSerializer::class;

/**
* {@inheritdoc}
*/
public $include = [
'user',
'lastPostedUser',
'mostRelevantPost',
'mostRelevantPost.user'
];

/**
* {@inheritdoc}
*/
public $optionalInclude = [
'firstPost',
'lastPost'
];

/**
* {@inheritdoc}
*/
public $sortFields = ['lastPostedAt', 'commentCount', 'createdAt'];

/**
* @var DiscussionSearcher
*/
protected $searcher;

/**
* @var UrlGenerator
*/
protected $url;

/**
* @param DiscussionSearcher $searcher
* @param UrlGenerator $url
*/
public function __construct(DiscussionSearcher $searcher, UrlGenerator $url)
{
$this->searcher = $searcher;
$this->url = $url;
}

/**
* {@inheritdoc}
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');
$query = Arr::get($this->extractFilter($request), 'q');
$sort = $this->extractSort($request);

$criteria = new SearchCriteria($actor, $query, $sort);

$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$load = array_merge($this->extractInclude($request), ['state']);

$results = $this->searcher->search($criteria, $limit, $offset);

$document->addPaginationLinks(
$this->url->to('api')->route('discussions.index'),
$request->getQueryParams(),
$offset,
$limit,
$results->areMoreResults() ? null : 0
);

Discussion::setStateUser($actor);

$results = $results->getResults()->load($load);

if ($relations = array_intersect($load, ['firstPost', 'lastPost'])) {
foreach ($results as $discussion) {
foreach ($relations as $relation) {
if ($discussion->$relation) {
$discussion->$relation->discussion = $discussion;
}
}
}
}

return $results;
}
}
93 changes: 93 additions & 0 deletions src/Api/Controller/SearchUsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?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\Api\Controller;

use Flarum\Api\Serializer\UserSerializer;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria;
use Flarum\User\Search\UserSearcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class SearchUsersController extends AbstractListController
{
/**
* {@inheritdoc}
*/
public $serializer = UserSerializer::class;

/**
* {@inheritdoc}
*/
public $include = ['groups'];

/**
* {@inheritdoc}
*/
public $sortFields = [
'username',
'commentCount',
'discussionCount',
'lastSeenAt',
'joinedAt'
];

/**
* @var UserSearcher
*/
protected $searcher;

/**
* @var UrlGenerator
*/
protected $url;

/**
* @param UserSearcher $searcher
* @param UrlGenerator $url
*/
public function __construct(UserSearcher $searcher, UrlGenerator $url)
{
$this->searcher = $searcher;
$this->url = $url;
}

/**
* {@inheritdoc}
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = $request->getAttribute('actor');

$actor->assertCan('viewUserList');

$query = Arr::get($this->extractFilter($request), 'q');
$sort = $this->extractSort($request);

$criteria = new SearchCriteria($actor, $query, $sort);

$limit = $this->extractLimit($request);
$offset = $this->extractOffset($request);
$load = $this->extractInclude($request);

$results = $this->searcher->search($criteria, $limit, $offset, $load);

$document->addPaginationLinks(
$this->url->to('api')->route('users.index'),
$request->getQueryParams(),
$offset,
$limit,
$results->areMoreResults() ? null : 0
);

return $results->getResults();
}
}
Loading