From b4d8b7cf386384a13727c67b135c009614193a0d Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Wed, 17 Oct 2018 13:33:38 -0400 Subject: [PATCH] move sorted topics code out of recent --- src/topics.js | 1 + src/topics/recent.js | 176 ----------------------------------------- src/topics/sorted.js | 184 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 176 deletions(-) create mode 100644 src/topics/sorted.js diff --git a/src/topics.js b/src/topics.js index daf3db8790..7c7a902a5a 100644 --- a/src/topics.js +++ b/src/topics.js @@ -18,6 +18,7 @@ var Topics = module.exports; require('./topics/data')(Topics); require('./topics/create')(Topics); require('./topics/delete')(Topics); +require('./topics/sorted')(Topics); require('./topics/unread')(Topics); require('./topics/recent')(Topics); require('./topics/user')(Topics); diff --git a/src/topics/recent.js b/src/topics/recent.js index c14f4bf852..8e5f287f0a 100644 --- a/src/topics/recent.js +++ b/src/topics/recent.js @@ -7,9 +7,6 @@ var winston = require('winston'); var db = require('../database'); var plugins = require('../plugins'); -var privileges = require('../privileges'); -var user = require('../user'); -var meta = require('../meta'); var posts = require('../posts'); module.exports = function (Topics) { @@ -20,125 +17,6 @@ module.exports = function (Topics) { year: 31104000000, }; - Topics.getSortedTopics = function (params, callback) { - var data = { - nextStart: 0, - topicCount: 0, - topics: [], - }; - - params.term = params.term || 'alltime'; - params.sort = params.sort || 'recent'; - 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) { - filterTids(tids, params.uid, params.filter, params.cids, next); - }, - ], 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; - } - tids = topicData.sort(sortFn).map(function (topic) { - return topic && topic.tid; - }); - next(null, tids); - }, - ], callback); - } - - function sortRecent(a, b) { - return b.lastposttime - a.lastposttime; - } - - function sortVotes(a, b) { - if (parseInt(a.votes, 10) !== parseInt(b.votes, 10)) { - return b.votes - a.votes; - } - return parseInt(b.postcount, 10) - parseInt(a.postcount, 10); - } - - function sortPopular(a, b) { - if (parseInt(a.postcount, 10) !== parseInt(b.postcount, 10)) { - return b.postcount - a.postcount; - } - return parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); - } - - 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) { - topicData.forEach(function (topicObj, i) { - topicObj.index = params.start + i; - }); - next(null, topicData); - }, - ], callback); - } - Topics.getRecentTopics = function (cid, uid, start, stop, filter, callback) { Topics.getSortedTopics({ cids: cid, @@ -150,60 +28,6 @@ module.exports = function (Topics) { }, callback); }; - function filterTids(tids, uid, filter, cids, callback) { - 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) { - async.parallel({ - ignoredCids: function (next) { - if (filter === 'watched' || parseInt(meta.config.disableRecentCategoryFilter, 10) === 1) { - return next(null, []); - } - user.getIgnoredCategories(uid, next); - }, - topicData: function (next) { - Topics.getTopicsFields(tids, ['uid', 'tid', 'cid'], next); - }, - }, next); - }, - function (results, next) { - user.blocks.filter(uid, results.topicData, function (err, filtered) { - if (err) { - return next(err); - } - - results.topicData = filtered; - next(null, results); - }); - }, - function (results, next) { - cids = cids && cids.map(String); - tids = results.topicData.filter(function (topic) { - if (topic && topic.cid) { - return !results.ignoredCids.includes(topic.cid.toString()) && (!cids || (cids.length && cids.includes(topic.cid.toString()))); - } - return false; - }).map(function (topic) { - return topic.tid; - }); - next(null, tids); - }, - ], callback); - } - /* not an orphan method, used in widget-essentials */ Topics.getLatestTopics = function (uid, start, stop, term, callback) { async.waterfall([ diff --git a/src/topics/sorted.js b/src/topics/sorted.js new file mode 100644 index 0000000000..4a42bb528f --- /dev/null +++ b/src/topics/sorted.js @@ -0,0 +1,184 @@ + +'use strict'; + +var async = require('async'); + +var db = require('../database'); +var privileges = require('../privileges'); +var user = require('../user'); +var meta = require('../meta'); + +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'; + 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) { + filterTids(tids, params.uid, params.filter, params.cids, next); + }, + ], 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; + } + tids = topicData.sort(sortFn).map(function (topic) { + return topic && topic.tid; + }); + next(null, tids); + }, + ], callback); + } + + function sortRecent(a, b) { + return b.lastposttime - a.lastposttime; + } + + function sortVotes(a, b) { + if (parseInt(a.votes, 10) !== parseInt(b.votes, 10)) { + return b.votes - a.votes; + } + return parseInt(b.postcount, 10) - parseInt(a.postcount, 10); + } + + function sortPopular(a, b) { + if (parseInt(a.postcount, 10) !== parseInt(b.postcount, 10)) { + return b.postcount - a.postcount; + } + return parseInt(b.viewcount, 10) - parseInt(a.viewcount, 10); + } + + function filterTids(tids, uid, filter, cids, callback) { + 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) { + async.parallel({ + ignoredCids: function (next) { + if (filter === 'watched' || parseInt(meta.config.disableRecentCategoryFilter, 10) === 1) { + return next(null, []); + } + user.getIgnoredCategories(uid, next); + }, + topicData: function (next) { + Topics.getTopicsFields(tids, ['uid', 'tid', 'cid'], next); + }, + }, next); + }, + function (results, next) { + user.blocks.filter(uid, results.topicData, function (err, filtered) { + if (err) { + return next(err); + } + + results.topicData = filtered; + next(null, results); + }); + }, + function (results, next) { + cids = cids && cids.map(String); + tids = results.topicData.filter(function (topic) { + if (topic && topic.cid) { + return !results.ignoredCids.includes(topic.cid.toString()) && (!cids || (cids.length && cids.includes(topic.cid.toString()))); + } + return false; + }).map(function (topic) { + return topic.tid; + }); + next(null, tids); + }, + ], 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) { + topicData.forEach(function (topicObj, i) { + topicObj.index = params.start + i; + }); + next(null, topicData); + }, + ], callback); + } +};