moving to posts
This commit is contained in:
parent
48feda2877
commit
17bb6ff837
|
|
@ -31,8 +31,7 @@
|
|||
"category": "feature",
|
||||
"icon": {
|
||||
"image": "resources/assets/logo.png",
|
||||
"backgroundColor": "#E3E7F1",
|
||||
"backgroundSize": "100%",
|
||||
"backgroundSize": "95%",
|
||||
"backgroundRepeat": "no-repeat",
|
||||
"backgroundPosition": "center"
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
Before Width: | Height: | Size: 33 KiB After Width: | Height: | Size: 5.5 KiB |
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.");
|
||||
|
|
|
|||
|
|
@ -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') ?? [];
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Blomstra\Search\Schemas;
|
||||
|
||||
use Flarum\Api\Serializer\PostSerializer;
|
||||
use Flarum\Post\CommentPost;
|
||||
use Flarum\Post\Event\Deleted;
|
||||
use Flarum\Post\Event\Posted;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CommentPostSchema extends Schema
|
||||
{
|
||||
public function filters(CommentPost $post): array
|
||||
{
|
||||
$filters = [
|
||||
'author' => $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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue