Update for beta 16

This commit is contained in:
Alexander Skvortsov 2021-03-13 16:32:48 -05:00
parent d8a39508dd
commit 58fa8d4b8c
6 changed files with 95 additions and 78 deletions

View File

@ -11,17 +11,17 @@ use Flarum\Api\Serializer\BasicDiscussionSerializer;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Event\Searching;
use Flarum\Event\ConfigureDiscussionGambits;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Post\Event\Deleted;
use Flarum\Post\Event\Hidden;
use Flarum\Post\Event\Posted;
use Flarum\Post\Event\Restored;
use Flarum\Subscriptions\Gambit\SubscriptionGambit;
use Flarum\Subscriptions\HideIgnoredFromAllDiscussionsPage;
use Flarum\Subscriptions\Query\SubscriptionFilterGambit;
use Flarum\Subscriptions\Listener;
use Flarum\Subscriptions\Notification\NewPostBlueprint;
use Illuminate\Contracts\Events\Dispatcher;
return [
(new Extend\Frontend('forum'))
@ -55,10 +55,10 @@ return [
->listen(Deleted::class, Listener\DeleteNotificationWhenPostIsHiddenOrDeleted::class)
->listen(Posted::class, Listener\FollowAfterReply::class),
function (Dispatcher $events) {
$events->listen(ConfigureDiscussionGambits::class, function (ConfigureDiscussionGambits $event) {
$event->gambits->add(SubscriptionGambit::class);
});
$events->listen(Searching::class, Listener\FilterDiscussionListBySubscription::class);
}
(new Extend\Filter(DiscussionFilterer::class))
->addFilter(SubscriptionFilterGambit::class)
->addFilterMutator(HideIgnoredFromAllDiscussionsPage::class),
(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(SubscriptionFilterGambit::class),
];

View File

@ -30,7 +30,7 @@ export default function addSubscriptionFilter() {
extend(DiscussionListState.prototype, 'requestParams', function (params) {
if (this.params.onFollowing) {
params.filter.q = (params.filter.q || '') + ' is:following';
params.filter.subscription = 'following';
}
});
}

View File

@ -1,37 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Gambit;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
class SubscriptionGambit extends AbstractRegexGambit
{
/**
* {@inheritdoc}
*/
protected $pattern = 'is:(follow|ignor)(?:ing|ed)';
/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$actor = $search->getActor();
$method = $negate ? 'whereNotIn' : 'whereIn';
$search->getQuery()->$method('id', function ($query) use ($actor, $matches) {
$query->select('discussion_id')
->from('discussion_user')
->where('user_id', $actor->id)
->where('subscription', $matches[1] === 'follow' ? 'follow' : 'ignore');
});
}
}

View File

@ -0,0 +1,32 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Subscriptions;
use Flarum\Filter\FilterState;
use Flarum\Query\QueryCriteria;
class HideIgnoredFromAllDiscussionsPage
{
public function __invoke(FilterState $filterState, QueryCriteria $criteria)
{
// We only want to hide on the "all discussions" page.
if (count($filterState->getActiveFilters()) === 0) {
// TODO: might be better as `id IN (subquery)`?
$actor = $filterState->getActor();
$filterState->getQuery()->whereNotExists(function ($query) use ($actor) {
$query->selectRaw(1)
->from('discussion_user')
->whereColumn('discussions.id', 'discussion_id')
->where('user_id', $actor->id)
->where('subscription', 'ignore');
});
}
}
}

View File

@ -1,30 +0,0 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Listener;
use Flarum\Discussion\Event\Searching;
class FilterDiscussionListBySubscription
{
public function handle(Searching $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('discussion_user')
->whereColumn('discussions.id', 'discussion_id')
->where('user_id', $actor->id)
->where('subscription', 'ignore');
});
}
}
}

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Subscriptions\Query;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
use Flarum\User\User;
use Illuminate\Database\Query\Builder;
class SubscriptionFilterGambit extends AbstractRegexGambit implements FilterInterface
{
protected function getGambitPattern() {
return 'is:(follow|ignor)(?:ing|ed)';
}
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $search->getActor(), $matches[1], $negate);
}
public function getFilterKey(): string
{
return 'subscription';
}
public function filter(FilterState $filterState, string $filterValue, bool $negate)
{
preg_match('/^' . $this->getGambitPattern().'$/i', 'is:'.$filterValue, $matches);
$this->constrain($filterState->getQuery(), $filterState->getActor(), $matches[1], $negate);
}
protected function constrain(Builder $query, User $actor, string $subscriptionType, bool $negate)
{
$method = $negate ? 'whereNotIn' : 'whereIn';
$query->$method('id', function ($query) use ($actor, $subscriptionType) {
$query->select('discussion_id')
->from('discussion_user')
->where('user_id', $actor->id)
->where('subscription', $subscriptionType === 'follow' ? 'follow' : 'ignore');
});
}
}