faster getTeasers

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

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

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

Loading…
Cancel
Save