Files
NodeBB/src/topics/sorted.js

197 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-10-17 13:33:38 -04:00
'use strict';
const async = require('async');
const _ = require('lodash');
2018-10-17 13:33:38 -04:00
const db = require('../database');
const privileges = require('../privileges');
const user = require('../user');
const categories = require('../categories');
const meta = require('../meta');
const plugins = require('../plugins');
2018-10-17 13:33:38 -04:00
module.exports = function (Topics) {
Topics.getSortedTopics = function (params, callback) {
var data = {
nextStart: 0,
topicCount: 0,
topics: [],
};
params.term = params.term || 'alltime';
params.sort = params.sort || 'recent';
2018-10-22 13:07:02 -04:00
params.query = params.query || {};
2018-10-17 13:33:38 -04:00
if (params.hasOwnProperty('cids') && params.cids && !Array.isArray(params.cids)) {
params.cids = [params.cids];
}
async.waterfall([
function (next) {
getTids(params, next);
},
function (tids, next) {
data.topicCount = tids.length;
data.tids = tids;
getTopics(tids, params, next);
},
function (topicData, next) {
data.topics = topicData;
data.nextStart = params.stop + 1;
next(null, data);
},
], callback);
};
function getTids(params, callback) {
async.waterfall([
function (next) {
if (params.term === 'alltime') {
var key = 'topics:' + params.sort;
if (params.cids) {
2018-12-12 13:46:13 -05:00
key = getCidSets(params.cids, params.sort);
2018-10-17 13:33:38 -04:00
}
db.getSortedSetRevRange(key, 0, 199, next);
} else {
Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term, next);
}
},
function (tids, next) {
2018-12-12 13:46:13 -05:00
if (params.term !== 'alltime' || (params.cids && params.sort !== 'recent')) {
2018-10-17 13:33:38 -04:00
sortTids(tids, params, next);
} else {
next(null, tids);
}
},
function (tids, next) {
2018-10-22 13:07:02 -04:00
filterTids(tids, params, next);
2018-10-17 13:33:38 -04:00
},
], callback);
}
2018-12-12 13:46:13 -05:00
function getCidSets(cids, sort) {
const keys = [];
cids.forEach(function (cid) {
if (sort === 'recent') {
keys.push('cid:' + cid + ':tids:lastposttime');
return;
}
keys.push('cid:' + cid + ':tids' + (sort ? ':' + sort : ''));
keys.push('cid:' + cid + ':tids:pinned');
});
return keys;
}
2018-10-17 13:33:38 -04:00
function sortTids(tids, params, callback) {
async.waterfall([
function (next) {
Topics.getTopicsFields(tids, ['tid', 'lastposttime', 'upvotes', 'downvotes', 'postcount'], next);
},
function (topicData, next) {
var sortFn = sortRecent;
if (params.sort === 'posts') {
sortFn = sortPopular;
} else if (params.sort === 'votes') {
sortFn = sortVotes;
}
2018-10-25 17:02:59 -04:00
tids = topicData.sort(sortFn).map(topic => topic && topic.tid);
2018-10-17 13:33:38 -04:00
next(null, tids);
},
], callback);
}
function sortRecent(a, b) {
return b.lastposttime - a.lastposttime;
}
function sortVotes(a, b) {
2018-10-25 17:02:59 -04:00
if (a.votes !== b.votes) {
2018-10-17 13:33:38 -04:00
return b.votes - a.votes;
}
2018-10-25 17:02:59 -04:00
return b.postcount - a.postcount;
2018-10-17 13:33:38 -04:00
}
function sortPopular(a, b) {
2018-10-25 17:02:59 -04:00
if (a.postcount !== b.postcount) {
2018-10-17 13:33:38 -04:00
return b.postcount - a.postcount;
}
2018-10-25 17:02:59 -04:00
return b.viewcount - a.viewcount;
2018-10-17 13:33:38 -04:00
}
2018-10-22 13:07:02 -04:00
function filterTids(tids, params, callback) {
const filter = params.filter;
const uid = params.uid;
let topicData;
let topicCids;
2018-10-17 13:33:38 -04:00
async.waterfall([
function (next) {
if (filter === 'watched') {
Topics.filterWatchedTids(tids, uid, next);
} else if (filter === 'new') {
Topics.filterNewTids(tids, uid, next);
} else if (filter === 'unreplied') {
Topics.filterUnrepliedTids(tids, next);
} else {
Topics.filterNotIgnoredTids(tids, uid, next);
}
},
function (tids, next) {
privileges.topics.filterTids('read', tids, uid, next);
},
function (tids, next) {
Topics.getTopicsFields(tids, ['uid', 'tid', 'cid'], next);
},
function (_topicData, next) {
topicData = _topicData;
topicCids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);
2018-10-17 13:33:38 -04:00
async.parallel({
ignoredCids: function (next) {
if (filter === 'watched' || meta.config.disableRecentCategoryFilter) {
2018-10-17 13:33:38 -04:00
return next(null, []);
}
categories.isIgnored(topicCids, uid, next);
2018-10-17 13:33:38 -04:00
},
filtered: async.apply(user.blocks.filter, uid, topicData),
2018-10-17 13:33:38 -04:00
}, next);
},
function (results, next) {
const isCidIgnored = _.zipObject(topicCids, results.ignoredCids);
topicData = results.filtered;
2018-10-17 13:33:38 -04:00
2018-10-22 13:07:02 -04:00
const cids = params.cids && params.cids.map(String);
tids = topicData.filter(function (topic) {
return topic && topic.cid && !isCidIgnored[topic.cid] && (!cids || (cids.length && cids.includes(topic.cid.toString())));
2018-10-22 13:07:02 -04:00
}).map(topic => topic.tid);
plugins.fireHook('filter:topics.filterSortedTids', { tids: tids, params: params }, next);
},
function (data, next) {
next(null, data && data.tids);
2018-10-17 13:33:38 -04:00
},
], callback);
}
function getTopics(tids, params, callback) {
async.waterfall([
function (next) {
tids = tids.slice(params.start, params.stop !== -1 ? params.stop + 1 : undefined);
Topics.getTopicsByTids(tids, params.uid, next);
},
function (topicData, next) {
Topics.calculateTopicIndices(topicData, params.start);
2018-10-17 13:33:38 -04:00
next(null, topicData);
},
], callback);
}
Topics.calculateTopicIndices = function (topicData, start) {
topicData.forEach((topic, index) => {
if (topic) {
topic.index = start + index;
}
});
};
2018-10-17 13:33:38 -04:00
};