mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-12 00:45: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,68 +12,76 @@ 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) {
|
var counts = [];
|
||||||
|
var teaserPids = [];
|
||||||
|
|
||||||
|
topics.forEach(function(topic) {
|
||||||
|
counts.push(topic && (parseInt(topic.postcount, 10) || 0));
|
||||||
|
if (topic && topic.teaserPid) {
|
||||||
|
teaserPids.push(topic.teaserPid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
var counts = [];
|
|
||||||
var teaserPids = [];
|
|
||||||
|
|
||||||
topics.forEach(function(topic) {
|
var uids = postData.map(function(post) {
|
||||||
counts.push(topic && (parseInt(topic.postcount, 10) || 0));
|
return post.uid;
|
||||||
if (topic && topic.teaserPid) {
|
}).filter(function(uid, index, array) {
|
||||||
teaserPids.push(topic.teaserPid);
|
return array.indexOf(uid) === index;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
posts.getPostsFields(teaserPids, ['pid', 'uid', 'timestamp', 'tid'], function(err, postData) {
|
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, usersData) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
var uids = postData.map(function(post) {
|
var users = {};
|
||||||
return post.uid;
|
usersData.forEach(function(user) {
|
||||||
}).filter(function(uid, index, array) {
|
users[user.uid] = user;
|
||||||
return array.indexOf(uid) === index;
|
});
|
||||||
|
var tidToPost = {};
|
||||||
|
postData.forEach(function(post) {
|
||||||
|
post.user = users[post.uid];
|
||||||
|
post.timestamp = utils.toISOString(post.timestamp);
|
||||||
|
tidToPost[post.tid] = post;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var teasers = topics.map(function(topic, index) {
|
||||||
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, usersData) {
|
if (tidToPost[topic.tid]) {
|
||||||
if (err) {
|
tidToPost[topic.tid].index = counts[index];
|
||||||
return callback(err);
|
|
||||||
}
|
}
|
||||||
|
return tidToPost[topic.tid];
|
||||||
var users = {};
|
|
||||||
usersData.forEach(function(user) {
|
|
||||||
users[user.uid] = user;
|
|
||||||
});
|
|
||||||
var tidToPost = {};
|
|
||||||
postData.forEach(function(post) {
|
|
||||||
post.user = users[post.uid];
|
|
||||||
post.timestamp = utils.toISOString(post.timestamp);
|
|
||||||
tidToPost[post.tid] = post;
|
|
||||||
});
|
|
||||||
|
|
||||||
var teasers = tids.map(function(tid, index) {
|
|
||||||
if (tidToPost[tid]) {
|
|
||||||
tidToPost[tid].index = counts[index];
|
|
||||||
}
|
|
||||||
return tidToPost[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