dont modify counts on soft post delete

This commit is contained in:
barisusakli
2014-10-14 22:51:48 -04:00
parent 5de74914bd
commit b1d48df6d8
3 changed files with 126 additions and 94 deletions

View File

@@ -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);
});
});
});