diff --git a/src/postTools.js b/src/postTools.js index cfbd9d504f..009d4bde56 100644 --- a/src/postTools.js +++ b/src/postTools.js @@ -141,51 +141,23 @@ var winston = require('winston'), return callback(err); } - posts.setPostField(pid, 'deleted', isDelete ? 1 : 0, function(err) { - if (err) { - return callback(err); - } - - events[isDelete ? 'logPostDelete' : 'logPostRestore'](uid, pid); - - db.incrObjectFieldBy('global', 'postCount', isDelete ? -1 : 1); - - posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], function(err, postData) { + events[isDelete ? 'logPostDelete' : 'logPostRestore'](uid, pid); + if (isDelete) { + posts.delete(pid, callback); + } else { + posts.restore(pid, function(err, postData) { if (err) { return callback(err); } - - if (isDelete) { - plugins.fireHook('action:post.delete', pid); - } else { - plugins.fireHook('action:post.restore', postData); - } - - async.parallel([ - function(next) { - topics[isDelete ? 'decreasePostCount' : 'increasePostCount'](postData.tid, next); - }, - function(next) { - user.incrementUserPostCountBy(postData.uid, isDelete ? -1 : 1, next); - }, - function(next) { - updateTopicTimestamp(postData.tid, next); - }, - function(next) { - addOrRemoveFromCategory(pid, postData.tid, postData.timestamp, isDelete, next); + PostTools.parse(postData.content, function(err, parsed) { + if (err) { + return callback(err); } - ], function(err) { - if (!isDelete) { - PostTools.parse(postData.content, function(err, parsed) { - postData.content = parsed; - callback(err, postData); - }); - return; - } - callback(err, postData); + postData.content = parsed; + callback(null, postData); }); }); - }); + } }); } @@ -199,41 +171,6 @@ var winston = require('winston'), }); }; - function updateTopicTimestamp(tid, callback) { - topics.getLatestUndeletedPid(tid, function(err, pid) { - if(err || !pid) { - return callback(err); - } - - posts.getPostField(pid, 'timestamp', function(err, timestamp) { - if (err) { - return callback(err); - } - - if (timestamp) { - return topics.updateTimestamp(tid, timestamp, callback); - } - callback(); - }); - }); - } - - function addOrRemoveFromCategory(pid, tid, timestamp, isDelete, callback) { - topics.getTopicField(tid, 'cid', function(err, cid) { - if (err) { - return callback(err); - } - - db.incrObjectFieldBy('category:' + cid, 'post_count', isDelete ? -1 : 1); - - if (isDelete) { - db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, callback); - } else { - db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid, callback); - } - }); - } - PostTools.parse = function(raw, callback) { parse('filter:post.parse', raw, callback); }; diff --git a/src/posts/delete.js b/src/posts/delete.js index dfcff4dc35..744d5ee0d0 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -3,10 +3,105 @@ var async = require('async'), db = require('../database'), topics = require('../topics'), + user = require('../user'), plugins = require('../plugins'); module.exports = function(Posts) { + + Posts.delete = function(pid, callback) { + Posts.setPostField(pid, 'deleted', 1, function(err) { + if (err) { + return callback(err); + } + + Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'timestamp'], function(err, postData) { + if (err) { + return callback(err); + } + + plugins.fireHook('action:post.delete', pid); + + async.parallel([ + function(next) { + updateTopicTimestamp(postData.tid, next); + }, + function(next) { + removeFromCategoryRecentPosts(pid, postData.tid, next); + } + ], function(err) { + callback(err, postData); + }); + }); + }); + }; + + Posts.restore = function(pid, callback) { + Posts.setPostField(pid, 'deleted', 0, function(err) { + if (err) { + return callback(err); + } + + Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], function(err, postData) { + if (err) { + return callback(err); + } + + plugins.fireHook('action:post.restore', postData); + + async.parallel([ + function(next) { + updateTopicTimestamp(postData.tid, next); + }, + function(next) { + addToCategoryRecentPosts(pid, postData.tid, postData.timestamp, next); + } + ], function(err) { + callback(err, postData); + }); + }); + }); + }; + + function updateTopicTimestamp(tid, callback) { + topics.getLatestUndeletedPid(tid, function(err, pid) { + if(err || !pid) { + return callback(err); + } + + Posts.getPostField(pid, 'timestamp', function(err, timestamp) { + if (err) { + return callback(err); + } + + if (timestamp) { + return topics.updateTimestamp(tid, timestamp, callback); + } + callback(); + }); + }); + } + + function removeFromCategoryRecentPosts(pid, tid, callback) { + topics.getTopicField(tid, 'cid', function(err, cid) { + if (err) { + return callback(err); + } + + db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, callback); + }); + } + + function addToCategoryRecentPosts(pid, tid, timestamp, callback) { + topics.getTopicField(tid, 'cid', function(err, cid) { + if (err) { + return callback(err); + } + + db.sortedSetAdd('categories:recent_posts:cid:' + cid, timestamp, pid, callback); + }); + } + Posts.purge = function(pid, callback) { async.parallel([ function(next) { @@ -23,7 +118,7 @@ module.exports = function(Posts) { }, function(next) { db.sortedSetRemove('posts:pid', pid, next); - } + }, ], function(err) { if (err) { return callback(err); @@ -35,7 +130,7 @@ module.exports = function(Posts) { }; function deletePostFromTopicAndUser(pid, callback) { - Posts.getPostFields(pid, ['tid', 'uid', 'deleted'], function(err, postData) { + Posts.getPostFields(pid, ['tid', 'uid'], function(err, postData) { if (err) { return callback(err); } @@ -54,21 +149,20 @@ module.exports = function(Posts) { return callback(err); } - if (parseInt(postData.deleted, 10) === 0 && parseInt(topicData.deleted, 10) !== 1) { - async.parallel([ - function (next) { - db.decrObjectField('global', 'postCount', next); - }, - function (next) { - db.decrObjectField('category:' + topicData.cid, 'post_count', next); - }, - function (next) { - db.decrObjectField('topic:' + postData.tid, 'postcount', next); - } - ], callback); - } else { - callback(); - } + async.parallel([ + function (next) { + db.decrObjectField('global', 'postCount', next); + }, + function (next) { + db.decrObjectField('category:' + topicData.cid, 'post_count', next); + }, + function (next) { + topics.decreasePostCount(postData.tid, next); + }, + function(next) { + user.incrementUserPostCountBy(postData.uid, -1, next); + }, + ], callback); }); }); }); diff --git a/src/topics/delete.js b/src/topics/delete.js index 679cb986ca..8daec210be 100644 --- a/src/topics/delete.js +++ b/src/topics/delete.js @@ -60,7 +60,7 @@ module.exports = function(Topics) { Topics.deleteTopicTags(tid, next); }, function(next) { - updateCounters(tid, -1, next); + reduceCounters(tid, next); } ], function(err) { if (err) { @@ -72,7 +72,7 @@ module.exports = function(Topics) { }; function deleteTopicFromCategoryAndUser(tid, callback) { - Topics.getTopicFields(tid, ['cid', 'uid', 'deleted'], function(err, topicData) { + Topics.getTopicFields(tid, ['cid', 'uid'], function(err, topicData) { if (err) { return callback(err); } @@ -81,7 +81,8 @@ module.exports = function(Topics) { }); } - function updateCounters(tid, incr, callback) { + function reduceCounters(tid, callback) { + var incr = -1; async.parallel([ function(next) { db.incrObjectFieldBy('global', 'topicCount', incr, next);