Files
NodeBB/src/posts/delete.js

213 lines
6.6 KiB
JavaScript
Raw Normal View History

2014-06-10 14:24:50 -04:00
'use strict';
2016-02-23 13:08:38 +02:00
var async = require('async');
2017-05-26 01:39:40 -06:00
var _ = require('lodash');
2015-09-23 20:38:08 -04:00
2016-02-23 13:08:38 +02:00
var db = require('../database');
var topics = require('../topics');
var user = require('../user');
var groups = require('../groups');
2016-11-10 15:26:53 +03:00
var notifications = require('../notifications');
2016-02-23 13:08:38 +02:00
var plugins = require('../plugins');
2014-06-09 12:51:49 -04:00
module.exports = function (Posts) {
Posts.delete = function (pid, uid, callback) {
2018-10-30 19:41:06 -04:00
deleteOrRestore('delete', pid, uid, callback);
2014-10-14 22:51:48 -04:00
};
Posts.restore = function (pid, uid, callback) {
2018-10-30 19:41:06 -04:00
deleteOrRestore('restore', pid, uid, callback);
};
function deleteOrRestore(type, pid, uid, callback) {
2015-03-15 01:12:13 -04:00
var postData;
2018-10-30 19:41:06 -04:00
const isDeleting = type === 'delete';
2015-03-15 01:12:13 -04:00
async.waterfall([
2016-02-23 13:08:38 +02:00
function (next) {
2018-10-30 19:41:06 -04:00
plugins.fireHook('filter:post.' + type, { pid: pid, uid: uid }, next);
2016-02-23 13:08:38 +02:00
},
function (data, next) {
2018-10-30 19:41:06 -04:00
Posts.setPostFields(pid, {
deleted: isDeleting ? 1 : 0,
deleterUid: isDeleting ? uid : 0,
}, next);
2015-03-15 01:12:13 -04:00
},
2016-02-23 13:08:38 +02:00
function (next) {
2015-03-15 01:12:13 -04:00
Posts.getPostFields(pid, ['pid', 'tid', 'uid', 'content', 'timestamp'], next);
},
2016-02-23 13:08:38 +02:00
function (_post, next) {
2015-03-15 01:12:13 -04:00
postData = _post;
2016-05-13 10:28:01 +03:00
topics.getTopicFields(_post.tid, ['tid', 'cid', 'pinned'], next);
2015-03-15 01:12:13 -04:00
},
2016-05-13 10:28:01 +03:00
function (topicData, next) {
postData.cid = topicData.cid;
2014-10-14 22:51:48 -04:00
async.parallel([
function (next) {
2018-10-30 19:41:06 -04:00
topics.updateLastPostTimeFromLastPid(postData.tid, next);
2014-10-14 22:51:48 -04:00
},
function (next) {
2018-10-30 19:41:06 -04:00
if (isDeleting) {
db.sortedSetRemove('cid:' + topicData.cid + ':pids', pid, next);
} else {
db.sortedSetAdd('cid:' + topicData.cid + ':pids', postData.timestamp, pid, next);
}
},
function (next) {
topics.updateTeaser(postData.tid, next);
2017-02-17 19:31:21 -07:00
},
2016-05-13 10:28:01 +03:00
], next);
},
function (results, next) {
2018-10-30 19:41:06 -04:00
plugins.fireHook('action:post.' + type, { post: _.clone(postData), uid: uid });
2016-05-13 10:28:01 +03:00
next(null, postData);
2017-02-17 19:31:21 -07:00
},
2015-03-15 01:12:13 -04:00
], callback);
2018-10-30 19:41:06 -04:00
}
2014-10-14 22:51:48 -04:00
Posts.purge = function (pid, uid, callback) {
2018-10-30 19:41:06 -04:00
let postData;
2016-02-23 13:08:38 +02:00
async.waterfall([
function (next) {
2018-10-30 19:41:06 -04:00
Posts.getPostData(pid, next);
2016-02-23 13:08:38 +02:00
},
2018-10-30 19:41:06 -04:00
function (_postData, next) {
postData = _postData;
if (!postData) {
2016-02-23 13:08:38 +02:00
return callback();
2014-10-31 22:04:09 -04:00
}
plugins.fireHook('filter:post.purge', { post: postData, pid: pid, uid: uid }, next);
2016-02-23 13:08:38 +02:00
},
function (data, next) {
async.parallel([
async.apply(deletePostFromTopicUserNotification, postData),
async.apply(deletePostFromCategoryRecentPosts, pid),
async.apply(deletePostFromUsersBookmarks, pid),
async.apply(deletePostFromUsersVotes, pid),
async.apply(deletePostFromReplies, postData),
async.apply(deletePostFromGroups, postData),
async.apply(db.sortedSetsRemove, ['posts:pid', 'posts:votes', 'posts:flagged'], pid),
], err => next(err));
2017-01-12 14:41:35 +03:00
},
function (next) {
2017-02-24 12:46:40 -05:00
plugins.fireHook('action:post.purge', { post: postData, uid: uid });
2017-01-12 14:41:35 +03:00
db.delete('post:' + pid, next);
2017-02-24 12:46:40 -05:00
},
2016-02-23 13:08:38 +02:00
], callback);
2014-06-09 12:51:49 -04:00
};
function deletePostFromTopicUserNotification(postData, callback) {
2016-11-10 15:26:53 +03:00
async.waterfall([
function (next) {
db.sortedSetsRemove([
'tid:' + postData.tid + ':posts',
'tid:' + postData.tid + ':posts:votes',
2017-02-17 19:31:21 -07:00
'uid:' + postData.uid + ':posts',
], postData.pid, next);
2016-11-10 15:26:53 +03:00
},
function (next) {
topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned'], next);
},
function (topicData, next) {
const tasks = [
async.apply(db.decrObjectField, 'global', 'postCount'),
async.apply(db.decrObjectField, 'category:' + topicData.cid, 'post_count'),
async.apply(db.sortedSetRemove, 'cid:' + topicData.cid + ':uid:' + postData.uid + ':pids', postData.pid),
async.apply(db.sortedSetRemove, 'cid:' + topicData.cid + ':uid:' + postData.uid + ':pids:votes', postData.pid),
async.apply(topics.decreasePostCount, postData.tid),
async.apply(topics.updateTeaser, postData.tid),
async.apply(topics.updateLastPostTimeFromLastPid, postData.tid),
async.apply(db.sortedSetIncrBy, 'tid:' + postData.tid + ':posters', -1, postData.uid),
async.apply(user.incrementUserPostCountBy, postData.uid, -1),
async.apply(notifications.rescind, 'new_post:tid:' + postData.tid + ':pid:' + postData.pid + ':uid:' + postData.uid),
];
if (!topicData.pinned) {
tasks.push(async.apply(db.sortedSetIncrBy, 'cid:' + topicData.cid + ':tids:posts', -1, postData.tid));
}
async.parallel(tasks, next);
2017-02-17 19:31:21 -07:00
},
2016-11-10 15:26:53 +03:00
], function (err) {
callback(err);
2014-06-10 14:24:50 -04:00
});
}
function deletePostFromCategoryRecentPosts(pid, callback) {
2017-05-25 16:40:03 -04:00
async.waterfall([
function (next) {
db.getSortedSetRange('categories:cid', 0, -1, next);
},
function (cids, next) {
2018-10-30 19:41:06 -04:00
const sets = cids.map(cid => 'cid:' + cid + ':pids');
2017-05-25 16:40:03 -04:00
db.sortedSetsRemove(sets, pid, next);
},
], callback);
2014-06-10 14:24:50 -04:00
}
2016-10-08 19:09:48 +03:00
function deletePostFromUsersBookmarks(pid, callback) {
2017-05-25 16:40:03 -04:00
async.waterfall([
function (next) {
db.getSetMembers('pid:' + pid + ':users_bookmarked', next);
},
function (uids, next) {
2018-10-30 19:41:06 -04:00
const sets = uids.map(uid => 'uid:' + uid + ':bookmarks');
2017-05-25 16:40:03 -04:00
db.sortedSetsRemove(sets, pid, next);
},
function (next) {
db.delete('pid:' + pid + ':users_bookmarked', next);
},
], callback);
2014-06-10 14:24:50 -04:00
}
function deletePostFromUsersVotes(pid, callback) {
2017-05-25 16:40:03 -04:00
async.waterfall([
function (next) {
async.parallel({
upvoters: function (next) {
db.getSetMembers('pid:' + pid + ':upvote', next);
},
downvoters: function (next) {
db.getSetMembers('pid:' + pid + ':downvote', next);
},
}, next);
2017-02-17 19:31:21 -07:00
},
2017-05-25 16:40:03 -04:00
function (results, next) {
async.parallel([
function (next) {
2018-10-30 19:41:06 -04:00
const upvoterSets = results.upvoters.map(uid => 'uid:' + uid + ':upvote');
const downvoterSets = results.downvoters.map(uid => 'uid:' + uid + ':downvote');
db.sortedSetsRemove(upvoterSets.concat(downvoterSets), pid, next);
2017-05-25 16:40:03 -04:00
},
function (next) {
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
},
], next);
},
], callback);
2014-06-10 14:24:50 -04:00
}
function deletePostFromReplies(postData, callback) {
if (!parseInt(postData.toPid, 10)) {
return setImmediate(callback);
}
async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + postData.toPid + ':replies', postData.pid),
async.apply(db.decrObjectField, 'post:' + postData.toPid, 'replies'),
2017-05-25 16:40:03 -04:00
], callback);
2016-11-10 15:26:53 +03:00
}
function deletePostFromGroups(postData, callback) {
if (!parseInt(postData.uid, 10)) {
return setImmediate(callback);
}
async.waterfall([
function (next) {
groups.getUserGroupMembership('groups:visible:createtime', [postData.uid], next);
},
function (groupNames, next) {
groupNames = groupNames[0];
2018-10-30 19:41:06 -04:00
const keys = groupNames.map(groupName => 'group:' + groupName + ':member:pids');
db.sortedSetsRemove(keys, postData.pid, next);
},
], callback);
}
};