diff --git a/src/topics/delete.js b/src/topics/delete.js index 0238b123a4..679cb986ca 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -8,36 +8,6 @@ var async = require('async'), module.exports = function(Topics) { - function updateCounters(tid, incr, callback) { - async.parallel([ - function(next) { - db.incrObjectFieldBy('global', 'topicCount', incr, next); - }, - function(next) { - Topics.getTopicFields(tid, ['cid', 'postcount'], function(err, topicData) { - if (err) { - return next(err); - } - topicData.postcount = parseInt(topicData.postcount, 10); - topicData.postcount = topicData.postcount || 0; - var postCountChange = incr * topicData.postcount; - - async.parallel([ - function(next) { - db.incrObjectFieldBy('global', 'postCount', postCountChange, next); - }, - function(next) { - db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next); - }, - function(next) { - db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next); - } - ], next); - }); - } - ], callback); - } - Topics.delete = function(tid, callback) { async.parallel([ function(next) { @@ -49,18 +19,12 @@ module.exports = function(Topics) { function(next) { db.sortedSetsRemove(['topics:posts', 'topics:views'], tid, next); } - ], function(err) { - if (err) { - return callback(err); - } - - updateCounters(tid, -1, callback); - }); + ], callback); }; Topics.restore = function(tid, callback) { Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) { - if(err) { + if (err) { return callback(err); } @@ -77,13 +41,7 @@ module.exports = function(Topics) { function(next) { db.sortedSetAdd('topics:views', topicData.viewcount, tid, next); } - ], function(err) { - if (err) { - return callback(err); - } - - updateCounters(tid, 1, callback); - }); + ], callback); }); }; @@ -100,6 +58,9 @@ module.exports = function(Topics) { }, function(next) { Topics.deleteTopicTags(tid, next); + }, + function(next) { + updateCounters(tid, -1, next); } ], function(err) { if (err) { @@ -116,24 +77,37 @@ module.exports = function(Topics) { return callback(err); } - db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, function(err) { - if (err) { - return callback(err); - } + db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, callback); + }); + } + + function updateCounters(tid, incr, callback) { + async.parallel([ + function(next) { + db.incrObjectFieldBy('global', 'topicCount', incr, next); + }, + function(next) { + Topics.getTopicFields(tid, ['cid', 'postcount'], function(err, topicData) { + if (err) { + return next(err); + } + topicData.postcount = parseInt(topicData.postcount, 10); + topicData.postcount = topicData.postcount || 0; + var postCountChange = incr * topicData.postcount; - if (parseInt(topicData.deleted, 10) === 0) { async.parallel([ function(next) { - db.decrObjectField('category:' + topicData.cid, 'topic_count', next); + db.incrObjectFieldBy('global', 'postCount', postCountChange, next); }, function(next) { - db.decrObjectField('global', 'topicCount', next); + db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next); + }, + function(next) { + db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next); } - ], callback); - } else { - callback(); - } - }); - }); + ], next); + }); + } + ], callback); } };