2018-10-17 13:33:38 -04:00
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-12-12 11:15:34 -05:00
|
|
|
const async = require('async');
|
|
|
|
|
const _ = require('lodash');
|
2018-10-17 13:33:38 -04:00
|
|
|
|
2018-12-12 11:15:34 -05: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) {
|
|
|
|
|
key = params.cids.map(function (cid) {
|
|
|
|
|
if (params.sort === 'recent') {
|
|
|
|
|
return 'cid:' + cid + ':tids:lastposttime';
|
|
|
|
|
} else if (params.sort === 'votes') {
|
|
|
|
|
return 'cid:' + cid + ':tids:votes';
|
|
|
|
|
} else if (params.sort === 'posts') {
|
|
|
|
|
return 'cid:' + cid + ':tids:posts';
|
|
|
|
|
}
|
|
|
|
|
return 'cid:' + cid + ':tids';
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db.getSortedSetRevRange(key, 0, 199, next);
|
|
|
|
|
} else {
|
|
|
|
|
Topics.getLatestTidsFromSet('topics:tid', 0, -1, params.term, next);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
function (tids, next) {
|
|
|
|
|
if (params.term !== 'alltime') {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2018-12-12 11:15:34 -05:00
|
|
|
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) {
|
2018-12-12 11:15:34 -05:00
|
|
|
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) {
|
2018-10-21 16:47:51 -04:00
|
|
|
if (filter === 'watched' || meta.config.disableRecentCategoryFilter) {
|
2018-10-17 13:33:38 -04:00
|
|
|
return next(null, []);
|
|
|
|
|
}
|
2018-12-12 11:15:34 -05:00
|
|
|
categories.isIgnored(topicCids, uid, next);
|
2018-10-17 13:33:38 -04:00
|
|
|
},
|
2018-12-12 11:15:34 -05:00
|
|
|
filtered: async.apply(user.blocks.filter, uid, topicData),
|
2018-10-17 13:33:38 -04:00
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
2018-12-12 11:15:34 -05:00
|
|
|
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);
|
2018-12-12 11:15:34 -05:00
|
|
|
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) {
|
2018-10-27 06:26:50 -04:00
|
|
|
Topics.calculateTopicIndices(topicData, params.start);
|
2018-10-17 13:33:38 -04:00
|
|
|
next(null, topicData);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
2018-10-27 06:26:50 -04:00
|
|
|
|
|
|
|
|
Topics.calculateTopicIndices = function (topicData, start) {
|
|
|
|
|
topicData.forEach((topic, index) => {
|
|
|
|
|
if (topic) {
|
|
|
|
|
topic.index = start + index;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2018-10-17 13:33:38 -04:00
|
|
|
};
|