mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 08:55:47 +01:00
faster getTeasers
This commit is contained in:
@@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user