mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 11:35:55 +01:00
Cache the number of replies in the post object. See #5050.
https://github.com/NodeBB/NodeBB/pull/5050#pullrequestreview-4248269
This commit is contained in:
10
src/posts.js
10
src/posts.js
@@ -260,16 +260,6 @@ var plugins = require('./plugins');
|
||||
});
|
||||
};
|
||||
|
||||
Posts.countReplies = 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);
|
||||
};
|
||||
|
||||
@@ -86,7 +86,10 @@ module.exports = function (Posts) {
|
||||
if (!postData.toPid) {
|
||||
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) {
|
||||
db.incrObjectField('global', 'postCount', next);
|
||||
|
||||
@@ -145,7 +145,10 @@ module.exports = function (Posts) {
|
||||
if (!parseInt(toPid, 10)) {
|
||||
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) {
|
||||
|
||||
@@ -65,9 +65,6 @@ module.exports = function (Topics) {
|
||||
voteData: function (next) {
|
||||
posts.getVoteStatusByPostIDs(pids, uid, next);
|
||||
},
|
||||
replies: function (next) {
|
||||
posts.countReplies(pids, next);
|
||||
},
|
||||
userData: function (next) {
|
||||
var uids = [];
|
||||
|
||||
@@ -126,7 +123,7 @@ module.exports = function (Topics) {
|
||||
postObj.upvoted = results.voteData.upvotes[i];
|
||||
postObj.downvoted = results.voteData.downvotes[i];
|
||||
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);
|
||||
|
||||
// Username override for guests, if enabled
|
||||
|
||||
@@ -927,7 +927,10 @@ Upgrade.upgrade = function (callback) {
|
||||
if (!parseInt(post.toPid, 10)) {
|
||||
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);
|
||||
});
|
||||
}, function (err) {
|
||||
|
||||
Reference in New Issue
Block a user