From c0e0da0f1dc4470d3bbb545b98e64cf8cfc37fa6 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Fri, 14 Feb 2014 13:38:10 -0500 Subject: [PATCH] simplified getAllTopics --- src/routes/admin.js | 2 +- src/sitemap.js | 2 +- src/socket.io/admin.js | 5 +++- src/topics.js | 60 ++++++++---------------------------------- 4 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/routes/admin.js b/src/routes/admin.js index 05a2bbf8d2..c0afc39b4b 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -332,7 +332,7 @@ var nconf = require('nconf'), }); app.get('/topics', function (req, res) { - topics.getAllTopics(10, null, function (err, topics) { + topics.getAllTopics(0, 19, function (err, topics) { res.json({ topics: topics, notopics: topics.length === 0 diff --git a/src/sitemap.js b/src/sitemap.js index 8d478163b0..89b3a4b2ba 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -43,7 +43,7 @@ var path = require('path'), }, function(next) { var topicUrls = []; - topics.getAllTopics(null, null, function(err, topics) { + topics.getAllTopics(0, -1, function(err, topics) { topics.forEach(function(topic) { if (parseInt(topic.deleted, 10) !== 1) { diff --git a/src/socket.io/admin.js b/src/socket.io/admin.js index 5728bbc0c7..535be06be5 100644 --- a/src/socket.io/admin.js +++ b/src/socket.io/admin.js @@ -39,7 +39,10 @@ SocketAdmin.topics.getMore = function(socket, data, callback) { return callback(new Error('invalid data')); } - topics.getAllTopics(data.limit, data.after, callback); + var start = parseInt(data.after, 10), + end = start + parseInt(data.limit, 10); + + topics.getAllTopics(start, end, callback); }; /* User */ diff --git a/src/topics.js b/src/topics.js index 07fb5b14c4..3151a70705 100644 --- a/src/topics.js +++ b/src/topics.js @@ -424,12 +424,12 @@ var async = require('async'), }; function getTopics(set, uid, tids, callback) { - var latestTopics = { + var returnTopics = { 'topics': [] }; if (!tids || !tids.length) { - return callback(null, latestTopics); + return callback(null, returnTopics); } async.filter(tids, function(tid, next) { @@ -443,7 +443,7 @@ var async = require('async'), } if(!topicData || !topicData.length) { - return callback(null, latestTopics); + return callback(null, returnTopics); } db.sortedSetRevRank(set, topicData[topicData.length - 1].tid, function(err, rank) { @@ -451,9 +451,9 @@ var async = require('async'), return calllback(err); } - latestTopics.nextStart = parseInt(rank, 10) + 1; - latestTopics.topics = topicData; - callback(null, latestTopics); + returnTopics.nextStart = parseInt(rank, 10) + 1; + returnTopics.topics = topicData; + callback(null, returnTopics); }); }); }); @@ -905,49 +905,11 @@ var async = require('async'), }); }; - Topics.getAllTopics = function(limit, after, callback) { - db.getSetMembers('topics:tid', function(err, tids) { - if(err) { - return callback(err, null); - } - - var topics = [], - numTids, x; - - // Sort into ascending order - tids.sort(function(a, b) { - return a - b; - }); - - // Eliminate everything after the "after" tid - if (after) { - for (x = 0, numTids = tids.length; x < numTids; x++) { - if (tids[x] >= after) { - tids = tids.slice(0, x); - break; - } - } - } - - if (limit) { - if (limit > 0 && limit < tids.length) { - tids = tids.slice(tids.length - limit); - } - } - - // Sort into descending order - tids.sort(function(a, b) { - return b - a; - }); - - async.each(tids, function(tid, next) { - Topics.getTopicDataWithUser(tid, function(err, topicData) { - topics.push(topicData); - next(); - }); - }, function(err) { - callback(err, topics); - }); + Topics.getAllTopics = function(start, end, callback) { + db.getSortedSetRevRange('topics:recent', start, end, function(err, tids) { + async.map(tids, function(tid, next) { + Topics.getTopicDataWithUser(tid, next); + }, callback); }); };