refactor topics/delete.js

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

@ -11,85 +11,90 @@ 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) { async.parallel([
if (err) { function (next) {
return callback(err); Topics.setTopicFields(tid, {
} deleted: 1,
deleterUid: uid,
async.parallel([ deletedTimestamp: Date.now(),
function (next) { }, next);
Topics.setTopicFields(tid, { },
deleted: 1, function (next) {
deleterUid: uid, db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next);
deletedTimestamp: Date.now(), },
}, next); function (next) {
}, async.waterfall([
function (next) { function (next) {
db.sortedSetsRemove(['topics:recent', 'topics:posts', 'topics:views'], tid, next); async.parallel({
}, cid: function (next) {
function (next) { Topics.getTopicField(tid, 'cid', next);
Topics.getPids(tid, function (err, pids) { },
if (err) { pids: function (next) {
return next(err); Topics.getPids(tid, next);
} },
db.sortedSetRemove('cid:' + topicData.cid + ':pids', pids, next); }, next);
}); },
}, function (results, next) {
], function (err) { db.sortedSetRemove('cid:' + results.cid + ':pids', results.pids, next);
callback(err); },
}); ], next);
},
], function (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);
},
async.parallel([ function (_topicData, next) {
function (next) { topicData = _topicData;
Topics.setTopicField(tid, 'deleted', 0, next); async.parallel([
}, function (next) {
function (next) { Topics.setTopicField(tid, 'deleted', 0, next);
Topics.deleteTopicFields(tid, ['deleterUid', 'deletedTimestamp'], next); },
}, function (next) {
function (next) { Topics.deleteTopicFields(tid, ['deleterUid', 'deletedTimestamp'], next);
Topics.updateRecent(tid, topicData.lastposttime, next); },
}, function (next) {
function (next) { Topics.updateRecent(tid, topicData.lastposttime, next);
db.sortedSetAdd('topics:posts', topicData.postcount, tid, next); },
}, function (next) {
function (next) { db.sortedSetAdd('topics:posts', topicData.postcount, tid, next);
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next); },
}, function (next) {
function (next) { db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
Topics.getPids(tid, function (err, pids) { },
if (err) { function (next) {
return callback(err); async.waterfall([
} function (next) {
Topics.getPids(tid, next);
posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], function (err, postData) { },
if (err) { function (pids, next) {
return next(err); posts.getPostsFields(pids, ['pid', 'timestamp', 'deleted'], next);
} },
postData = postData.filter(function (post) { function (postData, next) {
return post && parseInt(post.deleted, 10) !== 1; postData = postData.filter(function (post) {
}); return post && parseInt(post.deleted, 10) !== 1;
var pidsToAdd = []; });
var scores = []; var pidsToAdd = [];
postData.forEach(function (post) { var scores = [];
pidsToAdd.push(post.pid); postData.forEach(function (post) {
scores.push(post.timestamp); pidsToAdd.push(post.pid);
}); 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) { },
callback(err); ], function (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) {
next(err);
});
}, },
], function (err) { function (next) {
if (err) { plugins.fireHook('action:topic.purge', tid);
return callback(err); db.delete('topic:' + tid, next);
} },
plugins.fireHook('action:topic.purge', tid); ], callback);
db.delete('topic:' + tid, callback);
});
}; };
function deleteFromFollowersIgnorers(tid, callback) { function deleteFromFollowersIgnorers(tid, callback) {
@ -176,24 +181,28 @@ 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);
} },
async.parallel([ function (topicData, next) {
function (next) { async.parallel([
db.sortedSetsRemove([ function (next) {
'cid:' + topicData.cid + ':tids', db.sortedSetsRemove([
'cid:' + topicData.cid + ':tids:pinned', 'cid:' + topicData.cid + ':tids',
'cid:' + topicData.cid + ':tids:posts', 'cid:' + topicData.cid + ':tids:pinned',
'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids', 'cid:' + topicData.cid + ':tids:posts',
'uid:' + topicData.uid + ':topics', 'cid:' + topicData.cid + ':uid:' + topicData.uid + ':tids',
], tid, next); 'uid:' + topicData.uid + ':topics',
}, ], tid, next);
function (next) { },
user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next); function (next) {
}, user.decrementUserFieldBy(topicData.uid, 'topiccount', 1, next);
], callback); },
], next);
},
], function (err) {
callback(err);
}); });
} }
@ -204,26 +213,28 @@ 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);
} },
topicData.postcount = parseInt(topicData.postcount, 10); function (topicData, next) {
topicData.postcount = topicData.postcount || 0; topicData.postcount = parseInt(topicData.postcount, 10);
var postCountChange = incr * topicData.postcount; topicData.postcount = topicData.postcount || 0;
var postCountChange = incr * topicData.postcount;
async.parallel([ async.parallel([
function (next) { function (next) {
db.incrObjectFieldBy('global', 'postCount', postCountChange, next); db.incrObjectFieldBy('global', 'postCount', postCountChange, next);
}, },
function (next) { function (next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next); db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);
}, },
function (next) { function (next) {
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