diff --git a/src/postTools.js b/src/postTools.js index 13f7963f36..5a7310603e 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -207,18 +207,22 @@ var winston = require('winston'), }); }); - // Restore topic if it is the only post - topics.getTopicField(postData.tid, 'postcount', function(err, count) { - if (parseInt(count, 10) === 1) { - threadTools.restore(postData.tid, uid); - } - }); - Feed.updateTopic(postData.tid); Feed.updateRecent(); db.searchIndex('post', postData.content, pid); + // Restore topic if it is the only post + topics.getTopicField(postData.tid, 'postcount', function(err, count) { + if (parseInt(count, 10) === 1) { + threadTools.restore(postData.tid, uid, function(err) { + if(err) { + winston.err(err); + } + }); + } + }); + callback(); }); }; diff --git a/src/threadTools.js b/src/threadTools.js index e7fa9a5854..076c9b4b0b 100644 --- a/src/threadTools.js +++ b/src/threadTools.js @@ -58,51 +58,69 @@ var winston = require('winston'), } ThreadTools.delete = function(tid, uid, callback) { - topics.delete(tid); + topics.getTopicField(tid, 'deleted', function(err, deleted) { + if(err) { + return callback(err); + } - db.decrObjectField('global', 'topicCount'); + if (parseInt(deleted, 10)) { + return callback(new Error('topic-already-deleted')); + } - ThreadTools.lock(tid); + topics.delete(tid); - db.searchRemove('topic', tid); + db.decrObjectField('global', 'topicCount'); - events.logTopicDelete(uid, tid); + ThreadTools.lock(tid); - websockets.emitTopicPostStats(); + db.searchRemove('topic', tid); - websockets.in('topic_' + tid).emit('event:topic_deleted', { - tid: tid - }); + events.logTopicDelete(uid, tid); + + websockets.emitTopicPostStats(); + + websockets.in('topic_' + tid).emit('event:topic_deleted', { + tid: tid + }); - if (callback) { callback(null, { tid: tid }); - } + }); } ThreadTools.restore = function(tid, uid, callback) { - topics.restore(tid); - db.incrObjectField('global', 'topicCount'); - ThreadTools.unlock(tid); + topics.getTopicField(tid, 'deleted', function(err, deleted) { + if(err) { + return callback(err); + } - events.logTopicRestore(uid, tid); + if (!parseInt(deleted, 10)) { + return callback(new Error('topic-already-restored')); + } - websockets.emitTopicPostStats(); + topics.restore(tid); - websockets.in('topic_' + tid).emit('event:topic_restored', { - tid: tid - }); + db.incrObjectField('global', 'topicCount'); - topics.getTopicField(tid, 'title', function(err, title) { - db.searchIndex('topic', title, tid); - }); + ThreadTools.unlock(tid); + + events.logTopicRestore(uid, tid); + + websockets.emitTopicPostStats(); + + websockets.in('topic_' + tid).emit('event:topic_restored', { + tid: tid + }); + + topics.getTopicField(tid, 'title', function(err, title) { + db.searchIndex('topic', title, tid); + }); - if(callback) { callback(null, { tid:tid }); - } + }); } ThreadTools.lock = function(tid, uid, callback) {