From 9d1a295b85e8ef06d9e356c090d5905662855b86 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Mon, 17 Feb 2014 22:53:01 -0500 Subject: [PATCH] better topic delete restore --- src/threadTools.js | 60 ++++++++++++++++++++++----------------- src/topics.js | 70 +++++++++++++++++++++++++++++++++------------- 2 files changed, 85 insertions(+), 45 deletions(-) diff --git a/src/threadTools.js b/src/threadTools.js index 047e1e6228..81eb9ff2d0 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -67,27 +67,31 @@ var winston = require('winston'), return callback(new Error('topic-already-deleted')); } - topics.delete(tid); + topics.delete(tid, function(err) { + if(err) { + return callback(err); + } - db.decrObjectField('global', 'topicCount'); + db.decrObjectField('global', 'topicCount'); - ThreadTools.lock(tid); + ThreadTools.lock(tid); - db.searchRemove('topic', tid); + db.searchRemove('topic', tid); - events.logTopicDelete(uid, tid); + events.logTopicDelete(uid, tid); - websockets.emitTopicPostStats(); + websockets.emitTopicPostStats(); - websockets.in('topic_' + tid).emit('event:topic_deleted', { - tid: tid - }); + websockets.in('topic_' + tid).emit('event:topic_deleted', { + tid: tid + }); - callback(null, { - tid: tid + callback(null, { + tid: tid + }); }); }); - } + }; ThreadTools.restore = function(tid, uid, callback) { topics.getTopicField(tid, 'deleted', function(err, deleted) { @@ -99,29 +103,33 @@ var winston = require('winston'), return callback(new Error('topic-already-restored')); } - topics.restore(tid); + topics.restore(tid, function(err) { + if(err) { + return callback(err); + } - db.incrObjectField('global', 'topicCount'); + db.incrObjectField('global', 'topicCount'); - ThreadTools.unlock(tid); + ThreadTools.unlock(tid); - events.logTopicRestore(uid, tid); + events.logTopicRestore(uid, tid); - websockets.emitTopicPostStats(); + websockets.emitTopicPostStats(); - websockets.in('topic_' + tid).emit('event:topic_restored', { - tid: tid - }); + websockets.in('topic_' + tid).emit('event:topic_restored', { + tid: tid + }); - topics.getTopicField(tid, 'title', function(err, title) { - db.searchIndex('topic', title, tid); - }); + topics.getTopicField(tid, 'title', function(err, title) { + db.searchIndex('topic', title, tid); + }); - callback(null, { - tid:tid + callback(null, { + tid:tid + }); }); }); - } + }; ThreadTools.lock = function(tid, uid, callback) { topics.setTopicField(tid, 'locked', 1); diff --git a/src/topics.js b/src/topics.js index acc76529a0..012a3208aa 100644 --- a/src/topics.js +++ b/src/topics.js @@ -1154,29 +1154,61 @@ var async = require('async'), }); } - Topics.delete = function(tid) { - Topics.setTopicField(tid, 'deleted', 1); - db.sortedSetRemove('topics:recent', tid); - db.sortedSetRemove('topics:posts', tid); - db.sortedSetRemove('topics:views', tid); - - Topics.getTopicField(tid, 'cid', function(err, cid) { - db.incrObjectFieldBy('category:' + cid, 'topic_count', -1); - }); - } + Topics.delete = function(tid, callback) { + async.parallel([ + function(next) { + Topics.setTopicField(tid, 'deleted', 1, next); + }, + function(next) { + db.sortedSetRemove('topics:recent', tid, next); + }, + function(next) { + db.sortedSetRemove('topics:posts', tid, next); + }, + function(next) { + db.sortedSetRemove('topics:views', tid, next); + }, + function(next) { + Topics.getTopicField(tid, 'cid', function(err, cid) { + if(err) { + return next(err); + } + db.incrObjectFieldBy('category:' + cid, 'topic_count', -1, next); + }); + } + ], callback); + }; - Topics.restore = function(tid) { - Topics.setTopicField(tid, 'deleted', 0); + Topics.restore = function(tid, callback) { Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) { - db.sortedSetAdd('topics:recent', topicData.lastposttime, tid); - db.sortedSetAdd('topics:posts', topicData.postcount, tid); - db.sortedSetAdd('topics:views', topicData.viewcount, tid); - }); + if(err) { + return callback(err); + } - Topics.getTopicField(tid, 'cid', function(err, cid) { - db.incrObjectFieldBy('category:' + cid, 'topic_count', 1); + async.parallel([ + function(next) { + Topics.setTopicField(tid, 'deleted', 0, next); + }, + function(next) { + db.sortedSetAdd('topics:recent', topicData.lastposttime, tid, next); + }, + function(next) { + db.sortedSetAdd('topics:posts', topicData.postcount, tid, next); + }, + function(next) { + db.sortedSetAdd('topics:views', topicData.viewcount, tid, next); + }, + function(next) { + Topics.getTopicField(tid, 'cid', function(err, cid) { + if(err) { + return next(err); + } + db.incrObjectFieldBy('category:' + cid, 'topic_count', 1, next); + }); + } + ], callback); }); - } + }; Topics.reIndexTopic = function(tid, callback) { Topics.getPids(tid, function(err, pids) {