faster getTeasers

v1.18.x
barisusakli 10 years ago
parent 14e46a87a6
commit 83312ad53a

@ -155,7 +155,7 @@ var async = require('async'),
async.parallel({
teasers: function(next) {
Topics.getTeasers(tids, next);
Topics.getTeasers(topics, next);
},
users: function(next) {
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], next);

@ -12,15 +12,11 @@ var async = require('async'),
module.exports = function(Topics) {
Topics.getTeasers = function(tids, callback) {
if (!Array.isArray(tids) || !tids.length) {
Topics.getTeasers = function(topics, callback) {
if (!Array.isArray(topics) || !topics.length) {
return callback(null, []);
}
Topics.getTopicsFields(tids, ['postcount', 'teaserPid'], function(err, topics) {
if (err) {
return callback(err);
}
var counts = [];
var teaserPids = [];
@ -42,7 +38,6 @@ module.exports = function(Topics) {
return array.indexOf(uid) === index;
});
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, usersData) {
if (err) {
return callback(err);
@ -59,21 +54,34 @@ module.exports = function(Topics) {
tidToPost[post.tid] = post;
});
var teasers = tids.map(function(tid, index) {
if (tidToPost[tid]) {
tidToPost[tid].index = counts[index];
var teasers = topics.map(function(topic, index) {
if (tidToPost[topic.tid]) {
tidToPost[topic.tid].index = counts[index];
}
return tidToPost[tid];
return tidToPost[topic.tid];
});
callback(null, teasers);
});
});
});
};
Topics.getTeasersByTids = function(tids, callback) {
if (!Array.isArray(tids) || !tids.length) {
return callback(null, []);
}
async.waterfall([
function(next) {
Topics.getTopicsFields(tids, ['tid', 'postcount', 'teaserPid'], next);
},
function(topics, next) {
Topics.getTeasers(topics, next);
}
], callback);
};
Topics.getTeaser = function(tid, callback) {
Topics.getTeasers([tid], function(err, teasers) {
Topics.getTeasersByTids([tid], function(err, teasers) {
callback(err, Array.isArray(teasers) && teasers.length ? teasers[0] : null);
});
};

Loading…
Cancel
Save