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({
|
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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user