From ed718e145842e9da0a29ba62536ab38ff44ad9e2 Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Fri, 18 Nov 2022 09:28:52 +0000 Subject: [PATCH 1/5] chore: fix incorect command --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4894a4f..52cde4a 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Enable the extension inside the admin area and configure the settings. Enable the extension in your admin area. Now to seed your existing discussions use the following command: ``` -php flarum blomstra:search:build +php flarum blomstra:search:index ``` All mutations to discussions are automatically added and removed from the elasticsearch index. From 23dd4034174e01bcccbd43426132e9978adbf62d Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Fri, 18 Nov 2022 09:29:19 +0000 Subject: [PATCH 2/5] fix: override endpoint not used correctly --- .../extendDiscussionState.tsx | 4 ++-- .../SearchSources/DiscussionsSearchSource.tsx | 24 +++++++++---------- js/src/forum/index.ts | 19 ++++++++------- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/js/src/forum/PaginatedListStates/extendDiscussionState.tsx b/js/src/forum/PaginatedListStates/extendDiscussionState.tsx index b25c9ec..ece53e3 100644 --- a/js/src/forum/PaginatedListStates/extendDiscussionState.tsx +++ b/js/src/forum/PaginatedListStates/extendDiscussionState.tsx @@ -8,8 +8,8 @@ export default function extendDiscussionState() { override(DiscussionListState.prototype, 'loadPage', async function (this: DiscussionListState, original, page: number = 1) { const preloaded = app.data.apiDocument || null; - // 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 existing payload is given fallback on native page. + if (preloaded) return original.call(this, page); const params = this.requestParams(); params.page = { diff --git a/js/src/forum/SearchSources/DiscussionsSearchSource.tsx b/js/src/forum/SearchSources/DiscussionsSearchSource.tsx index d4439e9..c3aff73 100644 --- a/js/src/forum/SearchSources/DiscussionsSearchSource.tsx +++ b/js/src/forum/SearchSources/DiscussionsSearchSource.tsx @@ -44,7 +44,7 @@ export default class DiscussionsSearchSource implements SearchSource { this.results.set(query, models); } - view(query: string): Mithril.Children { + view(query: string): Array { query = query.toLowerCase(); // Get results from map @@ -54,7 +54,7 @@ export default class DiscussionsSearchSource implements SearchSource { const mostRelevantPost = discussion.mostRelevantPost(); return ( -
  • +
  • {highlight(discussion.title(), query)}
    {!!mostRelevantPost &&
    {highlight(mostRelevantPost.contentPlain(), query, 100)}
    } @@ -63,16 +63,14 @@ export default class DiscussionsSearchSource implements SearchSource { ); }); - return ( - <> -
  • {app.translator.trans('core.forum.search.discussions_heading')}
  • -
  • - - {app.translator.trans('core.forum.search.all_discussions_button', { query })} - -
  • - {results} - - ); + return [ +
  • {app.translator.trans('core.forum.search.discussions_heading')}
  • , +
  • + + {app.translator.trans('core.forum.search.all_discussions_button', { query })} + +
  • , + ...results, + ]; } } diff --git a/js/src/forum/index.ts b/js/src/forum/index.ts index 290b154..e1c83d3 100644 --- a/js/src/forum/index.ts +++ b/js/src/forum/index.ts @@ -1,6 +1,6 @@ import app from 'flarum/forum/app'; -import Search from 'flarum/forum/components/Search'; +import Search, { SearchAttrs, SearchSource } from 'flarum/forum/components/Search'; import { extend } from 'flarum/common/extend'; import ItemList from 'flarum/common/utils/ItemList'; @@ -9,12 +9,15 @@ import DiscussionsSearchSource from './SearchSources/DiscussionsSearchSource'; import extendDiscussionState from './PaginatedListStates/extendDiscussionState'; app.initializers.add('blomstra-search', () => { - extend(Search.prototype, 'sourceItems', function (this: Search, items: ItemList) { - // items.remove('users'); - items.remove('discussions'); - - items.add('discussions', new DiscussionsSearchSource()); + extend(Search.prototype, 'sourceItems', function (this: Search, items: ItemList) { + items.setContent('discussions', new DiscussionsSearchSource()); }); - - extendDiscussionState(); }); + +app.initializers.add( + 'blomstra-search-early', + () => { + extendDiscussionState(); + }, + 999999 +); From a26f88a9b686000ef20d3aa471a66be41254f9e8 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 18 Nov 2022 09:29:46 +0000 Subject: [PATCH 3/5] Apply fixes from StyleCI --- extend.php | 22 ++++++-- src/Api/Client.php | 14 ++++- src/Api/Controllers/IndexController.php | 13 ++++- src/Api/Controllers/SearchController.php | 34 ++++++++---- src/Commands/BuildCommand.php | 71 ++++++++++++++---------- src/Elasticsearch/MatchPhraseQuery.php | 12 +++- src/Elasticsearch/MatchQuery.php | 10 ++++ src/Elasticsearch/SimpleSearchQuery.php | 20 +++++-- src/Elasticsearch/TermsQuery.php | 10 ++++ src/Elasticsearch/WildcardQuery.php | 13 ++++- src/Exceptions/SeedingException.php | 13 ++++- src/Jobs/DeletingJob.php | 22 ++++++-- src/Jobs/Job.php | 14 ++++- src/Jobs/SavingJob.php | 26 +++++++-- src/Provider.php | 18 ++++-- src/Save/Document.php | 26 ++++++--- src/Seeders/CommentSeeder.php | 31 +++++++---- src/Seeders/DiscussionSeeder.php | 32 +++++++---- src/Seeders/Seeder.php | 13 ++++- 19 files changed, 308 insertions(+), 106 deletions(-) diff --git a/extend.php b/extend.php index 012f9e6..50c0c60 100644 --- a/extend.php +++ b/extend.php @@ -1,23 +1,33 @@ register(Provider::class), + (new Flarum\ServiceProvider())->register(Provider::class), (new Flarum\Frontend('forum')) - ->js(__DIR__ . '/js/dist/forum.js'), + ->js(__DIR__.'/js/dist/forum.js'), (new Flarum\Frontend('admin')) - ->js(__DIR__ . '/js/dist/admin.js'), + ->js(__DIR__.'/js/dist/admin.js'), - (new Flarum\Locales(__DIR__ . '/resources/locale')), + (new Flarum\Locales(__DIR__.'/resources/locale')), (new Flarum\Routes('api')) ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class) ->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class), - (new Flarum\Console) - ->command(Commands\BuildCommand::class) + (new Flarum\Console()) + ->command(Commands\BuildCommand::class), ]; diff --git a/src/Api/Client.php b/src/Api/Client.php index 3bd431d..372a975 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -1,5 +1,15 @@ queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions"); + if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) { + return parent::get('/blomstra/search/discussions'); + } return parent::get($path); } diff --git a/src/Api/Controllers/IndexController.php b/src/Api/Controllers/IndexController.php index 573ce8c..87c2eb5 100644 --- a/src/Api/Controllers/IndexController.php +++ b/src/Api/Controllers/IndexController.php @@ -1,5 +1,15 @@ run( new ArrayInput([]), - new ConsoleOutput); + new ConsoleOutput() + ); return new EmptyResponse(); } diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 644e66d..767dcca 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -1,11 +1,21 @@ 'updated_at', - 'createdAt' => 'created_at', - 'commentCount' => 'comment_count' + 'createdAt' => 'created_at', + 'commentCount' => 'comment_count', ]; public function __construct(protected Client $elastic, protected UrlGenerator $uri) - {} + { + } protected function data(ServerRequestInterface $request, Document $document) { @@ -58,14 +69,14 @@ class SearchController extends ListDiscussionsController $filterQuery = BoolQuery::create(); - if (! empty($search)) { + if (!empty($search)) { $filterQuery // @todo commented out to use only partial matching for now ->add($this->sentenceMatch($search)) ->add($this->wordMatch($search, 'and')) ->add($this->wordMatch($search, 'or')) // ->add($this->partialMatch($search)) - ; +; } $builder = (new Builder($this->elastic)) @@ -91,7 +102,7 @@ class SearchController extends ListDiscussionsController // If the first level of the relationship wasn't explicitly included, // add it so the code below can look for it - if (! in_array('mostRelevantPost', $include)) { + if (!in_array('mostRelevantPost', $include)) { $include[] = 'mostRelevantPost'; } } @@ -106,19 +117,19 @@ class SearchController extends ListDiscussionsController if ($type === 'posts') { return [ 'most_relevant_post_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } else { return [ 'discussion_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } }); $document->addPaginationLinks( $this->uri->to('api')->route('blomstra.search', [ - 'type' => 'discussions' + 'type' => 'discussions', ]), $request->getQueryParams(), $offset, @@ -132,7 +143,7 @@ class SearchController extends ListDiscussionsController ->select('discussions.*') ->join('posts', 'posts.discussion_id', 'discussions.id') // Extra safety to prevent leaking hidden discussion (titles) towards search results. - ->when($actor->isGuest() || ! $actor->hasPermission('discussion.hide'), fn($query) => $query->whereNull('discussions.hidden_at')) + ->when($actor->isGuest() || !$actor->hasPermission('discussion.hide'), fn ($query) => $query->whereNull('discussions.hidden_at')) ->where(function ($query) use ($results) { $query ->whereIn('discussions.id', $results->pluck('discussion_id')->filter()) @@ -287,7 +298,6 @@ class SearchController extends ListDiscussionsController ); } - protected function getGroups(User $actor): Collection { /** @var Collection $groups */ diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 3494c94..f8a320d 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -1,11 +1,20 @@ [ - 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], - 'created_at' => ['type' => 'date'], - 'updated_at' => ['type' => 'date'], - 'is_private' => ['type' => 'boolean'], - 'is_sticky' => ['type' => 'boolean'], - 'groups' => ['type' => 'integer'], + 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], + 'created_at' => ['type' => 'date'], + 'updated_at' => ['type' => 'date'], + 'is_private' => ['type' => 'boolean'], + 'is_sticky' => ['type' => 'boolean'], + 'groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'], - 'recipient_users' => ['type' => 'integer'], - 'comment_count' => ['type' => 'integer'], - ] + 'recipient_users' => ['type' => 'integer'], + 'comment_count' => ['type' => 'integer'], + ], ]; if ($this->option('recreate')) { // Flush the index. $client->indices()->delete([ 'index' => $index, - 'ignore_unavailable' => true + 'ignore_unavailable' => true, ]); // Create a new index. @@ -68,38 +77,38 @@ class BuildCommand extends Command 'body' => [ 'settings' => [ 'index.max_ngram_diff' => 10, - 'analysis' => [ + 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ - 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english' + 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english', ], 'flarum_analyzer_partial' => [ - 'type' => 'custom', + 'type' => 'custom', 'tokenizer' => 'standard', - 'filter' => [ + 'filter' => [ 'lowercase', - 'partial_search_filter' - ] - ] + 'partial_search_filter', + ], + ], ], 'filter' => [ 'partial_search_filter' => [ - 'type' => 'ngram', - 'min_gram' => 1, - 'max_gram' => 10, - 'token_chars' => ['letter', 'digit', 'symbol'] - ] - ] - ] - ] - ] + 'type' => 'ngram', + 'min_gram' => 1, + 'max_gram' => 10, + 'token_chars' => ['letter', 'digit', 'symbol'], + ], + ], + ], + ], + ], ]); } if ($this->option('recreate') || $this->option('mapping')) { $client->indices()->putMapping([ 'index' => $index, - 'body' => $properties + 'body' => $properties, ]); } @@ -107,7 +116,9 @@ class BuildCommand extends Command /** @var Seeder $seeder */ foreach ($seeders as $seeder) { - if ($only && $seeder->type() !== $only) continue; + if ($only && $seeder->type() !== $only) { + continue; + } $total = 0; @@ -115,7 +126,7 @@ class BuildCommand extends Command ? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id')) : $seeder->query()->max('id'); - while($continueAt !== null) { + while ($continueAt !== null) { /** @var Collection $collection */ $collection = $seeder->query() ->latest('id') diff --git a/src/Elasticsearch/MatchPhraseQuery.php b/src/Elasticsearch/MatchPhraseQuery.php index c82ac3a..4a64540 100644 --- a/src/Elasticsearch/MatchPhraseQuery.php +++ b/src/Elasticsearch/MatchPhraseQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; return [ - 'match_phrase' => $query + 'match_phrase' => $query, ]; } } diff --git a/src/Elasticsearch/MatchQuery.php b/src/Elasticsearch/MatchQuery.php index 21cc07b..2703fc9 100644 --- a/src/Elasticsearch/MatchQuery.php +++ b/src/Elasticsearch/MatchQuery.php @@ -1,5 +1,15 @@ [ - 'query' => $this->value, - 'fields' => $this->fields, - 'analyzer' => $this->analyzer, + 'query' => $this->value, + 'fields' => $this->fields, + 'analyzer' => $this->analyzer, 'default_operator' => 'AND', - 'boost' => $this->boost - ] + 'boost' => $this->boost, + ], ]; } } diff --git a/src/Elasticsearch/TermsQuery.php b/src/Elasticsearch/TermsQuery.php index c312822..c35cc84 100644 --- a/src/Elasticsearch/TermsQuery.php +++ b/src/Elasticsearch/TermsQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; - $query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity; + $query['wildcard'][$this->field]['case_insensitive'] = !$this->sensitivity; if ($this->rewrite) { $query['wildcard'][$this->field]['rewrite'] = $this->rewrite; - } return $query; diff --git a/src/Exceptions/SeedingException.php b/src/Exceptions/SeedingException.php index 549fc6f..81b78e3 100644 --- a/src/Exceptions/SeedingException.php +++ b/src/Exceptions/SeedingException.php @@ -1,13 +1,22 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); return [ - ['delete' => ['_index' => $this->index, '_id' => $document->id]] + ['delete' => ['_index' => $this->index, '_id' => $document->id]], ]; })->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); } } diff --git a/src/Jobs/Job.php b/src/Jobs/Job.php index 288d10e..60fa0ec 100644 --- a/src/Jobs/Job.php +++ b/src/Jobs/Job.php @@ -1,5 +1,15 @@ index = resolve('blomstra.search.elastic_index'); - if (static::$onQueue) $this->onQueue(static::$onQueue); + if (static::$onQueue) { + $this->onQueue(static::$onQueue); + } } } diff --git a/src/Jobs/SavingJob.php b/src/Jobs/SavingJob.php index da80c1f..fec9a05 100644 --- a/src/Jobs/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -1,5 +1,15 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { @@ -19,18 +31,20 @@ class SavingJob extends Job return [ ['index' => ['_index' => $this->index, '_id' => $document->id]], - $document->toArray() + $document->toArray(), ]; }) ->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); - if (Arr::get($response, 'errors') !== true) return true; + if (Arr::get($response, 'errors') !== true) { + return true; + } $items = Arr::get($response, 'items'); diff --git a/src/Provider.php b/src/Provider.php index 4bd7994..8590fae 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -1,11 +1,20 @@ build(); }); - $this->container->instance( 'blomstra.search.elastic_index', $settings->get('blomstra-search.elastic-index', 'flarum') @@ -62,19 +70,19 @@ class Provider extends AbstractServiceProvider $this->container->extend( Client::class, function () { - $pipe = new MiddlewarePipe; + $pipe = new MiddlewarePipe(); $exclude = resolve('flarum.api_client.exclude_middleware'); $middlewareStack = array_filter(resolve('flarum.api.middleware'), function ($middlewareClass) use ($exclude) { - return ! in_array($middlewareClass, $exclude); + return !in_array($middlewareClass, $exclude); }); foreach ($middlewareStack as $middleware) { $pipe->pipe(resolve($middleware)); } - $pipe->pipe(new ExecuteRoute); + $pipe->pipe(new ExecuteRoute()); return new Api\Client($pipe); } diff --git a/src/Save/Document.php b/src/Save/Document.php index ba4d7af..87112db 100644 --- a/src/Save/Document.php +++ b/src/Save/Document.php @@ -1,19 +1,29 @@ getType(new CommentPost); + return resolve(PostSerializer::class)->getType(new CommentPost()); } public function query(): Builder @@ -53,21 +63,22 @@ class CommentSeeder extends Seeder /** * @param CommentPost $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->content, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->content, 'content_partial' => $model->content, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->edited_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model->discussion), - 'comment_count' => $model->discussion->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->edited_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model->discussion), + 'comment_count' => $model->discussion->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/DiscussionSeeder.php b/src/Seeders/DiscussionSeeder.php index 2ebf29b..40af780 100644 --- a/src/Seeders/DiscussionSeeder.php +++ b/src/Seeders/DiscussionSeeder.php @@ -1,5 +1,15 @@ getType(new Discussion); + return resolve(DiscussionSerializer::class)->getType(new Discussion()); } public function query(): Builder @@ -32,6 +42,7 @@ class DiscussionSeeder extends Seeder $includes[] = 'recipientUsers'; $includes[] = 'recipientGroups'; } + return Discussion::query() ->whereNull('hidden_at') ->with($includes); @@ -53,21 +64,22 @@ class DiscussionSeeder extends Seeder /** * @param Discussion $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->title, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->title, 'content_partial' => $model->title, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->last_posted_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model), - 'comment_count' => $model->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->last_posted_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model), + 'comment_count' => $model->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/Seeder.php b/src/Seeders/Seeder.php index cdbc84b..ff4997a 100644 --- a/src/Seeders/Seeder.php +++ b/src/Seeders/Seeder.php @@ -1,5 +1,15 @@ flatten(); } - if (! $discussion->is_private && $permissions->isEmpty()) { + if (!$discussion->is_private && $permissions->isEmpty()) { $permissions = $globalPermission; } From 5815144d3aecb0400ddb535a20a69b45d53cc42e Mon Sep 17 00:00:00 2001 From: Ian Morland Date: Fri, 18 Nov 2022 09:34:16 +0000 Subject: [PATCH 4/5] Revert "Apply fixes from StyleCI" This reverts commit a26f88a9b686000ef20d3aa471a66be41254f9e8. --- extend.php | 22 ++------ src/Api/Client.php | 14 +---- src/Api/Controllers/IndexController.php | 13 +---- src/Api/Controllers/SearchController.php | 34 ++++-------- src/Commands/BuildCommand.php | 71 ++++++++++-------------- src/Elasticsearch/MatchPhraseQuery.php | 12 +--- src/Elasticsearch/MatchQuery.php | 10 ---- src/Elasticsearch/SimpleSearchQuery.php | 20 ++----- src/Elasticsearch/TermsQuery.php | 10 ---- src/Elasticsearch/WildcardQuery.php | 13 +---- src/Exceptions/SeedingException.php | 13 +---- src/Jobs/DeletingJob.php | 22 ++------ src/Jobs/Job.php | 14 +---- src/Jobs/SavingJob.php | 26 ++------- src/Provider.php | 18 ++---- src/Save/Document.php | 26 +++------ src/Seeders/CommentSeeder.php | 31 ++++------- src/Seeders/DiscussionSeeder.php | 32 ++++------- src/Seeders/Seeder.php | 13 +---- 19 files changed, 106 insertions(+), 308 deletions(-) diff --git a/extend.php b/extend.php index 50c0c60..012f9e6 100644 --- a/extend.php +++ b/extend.php @@ -1,33 +1,23 @@ register(Provider::class), + (new Flarum\ServiceProvider)->register(Provider::class), (new Flarum\Frontend('forum')) - ->js(__DIR__.'/js/dist/forum.js'), + ->js(__DIR__ . '/js/dist/forum.js'), (new Flarum\Frontend('admin')) - ->js(__DIR__.'/js/dist/admin.js'), + ->js(__DIR__ . '/js/dist/admin.js'), - (new Flarum\Locales(__DIR__.'/resources/locale')), + (new Flarum\Locales(__DIR__ . '/resources/locale')), (new Flarum\Routes('api')) ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class) ->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class), - (new Flarum\Console()) - ->command(Commands\BuildCommand::class), + (new Flarum\Console) + ->command(Commands\BuildCommand::class) ]; diff --git a/src/Api/Client.php b/src/Api/Client.php index 372a975..3bd431d 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -1,15 +1,5 @@ queryParams, 'filter.q')) { - return parent::get('/blomstra/search/discussions'); - } + if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions"); return parent::get($path); } diff --git a/src/Api/Controllers/IndexController.php b/src/Api/Controllers/IndexController.php index 87c2eb5..573ce8c 100644 --- a/src/Api/Controllers/IndexController.php +++ b/src/Api/Controllers/IndexController.php @@ -1,15 +1,5 @@ run( new ArrayInput([]), - new ConsoleOutput() - ); + new ConsoleOutput); return new EmptyResponse(); } diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 767dcca..644e66d 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -1,21 +1,11 @@ 'updated_at', - 'createdAt' => 'created_at', - 'commentCount' => 'comment_count', + 'createdAt' => 'created_at', + 'commentCount' => 'comment_count' ]; public function __construct(protected Client $elastic, protected UrlGenerator $uri) - { - } + {} protected function data(ServerRequestInterface $request, Document $document) { @@ -69,14 +58,14 @@ class SearchController extends ListDiscussionsController $filterQuery = BoolQuery::create(); - if (!empty($search)) { + if (! empty($search)) { $filterQuery // @todo commented out to use only partial matching for now ->add($this->sentenceMatch($search)) ->add($this->wordMatch($search, 'and')) ->add($this->wordMatch($search, 'or')) // ->add($this->partialMatch($search)) -; + ; } $builder = (new Builder($this->elastic)) @@ -102,7 +91,7 @@ class SearchController extends ListDiscussionsController // If the first level of the relationship wasn't explicitly included, // add it so the code below can look for it - if (!in_array('mostRelevantPost', $include)) { + if (! in_array('mostRelevantPost', $include)) { $include[] = 'mostRelevantPost'; } } @@ -117,19 +106,19 @@ class SearchController extends ListDiscussionsController if ($type === 'posts') { return [ 'most_relevant_post_id' => $id, - 'weight' => Arr::get($hit, 'sort.0'), + 'weight' => Arr::get($hit, 'sort.0') ]; } else { return [ 'discussion_id' => $id, - 'weight' => Arr::get($hit, 'sort.0'), + 'weight' => Arr::get($hit, 'sort.0') ]; } }); $document->addPaginationLinks( $this->uri->to('api')->route('blomstra.search', [ - 'type' => 'discussions', + 'type' => 'discussions' ]), $request->getQueryParams(), $offset, @@ -143,7 +132,7 @@ class SearchController extends ListDiscussionsController ->select('discussions.*') ->join('posts', 'posts.discussion_id', 'discussions.id') // Extra safety to prevent leaking hidden discussion (titles) towards search results. - ->when($actor->isGuest() || !$actor->hasPermission('discussion.hide'), fn ($query) => $query->whereNull('discussions.hidden_at')) + ->when($actor->isGuest() || ! $actor->hasPermission('discussion.hide'), fn($query) => $query->whereNull('discussions.hidden_at')) ->where(function ($query) use ($results) { $query ->whereIn('discussions.id', $results->pluck('discussion_id')->filter()) @@ -298,6 +287,7 @@ class SearchController extends ListDiscussionsController ); } + protected function getGroups(User $actor): Collection { /** @var Collection $groups */ diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index f8a320d..3494c94 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -1,20 +1,11 @@ [ - 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], - 'created_at' => ['type' => 'date'], - 'updated_at' => ['type' => 'date'], - 'is_private' => ['type' => 'boolean'], - 'is_sticky' => ['type' => 'boolean'], - 'groups' => ['type' => 'integer'], + 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], + 'created_at' => ['type' => 'date'], + 'updated_at' => ['type' => 'date'], + 'is_private' => ['type' => 'boolean'], + 'is_sticky' => ['type' => 'boolean'], + 'groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'], - 'recipient_users' => ['type' => 'integer'], - 'comment_count' => ['type' => 'integer'], - ], + 'recipient_users' => ['type' => 'integer'], + 'comment_count' => ['type' => 'integer'], + ] ]; if ($this->option('recreate')) { // Flush the index. $client->indices()->delete([ 'index' => $index, - 'ignore_unavailable' => true, + 'ignore_unavailable' => true ]); // Create a new index. @@ -77,38 +68,38 @@ class BuildCommand extends Command 'body' => [ 'settings' => [ 'index.max_ngram_diff' => 10, - 'analysis' => [ + 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ - 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english', + 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english' ], 'flarum_analyzer_partial' => [ - 'type' => 'custom', + 'type' => 'custom', 'tokenizer' => 'standard', - 'filter' => [ + 'filter' => [ 'lowercase', - 'partial_search_filter', - ], - ], + 'partial_search_filter' + ] + ] ], 'filter' => [ 'partial_search_filter' => [ - 'type' => 'ngram', - 'min_gram' => 1, - 'max_gram' => 10, - 'token_chars' => ['letter', 'digit', 'symbol'], - ], - ], - ], - ], - ], + 'type' => 'ngram', + 'min_gram' => 1, + 'max_gram' => 10, + 'token_chars' => ['letter', 'digit', 'symbol'] + ] + ] + ] + ] + ] ]); } if ($this->option('recreate') || $this->option('mapping')) { $client->indices()->putMapping([ 'index' => $index, - 'body' => $properties, + 'body' => $properties ]); } @@ -116,9 +107,7 @@ class BuildCommand extends Command /** @var Seeder $seeder */ foreach ($seeders as $seeder) { - if ($only && $seeder->type() !== $only) { - continue; - } + if ($only && $seeder->type() !== $only) continue; $total = 0; @@ -126,7 +115,7 @@ class BuildCommand extends Command ? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id')) : $seeder->query()->max('id'); - while ($continueAt !== null) { + while($continueAt !== null) { /** @var Collection $collection */ $collection = $seeder->query() ->latest('id') diff --git a/src/Elasticsearch/MatchPhraseQuery.php b/src/Elasticsearch/MatchPhraseQuery.php index 4a64540..c82ac3a 100644 --- a/src/Elasticsearch/MatchPhraseQuery.php +++ b/src/Elasticsearch/MatchPhraseQuery.php @@ -1,15 +1,5 @@ field]['boost'] = $this->boost; return [ - 'match_phrase' => $query, + 'match_phrase' => $query ]; } } diff --git a/src/Elasticsearch/MatchQuery.php b/src/Elasticsearch/MatchQuery.php index 2703fc9..21cc07b 100644 --- a/src/Elasticsearch/MatchQuery.php +++ b/src/Elasticsearch/MatchQuery.php @@ -1,15 +1,5 @@ [ - 'query' => $this->value, - 'fields' => $this->fields, - 'analyzer' => $this->analyzer, + 'query' => $this->value, + 'fields' => $this->fields, + 'analyzer' => $this->analyzer, 'default_operator' => 'AND', - 'boost' => $this->boost, - ], + 'boost' => $this->boost + ] ]; } } diff --git a/src/Elasticsearch/TermsQuery.php b/src/Elasticsearch/TermsQuery.php index c35cc84..c312822 100644 --- a/src/Elasticsearch/TermsQuery.php +++ b/src/Elasticsearch/TermsQuery.php @@ -1,15 +1,5 @@ field]['boost'] = $this->boost; - $query['wildcard'][$this->field]['case_insensitive'] = !$this->sensitivity; + $query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity; if ($this->rewrite) { $query['wildcard'][$this->field]['rewrite'] = $this->rewrite; + } return $query; diff --git a/src/Exceptions/SeedingException.php b/src/Exceptions/SeedingException.php index 81b78e3..549fc6f 100644 --- a/src/Exceptions/SeedingException.php +++ b/src/Exceptions/SeedingException.php @@ -1,22 +1,13 @@ models->isEmpty()) { - return; - } + if ($this->models->isEmpty()) return; // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); return [ - ['delete' => ['_index' => $this->index, '_id' => $document->id]], + ['delete' => ['_index' => $this->index, '_id' => $document->id]] ]; })->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true, + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true ]); } } diff --git a/src/Jobs/Job.php b/src/Jobs/Job.php index 60fa0ec..288d10e 100644 --- a/src/Jobs/Job.php +++ b/src/Jobs/Job.php @@ -1,15 +1,5 @@ index = resolve('blomstra.search.elastic_index'); - if (static::$onQueue) { - $this->onQueue(static::$onQueue); - } + if (static::$onQueue) $this->onQueue(static::$onQueue); } } diff --git a/src/Jobs/SavingJob.php b/src/Jobs/SavingJob.php index fec9a05..da80c1f 100644 --- a/src/Jobs/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -1,15 +1,5 @@ models->isEmpty()) { - return; - } + if ($this->models->isEmpty()) return; // Preparing body for storing. $body = $this->models->map(function (Model $model) { @@ -31,20 +19,18 @@ class SavingJob extends Job return [ ['index' => ['_index' => $this->index, '_id' => $document->id]], - $document->toArray(), + $document->toArray() ]; }) ->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true, + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true ]); - if (Arr::get($response, 'errors') !== true) { - return true; - } + if (Arr::get($response, 'errors') !== true) return true; $items = Arr::get($response, 'items'); diff --git a/src/Provider.php b/src/Provider.php index 8590fae..4bd7994 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -1,20 +1,11 @@ build(); }); + $this->container->instance( 'blomstra.search.elastic_index', $settings->get('blomstra-search.elastic-index', 'flarum') @@ -70,19 +62,19 @@ class Provider extends AbstractServiceProvider $this->container->extend( Client::class, function () { - $pipe = new MiddlewarePipe(); + $pipe = new MiddlewarePipe; $exclude = resolve('flarum.api_client.exclude_middleware'); $middlewareStack = array_filter(resolve('flarum.api.middleware'), function ($middlewareClass) use ($exclude) { - return !in_array($middlewareClass, $exclude); + return ! in_array($middlewareClass, $exclude); }); foreach ($middlewareStack as $middleware) { $pipe->pipe(resolve($middleware)); } - $pipe->pipe(new ExecuteRoute()); + $pipe->pipe(new ExecuteRoute); return new Api\Client($pipe); } diff --git a/src/Save/Document.php b/src/Save/Document.php index 87112db..ba4d7af 100644 --- a/src/Save/Document.php +++ b/src/Save/Document.php @@ -1,29 +1,19 @@ getType(new CommentPost()); + return resolve(PostSerializer::class)->getType(new CommentPost); } public function query(): Builder @@ -63,22 +53,21 @@ class CommentSeeder extends Seeder /** * @param CommentPost $model - * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type().':'.$model->id, - 'content' => $model->content, + 'type' => $this->type(), + 'id' => $this->type() . ':' . $model->id, + 'content' => $model->content, 'content_partial' => $model->content, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->edited_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model->discussion), - 'comment_count' => $model->discussion->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->edited_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model->discussion), + 'comment_count' => $model->discussion->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/DiscussionSeeder.php b/src/Seeders/DiscussionSeeder.php index 40af780..2ebf29b 100644 --- a/src/Seeders/DiscussionSeeder.php +++ b/src/Seeders/DiscussionSeeder.php @@ -1,15 +1,5 @@ getType(new Discussion()); + return resolve(DiscussionSerializer::class)->getType(new Discussion); } public function query(): Builder @@ -42,7 +32,6 @@ class DiscussionSeeder extends Seeder $includes[] = 'recipientUsers'; $includes[] = 'recipientGroups'; } - return Discussion::query() ->whereNull('hidden_at') ->with($includes); @@ -64,22 +53,21 @@ class DiscussionSeeder extends Seeder /** * @param Discussion $model - * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type().':'.$model->id, - 'content' => $model->title, + 'type' => $this->type(), + 'id' => $this->type() . ':' . $model->id, + 'content' => $model->title, 'content_partial' => $model->title, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->last_posted_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model), - 'comment_count' => $model->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->last_posted_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model), + 'comment_count' => $model->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/Seeder.php b/src/Seeders/Seeder.php index ff4997a..cdbc84b 100644 --- a/src/Seeders/Seeder.php +++ b/src/Seeders/Seeder.php @@ -1,15 +1,5 @@ flatten(); } - if (!$discussion->is_private && $permissions->isEmpty()) { + if (! $discussion->is_private && $permissions->isEmpty()) { $permissions = $globalPermission; } From 1cd3dac1778d613b132b58a6e0159f03ed4eb23e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 18 Nov 2022 09:34:30 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- extend.php | 22 ++++++-- src/Api/Client.php | 14 ++++- src/Api/Controllers/IndexController.php | 13 ++++- src/Api/Controllers/SearchController.php | 34 ++++++++---- src/Commands/BuildCommand.php | 71 ++++++++++++++---------- src/Elasticsearch/MatchPhraseQuery.php | 12 +++- src/Elasticsearch/MatchQuery.php | 10 ++++ src/Elasticsearch/SimpleSearchQuery.php | 20 +++++-- src/Elasticsearch/TermsQuery.php | 10 ++++ src/Elasticsearch/WildcardQuery.php | 13 ++++- src/Exceptions/SeedingException.php | 13 ++++- src/Jobs/DeletingJob.php | 22 ++++++-- src/Jobs/Job.php | 14 ++++- src/Jobs/SavingJob.php | 26 +++++++-- src/Provider.php | 18 ++++-- src/Save/Document.php | 26 ++++++--- src/Seeders/CommentSeeder.php | 31 +++++++---- src/Seeders/DiscussionSeeder.php | 32 +++++++---- src/Seeders/Seeder.php | 13 ++++- 19 files changed, 308 insertions(+), 106 deletions(-) diff --git a/extend.php b/extend.php index 012f9e6..f0c595b 100644 --- a/extend.php +++ b/extend.php @@ -1,23 +1,33 @@ register(Provider::class), + (new Flarum\ServiceProvider())->register(Provider::class), (new Flarum\Frontend('forum')) - ->js(__DIR__ . '/js/dist/forum.js'), + ->js(__DIR__.'/js/dist/forum.js'), (new Flarum\Frontend('admin')) - ->js(__DIR__ . '/js/dist/admin.js'), + ->js(__DIR__.'/js/dist/admin.js'), - (new Flarum\Locales(__DIR__ . '/resources/locale')), + (new Flarum\Locales(__DIR__.'/resources/locale')), (new Flarum\Routes('api')) ->get('/blomstra/search/{type}', 'blomstra.search', Api\Controllers\SearchController::class) ->put('/blomstra/search/index', 'blomstra.search.index', Api\Controllers\IndexController::class), - (new Flarum\Console) - ->command(Commands\BuildCommand::class) + (new Flarum\Console()) + ->command(Commands\BuildCommand::class), ]; diff --git a/src/Api/Client.php b/src/Api/Client.php index 3bd431d..32a914b 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -1,5 +1,15 @@ queryParams, 'filter.q')) return parent::get("/blomstra/search/discussions"); + if ($path === '/discussions' && Arr::has($this->queryParams, 'filter.q')) { + return parent::get('/blomstra/search/discussions'); + } return parent::get($path); } diff --git a/src/Api/Controllers/IndexController.php b/src/Api/Controllers/IndexController.php index 573ce8c..b6d2937 100644 --- a/src/Api/Controllers/IndexController.php +++ b/src/Api/Controllers/IndexController.php @@ -1,5 +1,15 @@ run( new ArrayInput([]), - new ConsoleOutput); + new ConsoleOutput() + ); return new EmptyResponse(); } diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index 644e66d..a59997f 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -1,11 +1,21 @@ 'updated_at', - 'createdAt' => 'created_at', - 'commentCount' => 'comment_count' + 'createdAt' => 'created_at', + 'commentCount' => 'comment_count', ]; public function __construct(protected Client $elastic, protected UrlGenerator $uri) - {} + { + } protected function data(ServerRequestInterface $request, Document $document) { @@ -58,14 +69,14 @@ class SearchController extends ListDiscussionsController $filterQuery = BoolQuery::create(); - if (! empty($search)) { + if (!empty($search)) { $filterQuery // @todo commented out to use only partial matching for now ->add($this->sentenceMatch($search)) ->add($this->wordMatch($search, 'and')) ->add($this->wordMatch($search, 'or')) // ->add($this->partialMatch($search)) - ; +; } $builder = (new Builder($this->elastic)) @@ -91,7 +102,7 @@ class SearchController extends ListDiscussionsController // If the first level of the relationship wasn't explicitly included, // add it so the code below can look for it - if (! in_array('mostRelevantPost', $include)) { + if (!in_array('mostRelevantPost', $include)) { $include[] = 'mostRelevantPost'; } } @@ -106,19 +117,19 @@ class SearchController extends ListDiscussionsController if ($type === 'posts') { return [ 'most_relevant_post_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } else { return [ 'discussion_id' => $id, - 'weight' => Arr::get($hit, 'sort.0') + 'weight' => Arr::get($hit, 'sort.0'), ]; } }); $document->addPaginationLinks( $this->uri->to('api')->route('blomstra.search', [ - 'type' => 'discussions' + 'type' => 'discussions', ]), $request->getQueryParams(), $offset, @@ -132,7 +143,7 @@ class SearchController extends ListDiscussionsController ->select('discussions.*') ->join('posts', 'posts.discussion_id', 'discussions.id') // Extra safety to prevent leaking hidden discussion (titles) towards search results. - ->when($actor->isGuest() || ! $actor->hasPermission('discussion.hide'), fn($query) => $query->whereNull('discussions.hidden_at')) + ->when($actor->isGuest() || !$actor->hasPermission('discussion.hide'), fn ($query) => $query->whereNull('discussions.hidden_at')) ->where(function ($query) use ($results) { $query ->whereIn('discussions.id', $results->pluck('discussion_id')->filter()) @@ -287,7 +298,6 @@ class SearchController extends ListDiscussionsController ); } - protected function getGroups(User $actor): Collection { /** @var Collection $groups */ diff --git a/src/Commands/BuildCommand.php b/src/Commands/BuildCommand.php index 3494c94..3935da6 100644 --- a/src/Commands/BuildCommand.php +++ b/src/Commands/BuildCommand.php @@ -1,11 +1,20 @@ [ - 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], - 'created_at' => ['type' => 'date'], - 'updated_at' => ['type' => 'date'], - 'is_private' => ['type' => 'boolean'], - 'is_sticky' => ['type' => 'boolean'], - 'groups' => ['type' => 'integer'], + 'content' => ['type' => 'text', 'analyzer' => 'flarum_analyzer_partial', 'search_analyzer' => 'flarum_analyzer'], + 'created_at' => ['type' => 'date'], + 'updated_at' => ['type' => 'date'], + 'is_private' => ['type' => 'boolean'], + 'is_sticky' => ['type' => 'boolean'], + 'groups' => ['type' => 'integer'], 'recipient_groups' => ['type' => 'integer'], - 'recipient_users' => ['type' => 'integer'], - 'comment_count' => ['type' => 'integer'], - ] + 'recipient_users' => ['type' => 'integer'], + 'comment_count' => ['type' => 'integer'], + ], ]; if ($this->option('recreate')) { // Flush the index. $client->indices()->delete([ 'index' => $index, - 'ignore_unavailable' => true + 'ignore_unavailable' => true, ]); // Create a new index. @@ -68,38 +77,38 @@ class BuildCommand extends Command 'body' => [ 'settings' => [ 'index.max_ngram_diff' => 10, - 'analysis' => [ + 'analysis' => [ 'analyzer' => [ 'flarum_analyzer' => [ - 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english' + 'type' => $settings->get('blomstra-search.analyzer-language') ?: 'english', ], 'flarum_analyzer_partial' => [ - 'type' => 'custom', + 'type' => 'custom', 'tokenizer' => 'standard', - 'filter' => [ + 'filter' => [ 'lowercase', - 'partial_search_filter' - ] - ] + 'partial_search_filter', + ], + ], ], 'filter' => [ 'partial_search_filter' => [ - 'type' => 'ngram', - 'min_gram' => 1, - 'max_gram' => 10, - 'token_chars' => ['letter', 'digit', 'symbol'] - ] - ] - ] - ] - ] + 'type' => 'ngram', + 'min_gram' => 1, + 'max_gram' => 10, + 'token_chars' => ['letter', 'digit', 'symbol'], + ], + ], + ], + ], + ], ]); } if ($this->option('recreate') || $this->option('mapping')) { $client->indices()->putMapping([ 'index' => $index, - 'body' => $properties + 'body' => $properties, ]); } @@ -107,7 +116,9 @@ class BuildCommand extends Command /** @var Seeder $seeder */ foreach ($seeders as $seeder) { - if ($only && $seeder->type() !== $only) continue; + if ($only && $seeder->type() !== $only) { + continue; + } $total = 0; @@ -115,7 +126,7 @@ class BuildCommand extends Command ? ($this->continueAt($seeder->type()) ?? $seeder->query()->max('id')) : $seeder->query()->max('id'); - while($continueAt !== null) { + while ($continueAt !== null) { /** @var Collection $collection */ $collection = $seeder->query() ->latest('id') diff --git a/src/Elasticsearch/MatchPhraseQuery.php b/src/Elasticsearch/MatchPhraseQuery.php index c82ac3a..2c66a00 100644 --- a/src/Elasticsearch/MatchPhraseQuery.php +++ b/src/Elasticsearch/MatchPhraseQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; return [ - 'match_phrase' => $query + 'match_phrase' => $query, ]; } } diff --git a/src/Elasticsearch/MatchQuery.php b/src/Elasticsearch/MatchQuery.php index 21cc07b..2aa833c 100644 --- a/src/Elasticsearch/MatchQuery.php +++ b/src/Elasticsearch/MatchQuery.php @@ -1,5 +1,15 @@ [ - 'query' => $this->value, - 'fields' => $this->fields, - 'analyzer' => $this->analyzer, + 'query' => $this->value, + 'fields' => $this->fields, + 'analyzer' => $this->analyzer, 'default_operator' => 'AND', - 'boost' => $this->boost - ] + 'boost' => $this->boost, + ], ]; } } diff --git a/src/Elasticsearch/TermsQuery.php b/src/Elasticsearch/TermsQuery.php index c312822..8f91bc2 100644 --- a/src/Elasticsearch/TermsQuery.php +++ b/src/Elasticsearch/TermsQuery.php @@ -1,5 +1,15 @@ field]['boost'] = $this->boost; - $query['wildcard'][$this->field]['case_insensitive'] = ! $this->sensitivity; + $query['wildcard'][$this->field]['case_insensitive'] = !$this->sensitivity; if ($this->rewrite) { $query['wildcard'][$this->field]['rewrite'] = $this->rewrite; - } return $query; diff --git a/src/Exceptions/SeedingException.php b/src/Exceptions/SeedingException.php index 549fc6f..d186bf3 100644 --- a/src/Exceptions/SeedingException.php +++ b/src/Exceptions/SeedingException.php @@ -1,13 +1,22 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { $document = $this->seeder->toDocument($model); return [ - ['delete' => ['_index' => $this->index, '_id' => $document->id]] + ['delete' => ['_index' => $this->index, '_id' => $document->id]], ]; })->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); } } diff --git a/src/Jobs/Job.php b/src/Jobs/Job.php index 288d10e..782e1a7 100644 --- a/src/Jobs/Job.php +++ b/src/Jobs/Job.php @@ -1,5 +1,15 @@ index = resolve('blomstra.search.elastic_index'); - if (static::$onQueue) $this->onQueue(static::$onQueue); + if (static::$onQueue) { + $this->onQueue(static::$onQueue); + } } } diff --git a/src/Jobs/SavingJob.php b/src/Jobs/SavingJob.php index da80c1f..9aec901 100644 --- a/src/Jobs/SavingJob.php +++ b/src/Jobs/SavingJob.php @@ -1,5 +1,15 @@ models->isEmpty()) return; + if ($this->models->isEmpty()) { + return; + } // Preparing body for storing. $body = $this->models->map(function (Model $model) { @@ -19,18 +31,20 @@ class SavingJob extends Job return [ ['index' => ['_index' => $this->index, '_id' => $document->id]], - $document->toArray() + $document->toArray(), ]; }) ->flatten(1); $response = $client->bulk([ - 'index' => $this->index, - 'body' => $body->toArray(), - 'refresh' => true + 'index' => $this->index, + 'body' => $body->toArray(), + 'refresh' => true, ]); - if (Arr::get($response, 'errors') !== true) return true; + if (Arr::get($response, 'errors') !== true) { + return true; + } $items = Arr::get($response, 'items'); diff --git a/src/Provider.php b/src/Provider.php index 4bd7994..35b69a9 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -1,11 +1,20 @@ build(); }); - $this->container->instance( 'blomstra.search.elastic_index', $settings->get('blomstra-search.elastic-index', 'flarum') @@ -62,19 +70,19 @@ class Provider extends AbstractServiceProvider $this->container->extend( Client::class, function () { - $pipe = new MiddlewarePipe; + $pipe = new MiddlewarePipe(); $exclude = resolve('flarum.api_client.exclude_middleware'); $middlewareStack = array_filter(resolve('flarum.api.middleware'), function ($middlewareClass) use ($exclude) { - return ! in_array($middlewareClass, $exclude); + return !in_array($middlewareClass, $exclude); }); foreach ($middlewareStack as $middleware) { $pipe->pipe(resolve($middleware)); } - $pipe->pipe(new ExecuteRoute); + $pipe->pipe(new ExecuteRoute()); return new Api\Client($pipe); } diff --git a/src/Save/Document.php b/src/Save/Document.php index ba4d7af..372de98 100644 --- a/src/Save/Document.php +++ b/src/Save/Document.php @@ -1,19 +1,29 @@ getType(new CommentPost); + return resolve(PostSerializer::class)->getType(new CommentPost()); } public function query(): Builder @@ -53,21 +63,22 @@ class CommentSeeder extends Seeder /** * @param CommentPost $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->content, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->content, 'content_partial' => $model->content, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->edited_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model->discussion), - 'comment_count' => $model->discussion->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->edited_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model->discussion), + 'comment_count' => $model->discussion->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/DiscussionSeeder.php b/src/Seeders/DiscussionSeeder.php index 2ebf29b..1dfd31f 100644 --- a/src/Seeders/DiscussionSeeder.php +++ b/src/Seeders/DiscussionSeeder.php @@ -1,5 +1,15 @@ getType(new Discussion); + return resolve(DiscussionSerializer::class)->getType(new Discussion()); } public function query(): Builder @@ -32,6 +42,7 @@ class DiscussionSeeder extends Seeder $includes[] = 'recipientUsers'; $includes[] = 'recipientGroups'; } + return Discussion::query() ->whereNull('hidden_at') ->with($includes); @@ -53,21 +64,22 @@ class DiscussionSeeder extends Seeder /** * @param Discussion $model + * * @return Document */ public function toDocument(Model $model): Document { $document = new Document([ - 'type' => $this->type(), - 'id' => $this->type() . ':' . $model->id, - 'content' => $model->title, + 'type' => $this->type(), + 'id' => $this->type().':'.$model->id, + 'content' => $model->title, 'content_partial' => $model->title, - 'created_at' => $model->created_at?->toAtomString(), - 'updated_at' => $model->last_posted_at?->toAtomString(), - 'is_private' => $model->is_private, - 'user_id' => $model->user_id, - 'groups' => $this->groupsForDiscussion($model), - 'comment_count' => $model->comment_count, + 'created_at' => $model->created_at?->toAtomString(), + 'updated_at' => $model->last_posted_at?->toAtomString(), + 'is_private' => $model->is_private, + 'user_id' => $model->user_id, + 'groups' => $this->groupsForDiscussion($model), + 'comment_count' => $model->comment_count, ]); if ($this->extensionEnabled('fof-byobu')) { diff --git a/src/Seeders/Seeder.php b/src/Seeders/Seeder.php index cdbc84b..ff0b890 100644 --- a/src/Seeders/Seeder.php +++ b/src/Seeders/Seeder.php @@ -1,5 +1,15 @@ flatten(); } - if (! $discussion->is_private && $permissions->isEmpty()) { + if (!$discussion->is_private && $permissions->isEmpty()) { $permissions = $globalPermission; }