Node.js based forum software built for the modern web
refactor topics/delete.js
barisusakli90b8a3afd068
diff --git a/src/topics/delete.js b/src/topics/delete.jsindex 30992dd94c..082244f96d 100644--- a/src/topics/delete.js+++ b/src/topics/delete.js@@ -11,85 +11,90 @@ var batch = require('../batch');module.exports = function (Topics) {Topics.delete = function (tid, uid, callback) {- Topics.getTopicFields(tid, ['cid'], function (err, topicData) {- if (err) {- return callback(err);- }-- async.parallel([- function (next) {- Topics.setTopicFields(tid, {- deleted: 1,- deleterUid: uid,- deletedTimestamp: Date.now(),- }, next);- },- function (next) {- db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);- },- function (next) {- Topics.getPids(tid, function (err, pids) {- if (err) {- return next(err);- }- db.sortedSetRemove('cid:' + topicData.cid + ':pids', pids, next);- });- },- ], function (err) {- callback(err);- });+ async.parallel([+ function (next) {+ Topics.setTopicFields(tid, {+ deleted: 1,+ deleterUid: uid,+ deletedTimestamp: Date.now(),+ }, next);+ },+ function (next) {+ db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);+ },+ function (next) {+ async.waterfall([+ function (next) {+ async.parallel({+ cid: function (next) {+ Topics.getTopicField(tid, 'cid', next);+ },+ pids: function (next) {+ Topics.getPids(tid, next);+ },+ }, next);+ },+ function (results, next) {+ db.sortedSetRemove('cid:' + results.cid + ':pids', results.pids, next);+ },+ ], next);+ },+ ], function (err) {+ callback(err);});};Topics.restore = function (tid, uid, callback) {- Topics.getTopicFields(tid, ['cid', 'lastposttime', 'postcount', 'viewcount'], function (err, topicData) {- if (err) {- return callback(err);- }-- async.parallel([- function (next) {- Topics.setTopicField(tid, 'deleted', 0, next);- },- function (next) {- Topics.deleteTopicFields(tid, ['deleterUid', 'deletedTimestamp'], next);- },- function (next) {- Topics.updateRecent(tid, topicData.lastposttime, next);- },- function (next) {- db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);- },- function (next) {- db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);- },- function (next) {- Topics.getPids(tid, function (err, pids) {- if (err) {- return callback(err);- }-- posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], function (err, postData) {- if (err) {- return next(err);- }- postData = postData.filter(function (post) {- return post && parseInt(post.deleted, 10) !== 1;- });- var pidsToAdd = [];- var scores = [];- postData.forEach(function (post) {- pidsToAdd.push(post.pid);- scores.push(post.timestamp);- });- db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next);- });- });- },- ], function (err) {- callback(err);- });- });+ var topicData;+ async.waterfall([+ function (next) {+ Topics.getTopicFields(tid, ['cid', 'lastposttime', 'postcount', 'viewcount'], next);+ },+ function (_topicData, next) {+ topicData = _topicData;+ async.parallel([+ function (next) {+ Topics.setTopicField(tid, 'deleted', 0, next);+ },+ function (next) {+ Topics.deleteTopicFields(tid, ['deleterUid', 'deletedTimestamp'], next);+ },+ function (next) {+ Topics.updateRecent(tid, topicData.lastposttime, next);+ },+ function (next) {+ db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);+ },+ function (next) {+ db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);+ },+ function (next) {+ async.waterfall([+ function (next) {+ Topics.getPids(tid, next);+ },+ function (pids, next) {+ posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], next);+ },+ function (postData, next) {+ postData = postData.filter(function (post) {+ return post && parseInt(post.deleted, 10) !== 1;+ });+ var pidsToAdd = [];+ var scores = [];+ postData.forEach(function (post) {+ pidsToAdd.push(post.pid);+ scores.push(post.timestamp);+ });+ db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next);+ },+ ], next);+ },+ ], function (err) {+ next(err);+ });+ },+ ], callback);};Topics.purgePostsAndTopic = function (tid, uid, callback) {@@ -144,15 +149,15 @@ module.exports = function (Topics) {function (next) {reduceCounters(tid, next);},- ], next);+ ], function (err) {+ next(err);+ });},- ], function (err) {- if (err) {- return callback(err);- }- plugins.fireHook('action:topic.purge', tid);- db.delete('topic:' + tid, callback);- });+ function (next) {+ plugins.fireHook('action:topic.purge', tid);+ db.delete('topic:' + tid, next);+ },+ ], callback);};function deleteFromFollowersIgnorers(tid, callback) {@@ -176,24 +181,28 @@ module.exports = function (Topics) {}function deleteTopicFromCategoryAndUser(tid, callback) {- Topics.getTopicFields(tid, ['cid', 'uid'], function (err, topicData) {- if (err) {- return callback(err);- }- async.parallel([- function (next) {- db.sortedSetsRemove([- 'cid:' + topicData.cid + ':tids',- 'cid:' + topicData.cid + ':tids:pinned',- 'cid:' + topicData.cid + ':tids:posts',- 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',- 'uid:' + topicData.uid + ':topics',- ], tid, next);- },- function (next) {- user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);- },- ], callback);+ async.waterfall([+ function (next) {+ Topics.getTopicFields(tid, ['cid', 'uid'], next);+ },+ function (topicData, next) {+ async.parallel([+ function (next) {+ db.sortedSetsRemove([+ 'cid:' + topicData.cid + ':tids',+ 'cid:' + topicData.cid + ':tids:pinned',+ 'cid:' + topicData.cid + ':tids:posts',+ 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',+ 'uid:' + topicData.uid + ':topics',+ ], tid, next);+ },+ function (next) {+ user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);+ },+ ], next);+ },+ ], function (err) {+ callback(err);});}@@ -204,26 +213,28 @@ module.exports = function (Topics) {db.incrObjectFieldBy('global', 'topicCount', incr, next);},function (next) {- Topics.getTopicFields(tid, ['cid', 'postcount'], function (err, topicData) {- if (err) {- return next(err);- }- topicData.postcount = parseInt(topicData.postcount, 10);- topicData.postcount = topicData.postcount || 0;- var postCountChange = incr * topicData.postcount;+ async.waterfall([+ function (next) {+ Topics.getTopicFields(tid, ['cid', 'postcount'], next);+ },+ function (topicData, next) {+ topicData.postcount = parseInt(topicData.postcount, 10);+ topicData.postcount = topicData.postcount || 0;+ var postCountChange = incr * topicData.postcount;- async.parallel([- function (next) {- db.incrObjectFieldBy('global', 'postCount', postCountChange, next);- },- function (next) {- db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);- },- function (next) {- db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);- },- ], next);- });+ async.parallel([+ function (next) {+ db.incrObjectFieldBy('global', 'postCount', postCountChange, next);+ },+ function (next) {+ db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);+ },+ function (next) {+ db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);+ },+ ], next);+ },+ ], next);},], callback);}