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; }