You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/posts/delete.js

143 lines
3.4 KiB
JavaScript

11 years ago
'use strict';
var async = require('async'),
db = require('../database'),
topics = require('../topics'),
11 years ago
plugins = require('../plugins');
11 years ago
module.exports = function(Posts) {
11 years ago
Posts.purge = function(pid, callback) {
11 years ago
async.parallel([
function(next) {
deletePostFromTopicAndUser(pid, next);
},
function(next) {
deletePostFromCategoryRecentPosts(pid, next);
},
function(next) {
deletePostFromUsersFavourites(pid, next);
},
function(next) {
deletePostFromUsersVotes(pid, next);
},
function(next) {
db.sortedSetRemove('posts:pid', pid, next);
}
], function(err) {
if (err) {
return callback(err);
}
11 years ago
11 years ago
plugins.fireHook('action:post.delete', pid);
db.delete('post:' + pid, callback);
});
11 years ago
};
11 years ago
function deletePostFromTopicAndUser(pid, callback) {
Posts.getPostFields(pid, ['tid', 'uid', 'deleted'], function(err, postData) {
if (err) {
return callback(err);
}
async.parallel([
function(next) {
db.sortedSetRemove('tid:' + postData.tid + ':posts', pid, next);
},
function(next) {
db.sortedSetRemove('tid:' + postData.tid + ':posts:votes', pid, next);
},
function(next) {
db.sortedSetRemove('uid:' + postData.uid + ':posts', pid, next);
}
], function(err) {
if (err) {
return callback(err);
}
11 years ago
topics.getTopicFields(postData.tid, ['cid', 'deleted'], function(err, topicData) {
if (err) {
return callback(err);
}
if (parseInt(postData.deleted, 10) === 0 && parseInt(topicData.deleted, 10) !== 1) {
11 years ago
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();
}
});
11 years ago
});
});
}
function deletePostFromCategoryRecentPosts(pid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
if (err) {
return callback(err);
}
async.each(cids, function(cid, next) {
db.sortedSetRemove('categories:recent_posts:cid:' + cid, pid, next);
}, callback);
});
}
function deletePostFromUsersFavourites(pid, callback) {
db.getSetMembers('pid:' + pid + ':users_favourited', function(err, uids) {
if (err) {
return callback(err);
}
async.each(uids, function(uid, next) {
db.sortedSetRemove('uid:' + uid + ':favourites', pid, next);
}, function(err) {
if (err) {
return callback(err);
}
db.delete('pid:' + pid + ':users_favourited', callback);
});
});
}
function deletePostFromUsersVotes(pid, callback) {
async.parallel({
upvoters: function(next) {
db.getSetMembers('pid:' + pid + ':upvote', next);
},
downvoters: function(next) {
db.getSetMembers('pid:' + pid + ':downvote', next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
async.parallel([
function(next) {
async.each(results.upvoters, function(uid, next) {
db.sortedSetRemove('uid:' + uid + ':upvote', pid, next);
}, next);
},
function(next) {
async.each(results.downvoters, function(uid, next) {
db.sortedSetRemove('uid:' + uid + ':downvote', pid, next);
}, next);
}
], callback);
});
}
};