fix: search for byobu page

Fixes the index for byobu.
This commit is contained in:
Daniel Klabbers 2021-11-16 20:20:53 +01:00
parent 8b1a32af8d
commit 868713da11
4 changed files with 63 additions and 27 deletions

2
js/dist/forum.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -8,8 +8,6 @@ export default function extendDiscussionState() {
override(DiscussionListState.prototype, 'loadPage', async function (this: DiscussionListState, original, page: number = 1) { override(DiscussionListState.prototype, 'loadPage', async function (this: DiscussionListState, original, page: number = 1) {
const preloaded = app.data.apiDocument || null; const preloaded = app.data.apiDocument || null;
// If this is not the usual index, make sure to fallback to the native page (eg for byobu).
if (app.current.get('routeName') !== 'index') return original.call(this, page);
// If existing payload is given or no search is made, fallback on native page. // If existing payload is given or no search is made, fallback on native page.
if (preloaded || !this.requestParams()?.filter?.q) return original.call(this, page); if (preloaded || !this.requestParams()?.filter?.q) return original.call(this, page);

View File

@ -15,8 +15,8 @@ use Flarum\Group\Group;
use Flarum\Http\RequestUtil; use Flarum\Http\RequestUtil;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr; use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Spatie\ElasticsearchQueryBuilder\Builder; use Spatie\ElasticsearchQueryBuilder\Builder;
@ -48,19 +48,24 @@ class SearchController extends ListDiscussionsController
$filters = $this->extractFilter($request); $filters = $this->extractFilter($request);
$search = $this->getSearch($filters);
$include = array_merge($this->extractInclude($request), ['state']); $include = array_merge($this->extractInclude($request), ['state']);
$filterQuery = (BoolQuery::create()) $filterQuery = BoolQuery::create();
->add($this->sentenceMatch($filters))
->add($this->wordMatch($filters)) if (! empty($search)) {
; $filterQuery
->add($this->sentenceMatch($search))
->add($this->wordMatch($search));
}
$builder = (new Builder($this->elastic)) $builder = (new Builder($this->elastic))
->index(resolve('blomstra.search.elastic_index')) ->index(resolve('blomstra.search.elastic_index'))
->size($this->extractLimit($request)) ->size($this->extractLimit($request))
->from($this->extractOffset($request)) ->from($this->extractOffset($request))
->addQuery( ->addQuery(
$this->addFilters($filterQuery, $actor) $this->addFilters($filterQuery, $actor, $filters)
); );
foreach ($this->extractSort($request) as $field => $direction) { foreach ($this->extractSort($request) as $field => $direction) {
@ -150,14 +155,11 @@ class SearchController extends ListDiscussionsController
return $manager->isEnabled($extension); return $manager->isEnabled($extension);
} }
protected function addFilters(BoolQuery $query, User $actor): BoolQuery protected function addFilters(BoolQuery $query, User $actor, array $filters = []): BoolQuery
{ {
/** @var Collection $groups */ $groups = $this->getGroups($actor);
$groups = $actor->groups->pluck('id');
$groups->add(Group::GUEST_ID); $onlyPrivate = Str::contains($filters['q'] ?? '', 'is:private');
if ($actor->is_email_confirmed) $groups->add(Group::MEMBER_ID);
$subQuery = BoolQuery::create() $subQuery = BoolQuery::create()
->add(TermQuery::create('is_private', 'false')) ->add(TermQuery::create('is_private', 'false'))
@ -165,17 +167,20 @@ class SearchController extends ListDiscussionsController
if ($this->extensionEnabled('fof-byobu') && $actor->exists) { if ($this->extensionEnabled('fof-byobu') && $actor->exists) {
$byobuQuery = BoolQuery::create() $byobuQuery = BoolQuery::create()
->add(TermQuery::create('is_private', 'true'), 'should') ->add(TermQuery::create('is_private', 'true'))
->add( ->add(
BoolQuery::create() BoolQuery::create()
->add(TermsQuery::create('recipient-groups', $groups->toArray())) ->add(TermsQuery::create('recipient-groups', $groups->toArray()), 'should')
->add(TermQuery::create('recipient-users', $actor->id)), ->add(TermsQuery::create('recipient-users', [$actor->id]), 'should'),
'should'
); );
$subQuery = BoolQuery::create() if ($onlyPrivate) {
->add($subQuery, 'should') $subQuery = $byobuQuery;
->add($byobuQuery, 'should'); } else {
$subQuery = BoolQuery::create()
->add($subQuery, 'should')
->add($byobuQuery, 'should');
}
} }
$query->add( $query->add(
@ -186,13 +191,46 @@ class SearchController extends ListDiscussionsController
return $query; return $query;
} }
protected function sentenceMatch(array $filters): Query protected function sentenceMatch(string $q): Query
{ {
return new MatchPhraseQuery('content', $filters['q']); return new MatchPhraseQuery('content', $q);
} }
protected function wordMatch(array $filters) protected function wordMatch(string $q)
{ {
return (new MatchQuery('content', $filters['q']))->boost(.3); return (new MatchQuery('content', $q))->boost(.3);
}
protected function getGroups(User $actor): Collection
{
/** @var Collection $groups */
$groups = $actor->groups->pluck('id');
$groups->add(Group::GUEST_ID);
if ($actor->is_email_confirmed) {
$groups->add(Group::MEMBER_ID);
}
return $groups;
}
protected function getSearch(array $filters): ?string
{
$search = Arr::get($filters, 'q');
if ($search) {
$q = collect(explode(' ', $search))
->filter(function (string $part) {
return $part !== 'is:private';
})
->filter()
->join(' ');
return empty($q) ? null : $q;
}
return null;
} }
} }