mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
optimized home
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
|
||||
var async = require('async'),
|
||||
winston = require('winston'),
|
||||
_ = require('underscore'),
|
||||
|
||||
db = require('../database'),
|
||||
posts = require('../posts'),
|
||||
@@ -23,19 +24,51 @@ module.exports = function(Categories) {
|
||||
});
|
||||
};
|
||||
|
||||
Categories.getRecentTopicReplies = function(cid, uid, count, callback) {
|
||||
Categories.getRecentTopicReplies = function(categoryData, callback) {
|
||||
async.map(categoryData, function(category, next) {
|
||||
getRecentTopicPids(category.cid, parseInt(category.numRecentReplies), next);
|
||||
}, function(err, results) {
|
||||
var pids = _.flatten(results);
|
||||
|
||||
pids = pids.filter(function(pid, index, array) {
|
||||
return !!pid && array.indexOf(pid) === index;
|
||||
});
|
||||
|
||||
posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.each(categoryData, function(category, next) {
|
||||
assignPostsToCategory(category, posts, next);
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function assignPostsToCategory(category, posts, next) {
|
||||
category.posts = posts.filter(function(post) {
|
||||
return parseInt(post.category.cid, 10) === parseInt(category.cid);
|
||||
}).sort(function(a, b) {
|
||||
return parseInt(b.timestamp, 10) - parseInt(a.timestamp, 10);
|
||||
}).slice(0, parseInt(category.numRecentReplies, 10));
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
function getRecentTopicPids(cid, count, callback) {
|
||||
count = parseInt(count, 10);
|
||||
if (!count) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
db.getSortedSetRevRange('categories:recent_posts:cid:' + cid, 0, 0, function(err, pids) {
|
||||
if (err || !pids || !pids.length) {
|
||||
if (err || !Array.isArray(pids) || !pids.length) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
return posts.getPostSummaryByPids(pids, {stripTags: true}, callback);
|
||||
return callback(null, pids);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
@@ -60,22 +93,12 @@ module.exports = function(Categories) {
|
||||
pids = pids.concat(topicPids).filter(function(pid, index, array) {
|
||||
return !!pid && array.indexOf(pid) === index;
|
||||
});
|
||||
|
||||
posts.getPostSummaryByPids(pids, {stripTags: true}, function(err, posts) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
callback(null, pids);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
posts = posts.sort(function(a, b) {
|
||||
return parseInt(b.timestamp, 10) - parseInt(a.timestamp, 10);
|
||||
}).slice(0, count);
|
||||
|
||||
callback(err, posts);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Categories.moveRecentReplies = function(tid, oldCid, cid) {
|
||||
function movePost(postData, next) {
|
||||
|
||||
@@ -69,17 +69,7 @@ Controllers.home = function(req, res, next) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
function getRecentReplies(category, callback) {
|
||||
categories.getRecentTopicReplies(category.cid, uid, parseInt(category.numRecentReplies, 10), function (err, posts) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
category.posts = posts;
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
async.each(categoryData, getRecentReplies, function (err) {
|
||||
categories.getRecentTopicReplies(categoryData, function(err) {
|
||||
next(err, categoryData);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user