From 8bba4889d2891e6b2049133781815ef8040308cc Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Sun, 2 Feb 2014 15:43:33 -0500 Subject: [PATCH] moving deleted topics wont cause topic count to go negative --- src/threadTools.js | 54 +++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/threadTools.js b/src/threadTools.js index 31ba63b631..87d0026171 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -186,43 +186,39 @@ var winston = require('winston'), } ThreadTools.move = function(tid, cid, callback) { - topics.getTopicFields(tid, ['cid', 'lastposttime'], function(err, topicData) { + var topic; + async.waterfall([ + function(next) { + topics.getTopicFields(tid, ['cid', 'lastposttime', 'deleted'], next); + }, + function(topicData, next) { + topic = topicData; + db.sortedSetRemove('categories:' + topicData.cid + ':tid', tid, next); + }, + function(result, next) { + db.sortedSetAdd('categories:' + cid + ':tid', topic.lastposttime, tid, next); + } + ], function(err, result) { if(err) { return callback(err); } + var oldCid = topic.cid; - var oldCid = topicData.cid; - if(!oldCid) { - return callback(new Error('invalid-topic')); - } + topics.setTopicField(tid, 'cid', cid); - db.sortedSetRemove('categories:' + oldCid + ':tid', tid, function(err, result) { - if(err) { - return callback(err); + categories.moveActiveUsers(tid, oldCid, cid); + categories.moveRecentReplies(tid, oldCid, cid, function(err, data) { + if (err) { + winston.err(err); } + }); - db.sortedSetAdd('categories:' + cid + ':tid', topicData.lastposttime, tid, function(err, result) { - - if(err) { - return callback(err); - } - - topics.setTopicField(tid, 'cid', cid); - - categories.moveRecentReplies(tid, oldCid, cid, function(err, data) { - if (err) { - winston.err(err); - } - }); - - categories.moveActiveUsers(tid, oldCid, cid); - - categories.incrementCategoryFieldBy(oldCid, 'topic_count', -1); - categories.incrementCategoryFieldBy(cid, 'topic_count', 1); + if(!parseInt(topic.deleted, 10)) { + categories.incrementCategoryFieldBy(oldCid, 'topic_count', -1); + categories.incrementCategoryFieldBy(cid, 'topic_count', 1); + } - callback(null); - }); - }); + callback(null); }); }