feat: allow floating pinned topics to top in getSortedTopics

v1.18.x
Barış Soner Uşaklı 6 years ago
parent 6f7ab5861e
commit 1d7e0c6341

@ -55,6 +55,7 @@ recentController.getData = async function (req, url, sort) {
filter: filter, filter: filter,
term: term, term: term,
sort: sort, sort: sort,
floatPinned: req.query.pinned,
query: req.query, query: req.query,
}); });

@ -42,7 +42,7 @@ module.exports = function (Topics) {
} else { } else {
tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term); tids = await Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term);
} }
if (params.term !== 'alltime' || (params.cids && params.sort !== 'recent')) { if (params.term !== 'alltime' || params.cids || params.floatPinned) {
tids = await sortTids(tids, params); tids = await sortTids(tids, params);
} }
return await filterTids(tids, params); return await filterTids(tids, params);
@ -53,10 +53,10 @@ module.exports = function (Topics) {
const pinnedSets = []; const pinnedSets = [];
cids.forEach(function (cid) { cids.forEach(function (cid) {
if (sort === 'recent') { if (sort === 'recent') {
sets.push('cid:' + cid + ':tids:lastposttime'); sets.push('cid:' + cid + ':tids');
return; } else {
}
sets.push('cid:' + cid + ':tids' + (sort ? ':' + sort : '')); sets.push('cid:' + cid + ':tids' + (sort ? ':' + sort : ''));
}
pinnedSets.push('cid:' + cid + ':tids:pinned'); pinnedSets.push('cid:' + cid + ':tids:pinned');
}); });
const [tids, pinnedTids] = await Promise.all([ const [tids, pinnedTids] = await Promise.all([
@ -67,15 +67,30 @@ module.exports = function (Topics) {
} }
async function sortTids(tids, params) { async function sortTids(tids, params) {
const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount']); const topicData = await Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount', 'pinned']);
var sortFn = sortRecent; let sortFn = sortRecent;
if (params.sort === 'posts') { if (params.sort === 'posts') {
sortFn = sortPopular; sortFn = sortPopular;
} else if (params.sort === 'votes') { } else if (params.sort === 'votes') {
sortFn = sortVotes; sortFn = sortVotes;
} }
tids = topicData.sort(sortFn).map(topic => topic && topic.tid);
return tids; if (params.floatPinned) {
floatPinned(topicData, sortFn);
} else {
topicData.sort(sortFn);
}
return topicData.map(topic => topic && topic.tid);
}
function floatPinned(topicData, sortFn) {
topicData.sort((a, b) => {
if (a.pinned !== b.pinned) {
return b.pinned - a.pinned;
}
return sortFn(a, b);
});
} }
function sortRecent(a, b) { function sortRecent(a, b) {

Loading…
Cancel
Save