diff --git a/composer.json b/composer.json index 3700410..bfd0113 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,7 @@ "category": "feature", "icon": { "image": "resources/assets/logo.png", - "backgroundColor": "#E3E7F1", - "backgroundSize": "100%", + "backgroundSize": "95%", "backgroundRepeat": "no-repeat", "backgroundPosition": "center" } diff --git a/resources/assets/logo.png b/resources/assets/logo.png index d5eef47..eaa65b6 100644 Binary files a/resources/assets/logo.png and b/resources/assets/logo.png differ diff --git a/src/Api/Controllers/SearchController.php b/src/Api/Controllers/SearchController.php index a165af8..1d13f79 100644 --- a/src/Api/Controllers/SearchController.php +++ b/src/Api/Controllers/SearchController.php @@ -54,7 +54,7 @@ class SearchController extends AbstractListController $ids = Collection::make(Arr::get($result, 'hits.hits'))->pluck('_id')->toArray(); - return $schema::model()::query()->findMany($ids); + return $schema::query()->findMany($ids); } protected function getSchema(string $index): ?Schema diff --git a/src/Commands/RebuildDocumentsCommand.php b/src/Commands/RebuildDocumentsCommand.php index 74946c2..fc4bc0a 100644 --- a/src/Commands/RebuildDocumentsCommand.php +++ b/src/Commands/RebuildDocumentsCommand.php @@ -39,7 +39,8 @@ class RebuildDocumentsCommand extends Command $total = 0; - $model::query()->chunk(50, function (Collection $collection) use ($model, $queue, &$total) { + $schema::query()->chunk(50, function (Collection $collection) use ($model, $queue, &$total) { + $queue->push(new SavingJob($model, $collection)); $this->info("Pushed {$collection->count()} into the index."); diff --git a/src/Provider.php b/src/Provider.php index 8c2eb77..c35a13e 100644 --- a/src/Provider.php +++ b/src/Provider.php @@ -4,6 +4,7 @@ namespace Blomstra\Search; use Blomstra\Search\Observe\DeletingJob; use Blomstra\Search\Observe\SavingJob; +use Blomstra\Search\Schemas\CommentPostSchema; use Blomstra\Search\Schemas\DiscussionSchema; use Blomstra\Search\Schemas\Schema; use Elasticsearch\ClientBuilder; @@ -19,7 +20,8 @@ class Provider extends AbstractServiceProvider { public function register() { - $this->container->tag([DiscussionSchema::class], 'blomstra.search.schemas'); +// $this->container->tag([DiscussionSchema::class], 'blomstra.search.schemas'); + $this->container->tag([CommentPostSchema::class], 'blomstra.search.schemas'); $this->container->singleton('blomstra.search.elastic', function (Container $container) { $config = $container->make('flarum.config') ?? []; diff --git a/src/Schemas/CommentPostSchema.php b/src/Schemas/CommentPostSchema.php new file mode 100644 index 0000000..9ad80a3 --- /dev/null +++ b/src/Schemas/CommentPostSchema.php @@ -0,0 +1,79 @@ + $post->user_id, + 'created_at' => $post->created_at->toAtomString(), + 'private' => $post->is_private, + 'groups' => $this->groupsForDiscussion($post->discussion) + ]; + + + if ($this->extensionEnabled('fof-byobu')) { + $filters['recipient-users'] = $post->discussion->recipientUsers->pluck('id')->toArray(); + $filters['recipient-groups'] = $post->discussion->recipientGroups->pluck('id')->toArray(); + } + + if ($this->extensionEnabled('fof-best-answer')) { + $filters['best-answer-set'] = $post->discussion->best_answer_post_id !== null; + $filters['best-answer-set-at'] = $post->discussion->best_answer_set_at?->toAtomString(); + } + + return $filters; + } + + public function fulltext(CommentPost $post): array + { + return [ + 'title' => $post->discussion->title, + 'content' => $post->content + ]; + } + + public static function index(): string + { + return 'posts'; + } + + public static function model(): string + { + return CommentPost::class; + } + + public static function query(): Builder + { + return CommentPost::query() + ->where('type', CommentPost::$type); + } + + public static function serializer(): string + { + return PostSerializer::class; + } + + public static function savingOn(Dispatcher $events, callable $callable) + { + $events->listen(Posted::class, function (Posted $event) use ($callable) { + $callable($event->post); + }); + } + + public static function deletingOn(Dispatcher $events, callable $callable) + { + $events->listen(Deleted::class, function (Deleted $event) use ($callable) { + $callable($event->post); + }); + } +} diff --git a/src/Schemas/DiscussionSchema.php b/src/Schemas/DiscussionSchema.php index 0c19cd8..4ce14ea 100644 --- a/src/Schemas/DiscussionSchema.php +++ b/src/Schemas/DiscussionSchema.php @@ -97,8 +97,6 @@ class DiscussionSchema extends Schema public static function deletingOn(Dispatcher $events, callable $callable) { $events->listen([Deleted::class, Hidden::class], function ($event) use ($callable) { - if ($event->discussion->is_private) return; - return $callable($event->discussion); }); } diff --git a/src/Schemas/Schema.php b/src/Schemas/Schema.php index c0b5782..84ddbf6 100644 --- a/src/Schemas/Schema.php +++ b/src/Schemas/Schema.php @@ -2,8 +2,13 @@ namespace Blomstra\Search\Schemas; +use Flarum\Discussion\Discussion; use Flarum\Extension\ExtensionManager; +use Flarum\Group\Group; +use Flarum\Group\Permission; +use Flarum\Tags\Tag; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Database\Eloquent\Builder; abstract class Schema { @@ -18,9 +23,49 @@ abstract class Schema abstract public static function index(): string; abstract public static function model(): string; + abstract public static function query(): Builder; abstract public static function serializer(): string; abstract public static function savingOn(Dispatcher $events, callable $callable); abstract public static function deletingOn(Dispatcher $events, callable $callable); + + protected function groupsForDiscussion(Discussion $discussion): array + { + $permissions = collect(); + + $globalPermission = Permission::query() + ->where('permission', 'viewForum') + ->pluck('group_id'); + + if ($this->extensionEnabled('flarum-tags')) { + /** @var \Illuminate\Database\Eloquent\Collection $tags */ + $tags = $discussion->tags; + + $filters['tags'] = $tags->pluck('id')->toArray(); + $tagPermissions = Permission::query() + ->whereIn( + 'permission', + $tags->pluck('id')->map(function (int $id) { + return "tag$id.viewForum"; + }) + )->get(); + + $permissions = $tags->map(function (Tag $tag) use ($tagPermissions) { + $permissions = $tagPermissions->where('permission', "tag$tag->id.viewForum"); + + if ($tag->is_restricted) { + $permissions = $permissions->add(['group_id' => Group::ADMINISTRATOR_ID]); + } + + return $permissions->pluck('group_id'); + })->flatten(); + } + + if (! $discussion->is_private && $permissions->isEmpty()) { + $permissions = $globalPermission; + } + + return $permissions->toArray(); + } }