-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a solution filter for the posts endpoint
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace FoF\BestAnswer\Search; | ||
|
||
use Flarum\Filter\FilterInterface; | ||
use Flarum\Filter\FilterState; | ||
use Flarum\User\User; | ||
use Illuminate\Database\Query\Builder; | ||
|
||
class BestAnswerPostFilter implements FilterInterface | ||
{ | ||
public function getFilterKey(): string | ||
{ | ||
return 'is:solution'; | ||
} | ||
|
||
public function filter(FilterState $filterState, string $filterValue, bool $negate) | ||
{ | ||
$this->constrain($filterState->getQuery(), $filterState->getActor(), $negate); | ||
} | ||
|
||
protected function constrain(Builder $query, User $actor, bool $negate) | ||
{ | ||
// Join the `discussions` table to access `best_answer_post_id`. | ||
$query->join('discussions', 'posts.discussion_id', '=', 'discussions.id') | ||
->where('posts.type', 'comment'); | ||
|
||
if ($negate) { | ||
// Exclude posts that are marked as the best answer | ||
$query->where(function ($query) { | ||
$query->whereNull('discussions.best_answer_post_id') | ||
->orWhereColumn('posts.id', '!=', 'discussions.best_answer_post_id'); | ||
}); | ||
} else { | ||
// Include only posts that are marked as the best answer | ||
$query->whereNotNull('discussions.best_answer_post_id') | ||
->whereColumn('posts.id', '=', 'discussions.best_answer_post_id'); | ||
} | ||
} | ||
} |