getType(new CommentPost()); } public function joinRelation(): string { return 'post'; } public function routing(Model $model): string { return (string) $model->discussion_id; } public function query(): Builder { return CommentPost::query() ->where('type', CommentPost::$type); } public static function savingOn(Dispatcher $events, callable $callable) { $events->listen([ Core\Posted::class, Core\Revised::class, Core\Hidden::class, Core\Restored::class, ], function ($event) use ($callable) { $callable($event->post); }); } public static function deletingOn(Dispatcher $events, callable $callable) { $events->listen([ Core\Deleted::class ], function ($event) use ($callable) { $callable($event->post); }); } /** Cached discussion type string (e.g. "discussions") — resolved once per job. */ private ?string $discussionType = null; private function discussionType(): string { return $this->discussionType ??= resolve(DiscussionSerializer::class)->getType(new Discussion()); } /** * Post documents only need content for has_child matching and the join * field to establish the parent-child relationship. All discussion-level * fields (groups, tags, comment_count, etc.) live on the discussion * document and are irrelevant here. * * @param CommentPost $model */ public function toDocument(Model $model): Document { return new Document([ 'join_field' => ['name' => $this->joinRelation(), 'parent' => "{$this->discussionType()}:{$model->discussion_id}"], 'discussion_id' => $model->discussion_id, 'id' => $this->type().':'.$model->id, 'rawId' => $model->id, 'content' => $model->content, 'is_hidden' => $model->hidden_at !== null, ]); } }