simplified getAllTopics

v1.18.x
Baris Soner Usakli 11 years ago
parent e5727af959
commit c0e0da0f1d

@ -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

@ -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) {

@ -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 */

@ -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);
});
};

Loading…
Cancel
Save