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

292 lines
7.2 KiB
JavaScript

11 years ago
'use strict';
9 years ago
var async = require('async');
var _ = require('underscore');
9 years ago
9 years ago
var db = require('../database');
var topics = require('../topics');
var user = require('../user');
var plugins = require('../plugins');
11 years ago
module.exports = function (Posts) {
11 years ago
Posts.delete = function (pid, uid, callback) {
var postData;
async.waterfall([
9 years ago
function (next) {
plugins.fireHook('filter:post.delete', {pid: pid, uid: uid}, next);
},
function (data, next) {
8 years ago
Posts.setPostFields(pid, {deleted: 1, deleterUid: uid}, next);
},
9 years ago
function (next) {
Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'timestamp'], next);
},
9 years ago
function (_post, next) {
postData = _post;
9 years ago
topics.getTopicFields(_post.tid, ['tid', 'cid', 'pinned'], next);
},
9 years ago
function (topicData, next) {
async.parallel([
function (next) {
9 years ago
updateTopicTimestamp(topicData, next);
},
function (next) {
9 years ago
db.sortedSetRemove('cid:' + topicData.cid + ':pids', pid, next);
},
function (next) {
topics.updateTeaser(postData.tid, next);
}
9 years ago
], next);
},
function (results, next) {
plugins.fireHook('action:post.delete', pid);
next(null, postData);
}
], callback);
};
Posts.restore = function (pid, uid, callback) {
var postData;
async.waterfall([
9 years ago
function (next) {
plugins.fireHook('filter:post.restore', {pid: pid, uid: uid}, next);
},
function (data, next) {
8 years ago
Posts.setPostFields(pid, {deleted: 0, deleterUid: 0}, next);
},
9 years ago
function (next) {
Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], next);
},
9 years ago
function (_post, next) {
postData = _post;
9 years ago
topics.getTopicFields(_post.tid, ['tid', 'cid', 'pinned'], next);
},
9 years ago
function (topicData, next) {
postData.cid = topicData.cid;
async.parallel([
function (next) {
9 years ago
updateTopicTimestamp(topicData, next);
},
function (next) {
9 years ago
db.sortedSetAdd('cid:' + topicData.cid + ':pids', postData.timestamp, pid, next);
},
function (next) {
topics.updateTeaser(postData.tid, next);
}
9 years ago
], next);
},
function (results, next) {
plugins.fireHook('action:post.restore', _.clone(postData));
next(null, postData);
}
], callback);
};
9 years ago
function updateTopicTimestamp(topicData, callback) {
var timestamp;
async.waterfall([
function (next) {
topics.getLatestUndeletedPid(topicData.tid, next);
},
function (pid, next) {
if (!parseInt(pid, 10)) {
return callback();
}
9 years ago
Posts.getPostField(pid, 'timestamp', next);
},
function (_timestamp, next) {
timestamp = _timestamp;
if (!parseInt(timestamp, 10)) {
return callback();
}
9 years ago
topics.updateTimestamp(topicData.tid, timestamp, next);
},
function (next) {
if (parseInt(topicData.pinned, 10) !== 1) {
db.sortedSetAdd('cid:' + topicData.cid + ':tids', timestamp, topicData.tid, next);
} else {
next();
}
}
], callback);
}
Posts.purge = function (pid, uid, callback) {
9 years ago
async.waterfall([
function (next) {
Posts.exists(pid, next);
},
function (exists, next) {
if (!exists) {
return callback();
}
9 years ago
plugins.fireHook('filter:post.purge', {pid: pid, uid: uid}, next);
},
function (data, next) {
async.parallel([
function (next) {
deletePostFromTopicAndUser(pid, next);
},
function (next) {
deletePostFromCategoryRecentPosts(pid, next);
},
function (next) {
8 years ago
deletePostFromUsersBookmarks(pid, next);
9 years ago
},
function (next) {
deletePostFromUsersVotes(pid, next);
},
function (next) {
Posts.getPostField(pid, 'toPid', function (err, toPid) {
if (err) {
return next(err);
}
if (!parseInt(toPid, 10)) {
return next(null);
}
async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
async.apply(db.decrObjectField, 'post:' + toPid, 'replies')
], next);
});
},
9 years ago
function (next) {
db.sortedSetsRemove(['posts:pid', 'posts:flagged'], pid, next);
},
function (next) {
Posts.dismissFlag(pid, next);
}
], function (err) {
9 years ago
if (err) {
return next(err);
}
plugins.fireHook('action:post.purge', pid);
db.delete('post:' + pid, next);
});
}
], callback);
11 years ago
};
11 years ago
function deletePostFromTopicAndUser(pid, callback) {
Posts.getPostFields(pid, ['tid', 'uid'], function (err, postData) {
11 years ago
if (err) {
return callback(err);
}
11 years ago
db.sortedSetsRemove([
'tid:' + postData.tid + ':posts',
'tid:' + postData.tid + ':posts:votes',
'uid:' + postData.uid + ':posts'
], pid, function (err) {
11 years ago
if (err) {
return callback(err);
}
topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned'], function (err, topicData) {
if (err) {
return callback(err);
}
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) {
topics.updateTeaser(postData.tid, next);
},
9 years ago
function (next) {
updateTopicTimestamp(topicData, next);
},
function (next) {
10 years ago
db.sortedSetIncrBy('cid:' + topicData.cid + ':tids:posts', -1, postData.tid, next);
},
function (next) {
9 years ago
db.sortedSetIncrBy('tid:' + postData.tid + ':posters', -1, postData.uid, next);
},
function (next) {
10 years ago
user.incrementUserPostCountBy(postData.uid, -1, next);
}
], callback);
});
11 years ago
});
});
}
function deletePostFromCategoryRecentPosts(pid, callback) {
db.getSortedSetRange('categories:cid', 0, -1, function (err, cids) {
11 years ago
if (err) {
return callback(err);
}
var sets = cids.map(function (cid) {
10 years ago
return 'cid:' + cid + ':pids';
});
db.sortedSetsRemove(sets, pid, callback);
11 years ago
});
}
8 years ago
function deletePostFromUsersBookmarks(pid, callback) {
db.getSetMembers('pid:' + pid + ':users_bookmarked', function (err, uids) {
11 years ago
if (err) {
return callback(err);
}
var sets = uids.map(function (uid) {
8 years ago
return 'uid:' + uid + ':bookmarks';
});
db.sortedSetsRemove(sets, pid, function (err) {
11 years ago
if (err) {
return callback(err);
}
8 years ago
db.delete('pid:' + pid + ':users_bookmarked', callback);
11 years ago
});
});
}
function deletePostFromUsersVotes(pid, callback) {
async.parallel({
upvoters: function (next) {
11 years ago
db.getSetMembers('pid:' + pid + ':upvote', next);
},
downvoters: function (next) {
11 years ago
db.getSetMembers('pid:' + pid + ':downvote', next);
}
}, function (err, results) {
11 years ago
if (err) {
return callback(err);
}
var upvoterSets = results.upvoters.map(function (uid) {
return 'uid:' + uid + ':upvote';
});
var downvoterSets = results.downvoters.map(function (uid) {
return 'uid:' + uid + ':downvote';
});
11 years ago
async.parallel([
function (next) {
db.sortedSetsRemove(upvoterSets, pid, next);
11 years ago
},
function (next) {
db.sortedSetsRemove(downvoterSets, pid, next);
},
function (next) {
11 years ago
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
11 years ago
}
], callback);
});
}
};