Update to beta.16

This commit is contained in:
SychO9 2021-03-12 15:01:08 +01:00 committed by Alexander Skvortsov
parent 2a74e2ceca
commit 72ad775840
4 changed files with 74 additions and 64 deletions

View File

@ -10,12 +10,16 @@
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Event\Saving;
use Flarum\Discussion\Filter\DiscussionFilterer;
use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Extend;
use Flarum\Sticky\Event\DiscussionWasStickied;
use Flarum\Sticky\Event\DiscussionWasUnstickied;
use Flarum\Sticky\Listener;
use Flarum\Sticky\Listener\SaveStickyToDatabase;
use Flarum\Sticky\Post\DiscussionStickiedPost;
use Flarum\Sticky\PinStickiedDiscussionsToTop;
use Flarum\Sticky\Query\StickyFilterGambit;
use Illuminate\Contracts\Events\Dispatcher;
return [
@ -47,8 +51,10 @@ return [
->listen(DiscussionWasStickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasStickied'])
->listen(DiscussionWasUnstickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasUnstickied']),
function (Dispatcher $events) {
// Replace with Filter extender before stable
$events->subscribe(Listener\PinStickiedDiscussionsToTop::class);
},
(new Extend\Filter(DiscussionFilterer::class))
->addFilter(StickyFilterGambit::class)
->addFilterMutator(PinStickiedDiscussionsToTop::class),
(new Extend\SimpleFlarumSearch(DiscussionSearcher::class))
->addGambit(StickyFilterGambit::class),
];

View File

@ -1,29 +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\Sticky\Gambit;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\AbstractSearch;
class StickyGambit extends AbstractRegexGambit
{
/**
* {@inheritdoc}
*/
protected $pattern = 'is:sticky';
/**
* {@inheritdoc}
*/
protected function conditions(AbstractSearch $search, array $matches, $negate)
{
$search->getQuery()->where('is_sticky', ! $negate);
}
}

View File

@ -7,48 +7,38 @@
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Sticky\Listener;
namespace Flarum\Sticky;
use Flarum\Discussion\Event\Searching;
use Flarum\Event\ConfigureDiscussionGambits;
use Flarum\Sticky\Gambit\StickyGambit;
use Flarum\Tags\Gambit\TagGambit;
use Illuminate\Contracts\Events\Dispatcher;
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Filter\FilterState;
use Flarum\Query\QueryCriteria;
use Flarum\Tags\Query\TagFilterGambit;
class PinStickiedDiscussionsToTop
{
/**
* @param Dispatcher $events
* Used to get the default sort.
*
* @var ListDiscussionsController
*/
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureDiscussionGambits::class, [$this, 'addStickyGambit']);
$events->listen(Searching::class, [$this, 'reorderSearch']);
protected $listDiscussionsController;
public function __construct(ListDiscussionsController $listDiscussionsController) {
$this->listDiscussionsController = $listDiscussionsController;
}
/**
* @param ConfigureDiscussionGambits $event
*/
public function addStickyGambit(ConfigureDiscussionGambits $event)
public function __invoke(FilterState $filterState, QueryCriteria $criteria)
{
$event->gambits->add(StickyGambit::class);
}
/**
* @param Searching $event
*/
public function reorderSearch(Searching $event)
{
if ($event->criteria->sort === null) {
$search = $event->search;
$query = $search->getQuery();
// TODO: This should take modifications by extensions into account.
if ($criteria->sort == $this->listDiscussionsController->sort) {
$query = $filterState->getQuery();
// If we are viewing a specific tag, then pin all stickied
// discussions to the top no matter what.
$gambits = $search->getActiveGambits();
$filters = $filterState->getActiveFilters();
if ($count = count($gambits)) {
if ($count === 1 && $gambits[0] instanceof TagGambit) {
if ($count = count($filters)) {
if ($count === 1 && $filters[0] instanceof TagFilterGambit) {
if (! is_array($query->orders)) {
$query->orders = [];
}
@ -74,14 +64,14 @@ class PinStickiedDiscussionsToTop
->selectRaw(1)
->from('discussion_user as sticky')
->whereColumn('sticky.discussion_id', 'id')
->where('sticky.user_id', '=', $search->getActor()->id)
->where('sticky.user_id', '=', $filterState->getActor()->id)
->whereColumn('sticky.last_read_post_number', '>=', 'last_post_number');
// Add the bindings manually (rather than as the second
// argument in orderByRaw) for now due to a bug in Laravel which
// would add the bindings in the wrong order.
$query->orderByRaw('is_sticky and not exists ('.$read->toSql().') and last_posted_at > ? desc')
->addBinding(array_merge($read->getBindings(), [$search->getActor()->read_time ?: 0]), 'union');
->addBinding(array_merge($read->getBindings(), [$filterState->getActor()->read_time ?: 0]), 'union');
$query->unionOrders = array_merge($query->unionOrders, $query->orders);
$query->unionLimit = $query->limit;

View File

@ -0,0 +1,43 @@
<?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\Sticky\Query;
use Flarum\Filter\FilterInterface;
use Flarum\Filter\FilterState;
use Flarum\Search\AbstractRegexGambit;
use Flarum\Search\SearchState;
use Illuminate\Database\Query\Builder;
class StickyFilterGambit extends AbstractRegexGambit implements FilterInterface
{
protected function getGambitPattern() {
return 'is:sticky';
}
protected function conditions(SearchState $search, array $matches, $negate)
{
$this->constrain($search->getQuery(), $negate);
}
public function getFilterKey(): string
{
return 'sticky';
}
public function filter(FilterState $filterState, string $filterValue, $negate)
{
$this->constrain($filterState->getQuery(), $negate);
}
protected function constrain(Builder $query, bool $negate) {
$query->where('is_sticky', !$negate);
}
}