mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
performance improvements
store parsed category description removed mongo _key from returns dont get category teaser for parent
This commit is contained in:
@@ -53,17 +53,11 @@ module.exports = function(Categories) {
|
||||
}
|
||||
|
||||
if (category.description) {
|
||||
plugins.fireHook('filter:parse.raw', category.description, function(err, parsedDescription) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
category.descriptionParsed = parsedDescription;
|
||||
category.description = validator.escape(category.description);
|
||||
callback(null, category);
|
||||
});
|
||||
} else {
|
||||
callback(null, category);
|
||||
category.descriptionParsed = category.descriptionParsed || category.description;
|
||||
}
|
||||
|
||||
callback(null, category);
|
||||
}
|
||||
|
||||
Categories.getCategoryField = function(cid, field, callback) {
|
||||
|
||||
@@ -3,14 +3,13 @@
|
||||
|
||||
var async = require('async'),
|
||||
winston = require('winston'),
|
||||
validator = require('validator'),
|
||||
_ = require('underscore'),
|
||||
|
||||
meta = require('../meta'),
|
||||
db = require('../database'),
|
||||
posts = require('../posts'),
|
||||
topics = require('../topics'),
|
||||
privileges = require('../privileges'),
|
||||
plugins = require('../plugins');
|
||||
privileges = require('../privileges');
|
||||
|
||||
module.exports = function(Categories) {
|
||||
Categories.getRecentReplies = function(cid, uid, count, callback) {
|
||||
@@ -38,25 +37,21 @@ module.exports = function(Categories) {
|
||||
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
async.map(categoryData, getRecentTopicPids, next);
|
||||
async.map(categoryData, getRecentTopicTids, next);
|
||||
},
|
||||
function(results, next) {
|
||||
var pids = _.flatten(results);
|
||||
var tids = _.flatten(results);
|
||||
|
||||
pids = pids.filter(function(pid, index, array) {
|
||||
return !!pid && array.indexOf(pid) === index;
|
||||
tids = tids.filter(function(tid, index, array) {
|
||||
return !!tid && array.indexOf(tid) === index;
|
||||
});
|
||||
privileges.posts.filter('read', pids, uid, next);
|
||||
privileges.topics.filterTids('read', tids, uid, next);
|
||||
},
|
||||
function(pids, next) {
|
||||
if (meta.config.teaserPost === 'first') {
|
||||
getMainPosts(pids, uid, next);
|
||||
} else {
|
||||
posts.getPostSummaryByPids(pids, uid, {stripTags: true}, next);
|
||||
}
|
||||
function(tids, next) {
|
||||
getTopics(tids, next);
|
||||
},
|
||||
function(posts, next) {
|
||||
assignPostsToCategories(categoryData, posts);
|
||||
function(topics, next) {
|
||||
assignTopicsToCategories(categoryData, topics);
|
||||
|
||||
bubbleUpChildrenPosts(categoryData);
|
||||
|
||||
@@ -65,27 +60,84 @@ module.exports = function(Categories) {
|
||||
], callback);
|
||||
};
|
||||
|
||||
function getMainPosts(pids, uid, callback) {
|
||||
function getRecentTopicTids(category, callback) {
|
||||
var count = parseInt(category.numRecentReplies, 10);
|
||||
if (!count) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
var keys = pids.map(function(pid) {
|
||||
return 'post:' + pid;
|
||||
});
|
||||
db.getObjectsFields(keys, ['tid'], next);
|
||||
function (next) {
|
||||
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, next);
|
||||
},
|
||||
function(posts, next) {
|
||||
var keys = posts.map(function(post) {
|
||||
return 'topic:' + post.tid;
|
||||
});
|
||||
db.getObjectsFields(keys, ['mainPid'], next);
|
||||
function (pid, next) {
|
||||
posts.getPostField(pid, 'tid', next);
|
||||
},
|
||||
function(topics, next) {
|
||||
var mainPids = topics.map(function(topic) {
|
||||
return topic.mainPid;
|
||||
});
|
||||
posts.getPostSummaryByPids(mainPids, uid, {stripTags: true}, next);
|
||||
function (tid, next) {
|
||||
next(null, [tid]);
|
||||
}
|
||||
], callback);
|
||||
return;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
pinnedTids: function(next) {
|
||||
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
|
||||
},
|
||||
tids: function(next) {
|
||||
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(1, count), Date.now(), 0, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
results.tids = results.tids.concat(results.pinnedTids);
|
||||
|
||||
callback(null, results.tids);
|
||||
});
|
||||
}
|
||||
|
||||
function getTopics(tids, callback) {
|
||||
var topicData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
topics.getTopicsFields(tids, ['tid', 'mainPid', 'slug', 'title', 'teaserPid', 'cid', 'postcount'], next);
|
||||
},
|
||||
function (_topicData, next) {
|
||||
topicData = _topicData;
|
||||
topicData.forEach(function(topic) {
|
||||
topic.teaserPid = topic.teaserPid || topic.mainPid;
|
||||
});
|
||||
|
||||
topics.getTeasers(topicData, next);
|
||||
},
|
||||
function (teasers, next) {
|
||||
teasers.forEach(function(teaser, index) {
|
||||
if (teaser) {
|
||||
teaser.cid = topicData[index].cid;
|
||||
teaser.tid = teaser.uid = teaser.user.uid = undefined;
|
||||
teaser.topic = {
|
||||
slug: topicData[index].slug,
|
||||
title: validator.escape(topicData[index].title)
|
||||
};
|
||||
}
|
||||
});
|
||||
teasers = teasers.filter(Boolean);
|
||||
next(null, teasers);
|
||||
}
|
||||
], callback);
|
||||
}
|
||||
|
||||
function assignTopicsToCategories(categories, topics) {
|
||||
categories.forEach(function(category) {
|
||||
category.posts = topics.filter(function(topic) {
|
||||
return topic.cid && parseInt(topic.cid, 10) === parseInt(category.cid, 10);
|
||||
}).sort(function(a, b) {
|
||||
return b.pid - a.pid;
|
||||
}).slice(0, parseInt(category.numRecentReplies, 10));
|
||||
});
|
||||
}
|
||||
|
||||
function bubbleUpChildrenPosts(categoryData) {
|
||||
@@ -97,7 +149,7 @@ module.exports = function(Categories) {
|
||||
getPostsRecursive(category, posts);
|
||||
|
||||
posts.sort(function(a, b) {
|
||||
return b.timestamp - a.timestamp;
|
||||
return b.pid - a.pid;
|
||||
});
|
||||
if (posts.length) {
|
||||
category.posts = [posts[0]];
|
||||
@@ -115,64 +167,6 @@ module.exports = function(Categories) {
|
||||
});
|
||||
}
|
||||
|
||||
function assignPostsToCategories(categories, posts) {
|
||||
categories.forEach(function(category) {
|
||||
category.posts = posts.filter(function(post) {
|
||||
return post.category && (parseInt(post.category.cid, 10) === parseInt(category.cid, 10) ||
|
||||
parseInt(post.category.parentCid, 10) === parseInt(category.cid, 10));
|
||||
}).sort(function(a, b) {
|
||||
return b.timestamp - a.timestamp;
|
||||
}).slice(0, parseInt(category.numRecentReplies, 10));
|
||||
});
|
||||
}
|
||||
|
||||
function getRecentTopicPids(category, callback) {
|
||||
var count = parseInt(category.numRecentReplies, 10);
|
||||
if (!count) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
db.getSortedSetRevRange('cid:' + category.cid + ':pids', 0, 0, function(err, pids) {
|
||||
if (err || !Array.isArray(pids) || !pids.length) {
|
||||
return callback(err, []);
|
||||
}
|
||||
|
||||
if (count === 1) {
|
||||
return callback(null, pids);
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
pinnedTids: function(next) {
|
||||
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, -1, '+inf', Date.now(), next);
|
||||
},
|
||||
tids: function(next) {
|
||||
db.getSortedSetRevRangeByScore('cid:' + category.cid + ':tids', 0, Math.max(0, count), Date.now(), 0, next);
|
||||
}
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
results.tids = results.tids.concat(results.pinnedTids);
|
||||
|
||||
async.map(results.tids, topics.getLatestUndeletedPid, function(err, topicPids) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
pids = pids.concat(topicPids).filter(function(pid, index, array) {
|
||||
return !!pid && array.indexOf(pid) === index;
|
||||
}).sort(function(a, b) {
|
||||
return b - a;
|
||||
}).slice(0, count);
|
||||
|
||||
callback(null, pids);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Categories.moveRecentReplies = function(tid, oldCid, cid) {
|
||||
updatePostCount(tid, oldCid, cid);
|
||||
topics.getPids(tid, function(err, pids) {
|
||||
|
||||
@@ -68,6 +68,8 @@ module.exports = function(Categories) {
|
||||
|
||||
if (key === 'order') {
|
||||
updateOrder(cid, value, callback);
|
||||
} else if (key === 'description') {
|
||||
parseDescription(cid, value, callback);
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
@@ -119,4 +121,13 @@ module.exports = function(Categories) {
|
||||
});
|
||||
}
|
||||
|
||||
function parseDescription(cid, description, callback) {
|
||||
plugins.fireHook('filter:parse.raw', description, function(err, parsedDescription) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
Categories.setCategoryField(cid, 'descriptionParsed', parsedDescription, callback);
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -69,7 +69,7 @@ categoriesController.list = function(req, res, next) {
|
||||
if (category && Array.isArray(category.posts) && category.posts.length) {
|
||||
category.teaser = {
|
||||
url: nconf.get('relative_path') + '/topic/' + category.posts[0].topic.slug + '/' + category.posts[0].index,
|
||||
timestampISO: category.posts[0].relativeTime
|
||||
timestampISO: category.posts[0].timestamp
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -203,8 +203,11 @@ categoriesController.get = function(req, res, callback) {
|
||||
});
|
||||
},
|
||||
function(categoryData, next) {
|
||||
if (!categoryData.children.length) {
|
||||
return next(null, categoryData);
|
||||
}
|
||||
var allCategories = [];
|
||||
categories.flattenCategories(allCategories, [categoryData]);
|
||||
categories.flattenCategories(allCategories, categoryData.children);
|
||||
categories.getRecentTopicReplies(allCategories, req.uid, function(err) {
|
||||
next(err, categoryData);
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ helpers.toMap = function(data) {
|
||||
var map = {};
|
||||
for (var i = 0; i<data.length; ++i) {
|
||||
map[data[i]._key] = data[i];
|
||||
data[i]._key = undefined;
|
||||
}
|
||||
return map;
|
||||
};
|
||||
|
||||
@@ -125,7 +125,7 @@ module.exports = function(db, module) {
|
||||
if (withScores) {
|
||||
fields.score = 1;
|
||||
}
|
||||
db.collection('objects').find({_key:key}, {fields: fields})
|
||||
db.collection('objects').find({_key: key}, {fields: fields})
|
||||
.limit(stop - start + 1)
|
||||
.skip(start)
|
||||
.sort({score: sort})
|
||||
|
||||
Reference in New Issue
Block a user