Cache the number of replies in the post object. See #5050.

https://github.com/NodeBB/NodeBB/pull/5050#pullrequestreview-4248269
v1.18.x
Ben Lubar 8 years ago
parent 5d66811213
commit 123c66e3ec

@ -260,21 +260,11 @@ var plugins = require('./plugins');
}); });
}; };
Posts.countReplies = function(pid, callback) { Posts.getReplyPids = function (pid, callback) {
if (Array.isArray(pid)) {
db.sortedSetsCard(pid.map(function (id) {
return 'pid:' + id + ':replies';
}), callback);
} else {
db.sortedSetCard('pid:' + pid + ':replies', callback);
}
};
Posts.getReplyPids = function(pid, callback) {
db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback); db.getSortedSetRange('pid:' + pid + ':replies', 0, -1, callback);
}; };
Posts.getReplyPosts = function(pid, uid, callback) { Posts.getReplyPosts = function (pid, uid, callback) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
Posts.getReplyPids(pid, next); Posts.getReplyPids(pid, next);

@ -86,7 +86,10 @@ module.exports = function (Posts) {
if (!postData.toPid) { if (!postData.toPid) {
return next(null); return next(null);
} }
db.sortedSetAdd('pid:' + postData.toPid + ':replies', timestamp, postData.pid, next); async.parallel([
async.apply(db.sortedSetAdd, 'pid:' + postData.toPid + ':replies', timestamp, postData.pid),
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies')
], next);
}, },
function (next) { function (next) {
db.incrObjectField('global', 'postCount', next); db.incrObjectField('global', 'postCount', next);

@ -137,7 +137,7 @@ module.exports = function (Posts) {
function (next) { function (next) {
deletePostFromUsersVotes(pid, next); deletePostFromUsersVotes(pid, next);
}, },
function(next) { function (next) {
Posts.getPostField(pid, 'toPid', function (err, toPid) { Posts.getPostField(pid, 'toPid', function (err, toPid) {
if (err) { if (err) {
return next(err); return next(err);
@ -145,7 +145,10 @@ module.exports = function (Posts) {
if (!parseInt(toPid, 10)) { if (!parseInt(toPid, 10)) {
return next(null); return next(null);
} }
db.sortedSetRemove('pid:' + toPid + ':replies', pid, next); async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
async.apply(db.decrObjectField, 'post:' + toPid, 'replies')
], next);
}); });
}, },
function (next) { function (next) {

@ -65,9 +65,6 @@ module.exports = function (Topics) {
voteData: function (next) { voteData: function (next) {
posts.getVoteStatusByPostIDs(pids, uid, next); posts.getVoteStatusByPostIDs(pids, uid, next);
}, },
replies: function (next) {
posts.countReplies(pids, next);
},
userData: function (next) { userData: function (next) {
var uids = []; var uids = [];
@ -126,7 +123,7 @@ module.exports = function (Topics) {
postObj.upvoted = results.voteData.upvotes[i]; postObj.upvoted = results.voteData.upvotes[i];
postObj.downvoted = results.voteData.downvotes[i]; postObj.downvoted = results.voteData.downvotes[i];
postObj.votes = postObj.votes || 0; postObj.votes = postObj.votes || 0;
postObj.replies = results.replies[i] || 0; postObj.replies = postObj.replies || 0;
postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10); postObj.selfPost = !!parseInt(uid, 10) && parseInt(uid, 10) === parseInt(postObj.uid, 10);
// Username override for guests, if enabled // Username override for guests, if enabled

@ -908,7 +908,7 @@ Upgrade.upgrade = function (callback) {
next(); next();
} }
}, },
function(next) { function (next) {
thisSchemaDate = Date.UTC(2016, 9, 14); thisSchemaDate = Date.UTC(2016, 9, 14);
if (schemaDate < thisSchemaDate) { if (schemaDate < thisSchemaDate) {
@ -917,20 +917,23 @@ Upgrade.upgrade = function (callback) {
var posts = require('./posts'); var posts = require('./posts');
var batch = require('./batch'); var batch = require('./batch');
batch.processSortedSet('posts:pid', function(ids, next) { batch.processSortedSet('posts:pid', function (ids, next) {
posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function(err, data) { posts.getPostsFields(ids, ['pid', 'toPid', 'timestamp'], function (err, data) {
if (err) { if (err) {
return next(err); return next(err);
} }
async.each(data, function(postData, next) { async.each(data, function (postData, next) {
if (!parseInt(post.toPid, 10)) { if (!parseInt(post.toPid, 10)) {
return next(null); return next(null);
} }
db.sortedSetAdd('pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid, next); async.parallel([
async.apply(db.sortedSetAdd, 'pid:' + postData.toPid + ':replies', postData.timestamp, postData.pid),
async.apply(db.incrObjectField, 'post:' + postData.toPid, 'replies')
], next);
}, next); }, next);
}); });
}, function(err) { }, function (err) {
if (err) { if (err) {
return next(err); return next(err);
} }

Loading…
Cancel
Save