moved updateCounters to purge

v1.18.x
barisusakli 10 years ago
parent b65c17c5bc
commit 5de74914bd

@ -8,36 +8,6 @@ var async = require('async'),
module.exports = function(Topics) { module.exports = function(Topics) {
function updateCounters(tid, incr, callback) {
async.parallel([
function(next) {
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.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);
});
}
], callback);
}
Topics.delete = function(tid, callback) { Topics.delete = function(tid, callback) {
async.parallel([ async.parallel([
function(next) { function(next) {
@ -49,18 +19,12 @@ module.exports = function(Topics) {
function(next) { function(next) {
db.sortedSetsRemove(['topics:posts', 'topics:views'], tid, next); db.sortedSetsRemove(['topics:posts', 'topics:views'], tid, next);
} }
], function(err) { ], callback);
if (err) {
return callback(err);
}
updateCounters(tid, -1, callback);
});
}; };
Topics.restore = function(tid, callback) { Topics.restore = function(tid, callback) {
Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) { Topics.getTopicFields(tid, ['lastposttime', 'postcount', 'viewcount'], function(err, topicData) {
if(err) { if (err) {
return callback(err); return callback(err);
} }
@ -77,13 +41,7 @@ module.exports = function(Topics) {
function(next) { function(next) {
db.sortedSetAdd('topics:views', topicData.viewcount, tid, next); db.sortedSetAdd('topics:views', topicData.viewcount, tid, next);
} }
], function(err) { ], callback);
if (err) {
return callback(err);
}
updateCounters(tid, 1, callback);
});
}); });
}; };
@ -100,6 +58,9 @@ module.exports = function(Topics) {
}, },
function(next) { function(next) {
Topics.deleteTopicTags(tid, next); Topics.deleteTopicTags(tid, next);
},
function(next) {
updateCounters(tid, -1, next);
} }
], function(err) { ], function(err) {
if (err) { if (err) {
@ -116,24 +77,37 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, function(err) { db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, callback);
if (err) { });
return callback(err); }
}
function updateCounters(tid, incr, callback) {
async.parallel([
function(next) {
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;
if (parseInt(topicData.deleted, 10) === 0) {
async.parallel([ async.parallel([
function(next) { function(next) {
db.decrObjectField('category:' + topicData.cid, 'topic_count', next); db.incrObjectFieldBy('global', 'postCount', postCountChange, next);
}, },
function(next) { function(next) {
db.decrObjectField('global', 'topicCount', next); db.incrObjectFieldBy('category:' + topicData.cid, 'post_count', postCountChange, next);
},
function(next) {
db.incrObjectFieldBy('category:' + topicData.cid, 'topic_count', incr, next);
} }
], callback); ], next);
} else { });
callback(); }
} ], callback);
});
});
} }
}; };

Loading…
Cancel
Save