From b717c74a816711c50938d52051191ca205ec2900 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Wed, 22 Jan 2014 23:58:21 -0500 Subject: [PATCH] closes #856 --- src/postTools.js | 16 ++++++----- src/threadTools.js | 66 +++++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 30 deletions(-) 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'), }); }); + 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); + threadTools.restore(postData.tid, uid, function(err) { + if(err) { + winston.err(err); + } + }); } }); - Feed.updateTopic(postData.tid); - Feed.updateRecent(); - - db.searchIndex('post', postData.content, pid); - 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); + } + + if (parseInt(deleted, 10)) { + return callback(new Error('topic-already-deleted')); + } - db.decrObjectField('global', 'topicCount'); + topics.delete(tid); - ThreadTools.lock(tid); + db.decrObjectField('global', 'topicCount'); - db.searchRemove('topic', tid); + ThreadTools.lock(tid); - events.logTopicDelete(uid, tid); + db.searchRemove('topic', tid); - websockets.emitTopicPostStats(); + events.logTopicDelete(uid, tid); - websockets.in('topic_' + tid).emit('event:topic_deleted', { - tid: 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) {