-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sticky posts in tags are not pinned to top (#52)
* fix: allow for additional default filters * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
bc4c7d1
commit f34bcf1
Showing
5 changed files
with
122 additions
and
3 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
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,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/discussion-language. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\DiscussionLanguage\Provider; | ||
|
||
use Flarum\Discussion\Filter\DiscussionFilterer; | ||
use Flarum\Foundation\AbstractServiceProvider; | ||
use Flarum\Sticky\PinStickiedDiscussionsToTop; | ||
use FoF\DiscussionLanguage\Sticky\PinStickiedDiscussionsToTop as CustomPinStickiedDiscussionsToTop; | ||
|
||
class LanguageFilterProvider extends AbstractServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->container->extend('flarum.filter.filter_mutators', function ($mutators) { | ||
if (isset($mutators[DiscussionFilterer::class]) && is_array($mutators[DiscussionFilterer::class])) { | ||
foreach ($mutators[DiscussionFilterer::class] as $key => $mutator) { | ||
if ($mutator === PinStickiedDiscussionsToTop::class) { | ||
$mutators[DiscussionFilterer::class][$key] = CustomPinStickiedDiscussionsToTop::class; | ||
} | ||
} | ||
} | ||
|
||
return $mutators; | ||
}); | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of fof/discussion-language. | ||
* | ||
* Copyright (c) FriendsOfFlarum. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace FoF\DiscussionLanguage\Sticky; | ||
|
||
use Flarum\Filter\FilterState; | ||
use Flarum\Query\QueryCriteria; | ||
use Flarum\Tags\Query\TagFilterGambit; | ||
|
||
class PinStickiedDiscussionsToTop | ||
{ | ||
public function __invoke(FilterState $filterState, QueryCriteria $criteria) | ||
{ | ||
if ($criteria->sortIsDefault) { | ||
$query = $filterState->getQuery(); | ||
|
||
// If we are viewing a specific tag, then pin all stickied | ||
// discussions to the top no matter what. | ||
$filters = $filterState->getActiveFilters(); | ||
|
||
if (count($filters) > 0) { | ||
// Use array_filter to check if any of the filters is an instance of TagFilterGambit | ||
$tagFilterGambits = array_filter($filters, function ($filter) { | ||
return $filter instanceof TagFilterGambit; | ||
}); | ||
|
||
// Check if there is at least one TagFilterGambit instance | ||
if (count($tagFilterGambits) > 0) { | ||
if (!is_array($query->orders)) { | ||
$query->orders = []; | ||
} | ||
|
||
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// Otherwise, if we are viewing "all discussions", only pin stickied | ||
// discussions to the top if they are unread. To do this in a | ||
// performant way we create another query which will select all | ||
// stickied discussions, marry them into the main query, and then | ||
// reorder the unread ones up to the top. | ||
$sticky = clone $query; | ||
$sticky->where('is_sticky', true); | ||
unset($sticky->orders); | ||
|
||
$query->union($sticky); | ||
|
||
$read = $query->newQuery() | ||
->selectRaw('1') | ||
->from('discussion_user as sticky') | ||
->whereColumn('sticky.discussion_id', '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(), [$filterState->getActor()->marked_all_as_read_at ?: 0]), 'union'); | ||
|
||
$query->unionOrders = array_merge($query->unionOrders, $query->orders); | ||
$query->unionLimit = $query->limit; | ||
$query->unionOffset = $query->offset; | ||
|
||
$query->limit = $sticky->limit = $query->offset + $query->limit; | ||
unset($query->offset, $sticky->offset); | ||
} | ||
} | ||
} |