refactor topics/delete.js

v1.18.x
barisusakli 8 years ago
parent 9f1a2b0cc9
commit 90b8a3afd0

@ -11,11 +11,6 @@ var batch = require('../batch');
module.exports = function (Topics) { module.exports = function (Topics) {
Topics.delete = function (tid, uid, callback) { Topics.delete = function (tid, uid, callback) {
Topics.getTopicFields(tid, ['cid'], function (err, topicData) {
if (err) {
return callback(err);
}
async.parallel([ async.parallel([
function (next) { function (next) {
Topics.setTopicFields(tid, { Topics.setTopicFields(tid, {
@ -28,25 +23,35 @@ module.exports = function (Topics) {
db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next); db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);
}, },
function (next) { function (next) {
Topics.getPids(tid, function (err, pids) { async.waterfall([
if (err) { function (next) {
return next(err); async.parallel({
} cid: function (next) {
db.sortedSetRemove('cid:' + topicData.cid + ':pids', pids, 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) { ], function (err) {
callback(err); callback(err);
}); });
});
}; };
Topics.restore = function (tid, uid, callback) { Topics.restore = function (tid, uid, callback) {
Topics.getTopicFields(tid, ['cid', 'lastposttime', 'postcount', 'viewcount'], function (err, topicData) { var topicData;
if (err) { async.waterfall([
return callback(err); function (next) {
} Topics.getTopicFields(tid, ['cid', 'lastposttime', 'postcount', 'viewcount'], next);
},
function (_topicData, next) {
topicData = _topicData;
async.parallel([ async.parallel([
function (next) { function (next) {
Topics.setTopicField(tid, 'deleted', 0, next); Topics.setTopicField(tid, 'deleted', 0, next);
@ -64,15 +69,14 @@ module.exports = function (Topics) {
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next); db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
}, },
function (next) { function (next) {
Topics.getPids(tid, function (err, pids) { async.waterfall([
if (err) { function (next) {
return callback(err); Topics.getPids(tid, next);
} },
function (pids, next) {
posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], function (err, postData) { posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], next);
if (err) { },
return next(err); function (postData, next) {
}
postData = postData.filter(function (post) { postData = postData.filter(function (post) {
return post && parseInt(post.deleted, 10) !== 1; return post && parseInt(post.deleted, 10) !== 1;
}); });
@ -83,13 +87,14 @@ module.exports = function (Topics) {
scores.push(post.timestamp); scores.push(post.timestamp);
}); });
db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next); db.sortedSetAdd('cid:' + topicData.cid + ':pids', scores, pidsToAdd, next);
}); },
}); ], next);
}, },
], function (err) { ], function (err) {
callback(err); next(err);
});
}); });
},
], callback);
}; };
Topics.purgePostsAndTopic = function (tid, uid, callback) { Topics.purgePostsAndTopic = function (tid, uid, callback) {
@ -144,15 +149,15 @@ module.exports = function (Topics) {
function (next) { function (next) {
reduceCounters(tid, next); reduceCounters(tid, next);
}, },
], next);
},
], function (err) { ], function (err) {
if (err) { next(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) { function deleteFromFollowersIgnorers(tid, callback) {
@ -176,10 +181,11 @@ module.exports = function (Topics) {
} }
function deleteTopicFromCategoryAndUser(tid, callback) { function deleteTopicFromCategoryAndUser(tid, callback) {
Topics.getTopicFields(tid, ['cid', 'uid'], function (err, topicData) { async.waterfall([
if (err) { function (next) {
return callback(err); Topics.getTopicFields(tid, ['cid', 'uid'], next);
} },
function (topicData, next) {
async.parallel([ async.parallel([
function (next) { function (next) {
db.sortedSetsRemove([ db.sortedSetsRemove([
@ -193,7 +199,10 @@ module.exports = function (Topics) {
function (next) { function (next) {
user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next); user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);
}, },
], callback); ], next);
},
], function (err) {
callback(err);
}); });
} }
@ -204,10 +213,11 @@ module.exports = function (Topics) {
db.incrObjectFieldBy('global', 'topicCount', incr, next); db.incrObjectFieldBy('global', 'topicCount', incr, next);
}, },
function (next) { function (next) {
Topics.getTopicFields(tid, ['cid', 'postcount'], function (err, topicData) { async.waterfall([
if (err) { function (next) {
return next(err); Topics.getTopicFields(tid, ['cid', 'postcount'], next);
} },
function (topicData, next) {
topicData.postcount = parseInt(topicData.postcount, 10); topicData.postcount = parseInt(topicData.postcount, 10);
topicData.postcount = topicData.postcount || 0; topicData.postcount = topicData.postcount || 0;
var postCountChange = incr * topicData.postcount; var postCountChange = incr * topicData.postcount;
@ -223,7 +233,8 @@ module.exports = function (Topics) {
db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next); db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);
}, },
], next); ], next);
}); },
], next);
}, },
], callback); ], callback);
} }

Loading…
Cancel
Save