Merge branch 'master' into next-back

# Conflicts:
#	src/Listener/PinStickiedDiscussionsToTop.php
This commit is contained in:
Toby Zerner 2017-12-26 20:38:55 +10:30
commit 57c730af29
3 changed files with 43 additions and 26 deletions

View File

@ -156,15 +156,12 @@ System.register('flarum/sticky/components/DiscussionStickiedPost', ['flarum/comp
});;
'use strict';
System.register('flarum/sticky/main', ['flarum/extend', 'flarum/app', 'flarum/Model', 'flarum/models/Discussion', 'flarum/sticky/components/DiscussionStickiedPost', 'flarum/sticky/addStickyBadge', 'flarum/sticky/addStickyControl', 'flarum/sticky/addStickyExcerpt'], function (_export, _context) {
System.register('flarum/sticky/main', ['flarum/app', 'flarum/Model', 'flarum/models/Discussion', 'flarum/sticky/components/DiscussionStickiedPost', 'flarum/sticky/addStickyBadge', 'flarum/sticky/addStickyControl', 'flarum/sticky/addStickyExcerpt'], function (_export, _context) {
"use strict";
var extend, notificationType, app, Model, Discussion, DiscussionStickiedPost, addStickyBadge, addStickyControl, addStickyExcerpt;
var app, Model, Discussion, DiscussionStickiedPost, addStickyBadge, addStickyControl, addStickyExcerpt;
return {
setters: [function (_flarumExtend) {
extend = _flarumExtend.extend;
notificationType = _flarumExtend.notificationType;
}, function (_flarumApp) {
setters: [function (_flarumApp) {
app = _flarumApp.default;
}, function (_flarumModel) {
Model = _flarumModel.default;

View File

@ -1,4 +1,3 @@
import { extend, notificationType } from 'flarum/extend';
import app from 'flarum/app';
import Model from 'flarum/Model';
import Discussion from 'flarum/models/Discussion';
@ -17,4 +16,4 @@ app.initializers.add('flarum-sticky', () => {
addStickyBadge();
addStickyControl();
addStickyExcerpt();
});
});

View File

@ -25,7 +25,7 @@ class PinStickiedDiscussionsToTop
public function subscribe(Dispatcher $events)
{
$events->listen(ConfigureDiscussionGambits::class, [$this, 'addStickyGambit']);
$events->listen(Searching::class, [$this, 'reorderSearch']);
$events->listen(Searching::class, [$this, 'reorderSearch'], -100);
}
/**
@ -45,33 +45,54 @@ class PinStickiedDiscussionsToTop
$search = $event->search;
$query = $search->getQuery();
if (! is_array($query->orders)) {
$query->orders = [];
}
// If we are viewing a specific tag, then pin all stickied
// discussions to the top no matter what.
$gambits = $search->getActiveGambits();
foreach ($search->getActiveGambits() as $gambit) {
foreach ($gambits as $gambit) {
if ($gambit instanceof TagGambit) {
if (! is_array($query->orders)) {
$query->orders = [];
}
array_unshift($query->orders, ['column' => 'is_sticky', 'direction' => 'desc']);
return;
}
}
$query->leftJoin('users_discussions', function ($join) use ($search) {
$join->on('users_discussions.discussion_id', '=', 'discussions.id')
->where('discussions.is_sticky', '=', true)
->where('users_discussions.user_id', '=', $search->getActor()->id);
});
// 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.
if (empty($gambits)) {
$sticky = clone $query;
$sticky->where('is_sticky', true);
$sticky->orders = null;
// TODO: Might be quicker to do a subquery in the order clause than a join?
$grammar = $query->getGrammar();
$readNumber = $grammar->wrap('users_discussions.read_number');
$lastPostNumber = $grammar->wrap('discussions.last_post_number');
$query->union($sticky);
array_unshift($query->orders, [
'type' => 'raw',
'sql' => "(is_sticky AND ($readNumber IS NULL OR $lastPostNumber > $readNumber)) desc"
]);
$read = $query->newQuery()
->selectRaw(1)
->from('users_discussions as sticky')
->whereRaw('sticky.discussion_id = id')
->where('sticky.user_id', '=', $search->getActor()->id)
->whereRaw('sticky.read_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_time > ? desc')
->addBinding(array_merge($read->getBindings(), [$search->getActor()->read_time ?: 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;
$query->offset = $sticky->offset = null;
}
}
}
}