<commit_before><?php namespace Flarum\Subscriptions\Listeners;

use Flarum\Events\RegisterDiscussionGambits;
use Flarum\Events\DiscussionSearchWillBePerformed;

class HideIgnoredDiscussions
{
    public function subscribe($events)
    {
        $events->listen(RegisterDiscussionGambits::class, [$this, 'addGambit']);
        $events->listen(DiscussionSearchWillBePerformed::class, [$this, 'filterIgnored']);
    }

    public function addGambit(RegisterDiscussionGambits $event)
    {
        $event->gambits->add('Flarum\Subscriptions\Gambits\SubscriptionGambit');
    }

    public function filterIgnored(DiscussionSearchWillBePerformed $event)
    {
        if (! $event->criteria->query) {
            // might be better as `id IN (subquery)`?
            $actor = $event->search->getActor();
            $event->search->getQuery()->whereNotExists(function ($query) use ($actor) {
                $query->select(app('flarum.db')->raw(1))
                      ->from('users_discussions')
                      ->whereRaw('discussion_id = discussions.id')
                      ->where('user_id', $actor->id)
                      ->where('subscription', 'ignore');
            });
        }
    }
}<commit_msg>Use existing helper method<commit_after><?php namespace Flarum\Subscriptions\Listeners;

use Flarum\Events\RegisterDiscussionGambits;
use Flarum\Events\DiscussionSearchWillBePerformed;

class HideIgnoredDiscussions
{
    public function subscribe($events)
    {
        $events->listen(RegisterDiscussionGambits::class, [$this, 'addGambit']);
        $events->listen(DiscussionSearchWillBePerformed::class, [$this, 'filterIgnored']);
    }

    public function addGambit(RegisterDiscussionGambits $event)
    {
        $event->gambits->add('Flarum\Subscriptions\Gambits\SubscriptionGambit');
    }

    public function filterIgnored(DiscussionSearchWillBePerformed $event)
    {
        if (! $event->criteria->query) {
            // might be better as `id IN (subquery)`?
            $actor = $event->search->getActor();
            $event->search->getQuery()->whereNotExists(function ($query) use ($actor) {
                $query->selectRaw(1)
                      ->from('users_discussions')
                      ->whereRaw('discussion_id = discussions.id')
                      ->where('user_id', $actor->id)
                      ->where('subscription', 'ignore');
            });
        }
    }
}
{'fraction_non_alphanumeric': 0.13578208832026414, 'fraction_numerical': 0.0008254230293025176, 'mean_word_length': 2.683890577507599, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6258278', 'n_tokens_mistral': 703, 'n_tokens_neox': 662, 'n_words': 103}