From 70f717821c8f604aff936c4af23118944fd41cdf Mon Sep 17 00:00:00 2001 From: IanM Date: Wed, 6 Nov 2024 23:07:35 +0000 Subject: [PATCH] constrain to within Q&A tags --- src/Search/BestAnswerPostFilter.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Search/BestAnswerPostFilter.php b/src/Search/BestAnswerPostFilter.php index e79e9c6..feccc62 100644 --- a/src/Search/BestAnswerPostFilter.php +++ b/src/Search/BestAnswerPostFilter.php @@ -13,8 +13,10 @@ use Flarum\Filter\FilterInterface; use Flarum\Filter\FilterState; +use Flarum\Tags\Tag; use Flarum\User\User; use Illuminate\Database\Query\Builder; +use Illuminate\Support\Collection; class BestAnswerPostFilter implements FilterInterface { @@ -45,5 +47,20 @@ protected function constrain(Builder $query, User $actor, bool $negate) $query->whereNotNull('discussions.best_answer_post_id') ->whereColumn('posts.id', '=', 'discussions.best_answer_post_id'); } + + // Constrain to discussions within the allowed Q&A tags + $query->whereIn('discussions.id', function ($subQuery) use ($actor) { + $subQuery->select('discussion_id') + ->from('discussion_tag') + ->whereIn('tag_id', $this->allowedQnATags($actor)); + }); + } + + protected function allowedQnATags(User $actor): Collection + { + return Tag::query() + ->whereVisibleTo($actor) + ->where('is_qna', true) + ->pluck('id'); } }