From 31f1914ec80224ffb73d02c298d56716b9012ebc Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 17 Jun 2014 11:56:47 +0700 Subject: [PATCH 1/3] Update global postCount when deleting/restoring topic --- src/topics/delete.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/topics/delete.js b/src/topics/delete.js index d8b7ee9472..f2a4dd59a4 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -8,6 +8,24 @@ var async = require('async'), module.exports = function(Topics) { + function updateGlobalCounters(tid, incr, callback) { + console.log('updateGlobalCounters'); + async.parallel([ + function(next) { + db.incrObjectFieldBy('global', 'topicCount', incr, next); + }, + function(next) { + Topics.getPostCount(tid, function(err, postCount) { + if (err) { + return next(err); + } + postCount = parseInt(postCount, 10) + 1; + db.incrObjectFieldBy('global', 'postCount', incr * postCount, next); + }); + } + ], callback); + } + Topics.delete = function(tid, callback) { async.parallel([ function(next) { @@ -35,7 +53,7 @@ module.exports = function(Topics) { return callback(err); } - Topics.updateTopicCount(callback); + updateGlobalCounters(tid, -1, callback); }); }; @@ -71,7 +89,7 @@ module.exports = function(Topics) { return callback(err); } - Topics.updateTopicCount(callback); + updateGlobalCounters(tid, 1, callback); }); }); }; @@ -135,7 +153,7 @@ module.exports = function(Topics) { db.decrObjectField('category:' + topicData.cid, 'topic_count', next); }, function(next) { - db.decrObjectField('global', 'topicCount', callback); + updateGlobalCounters(tid, -1, next); } ], callback); } else { From 1448bed05785e6f1d28741b84412089375b48af0 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 17 Jun 2014 11:57:55 +0700 Subject: [PATCH 2/3] Remove console.log --- src/topics/delete.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/topics/delete.js b/src/topics/delete.js index f2a4dd59a4..85301c0dd6 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -9,7 +9,6 @@ var async = require('async'), module.exports = function(Topics) { function updateGlobalCounters(tid, incr, callback) { - console.log('updateGlobalCounters'); async.parallel([ function(next) { db.incrObjectFieldBy('global', 'topicCount', incr, next); From b811bef42c88a8027a0bc3319b059ef953b602bd Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 17 Jun 2014 12:34:09 +0700 Subject: [PATCH 3/3] Don't decrease postCount when purging post from a deleted topic --- src/posts/delete.js | 21 +++++++++++++-------- src/topics/delete.js | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/posts/delete.js b/src/posts/delete.js index d46e57a6a5..d3d153b9a3 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -2,10 +2,9 @@ var async = require('async'), db = require('../database'), - + topics = require('../topics'), plugins = require('../plugins'); - module.exports = function(Posts) { Posts.purge = function(pid, callback) { @@ -56,11 +55,17 @@ module.exports = function(Posts) { return callback(err); } - if (parseInt(postData.deleted, 10) === 0) { - db.decrObjectField('global', 'postCount', callback); - } else { - callback(); - } + topics.getTopicFields(postData.tid, ['deleted'], function(err, topicData) { + if (err) { + return callback(err); + } + + if (parseInt(postData.deleted, 10) === 0 && parseInt(topicData.deleted, 10) !== 1) { + db.decrObjectField('global', 'postCount', callback); + } else { + callback(); + } + }); }); }); } @@ -124,4 +129,4 @@ module.exports = function(Posts) { } -}; \ No newline at end of file +}; diff --git a/src/topics/delete.js b/src/topics/delete.js index 85301c0dd6..e378bae04c 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -152,7 +152,7 @@ module.exports = function(Topics) { db.decrObjectField('category:' + topicData.cid, 'topic_count', next); }, function(next) { - updateGlobalCounters(tid, -1, next); + db.decrObjectField('global', 'topicCount', next); } ], callback); } else {