Files
NodeBB/src/posts/delete.js

301 lines
7.8 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) {
2015-03-15 01:12:13 -04:00
var postData;
async.waterfall([
2016-02-23 13:08:38 +02:00
function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.delete', { pid: pid, uid: uid }, next);
2016-02-23 13:08:38 +02:00
},
function (data, next) {
2017-02-18 12:30:49 -07:00
Posts.setPostFields(pid, { deleted: 1, deleterUid: uid }, 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', '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) {
topics.updateLastPostTimeFromLastPid(postData.tid, next);
2014-10-14 22:51:48 -04:00
},
function (next) {
2016-05-13 10:28:01 +03:00
db.sortedSetRemove('cid:' + topicData.cid + ':pids', 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) {
2017-02-24 12:46:40 -05:00
plugins.fireHook('action:post.delete', { 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);
2014-10-14 22:51:48 -04:00
};
Posts.restore = function (pid, uid, callback) {
2015-03-15 01:12:13 -04:00
var postData;
async.waterfall([
2016-02-23 13:08:38 +02:00
function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.restore', { pid: pid, uid: uid }, next);
2016-02-23 13:08:38 +02:00
},
function (data, next) {
2017-02-18 12:30:49 -07:00
Posts.setPostFields(pid, { deleted: 0, deleterUid: 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) {
topics.updateLastPostTimeFromLastPid(topicData.tid, next);
2014-10-14 22:51:48 -04:00
},
function (next) {
2016-05-13 10:28:01 +03:00
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) {
2017-02-24 12:46:40 -05:00
plugins.fireHook('action:post.restore', { 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);
2014-10-14 22:51:48 -04:00
};
Posts.purge = function (pid, uid, callback) {
2016-02-23 13:08:38 +02:00
async.waterfall([
function (next) {
Posts.exists(pid, next);
},
function (exists, next) {
if (!exists) {
return callback();
2014-10-31 22:04:09 -04:00
}
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.purge', { pid: pid, uid: uid }, next);
2016-02-23 13:08:38 +02:00
},
function (data, next) {
async.parallel([
function (next) {
2016-11-10 15:26:53 +03:00
deletePostFromTopicUserNotification(pid, next);
2016-02-23 13:08:38 +02:00
},
function (next) {
deletePostFromCategoryRecentPosts(pid, next);
},
function (next) {
2016-10-08 19:09:48 +03:00
deletePostFromUsersBookmarks(pid, next);
2016-02-23 13:08:38 +02:00
},
function (next) {
deletePostFromUsersVotes(pid, next);
},
function (next) {
2016-11-10 15:26:53 +03:00
deletePostFromReplies(pid, next);
},
function (next) {
deletePostFromGroups(pid, next);
},
2016-02-23 13:08:38 +02:00
function (next) {
2017-06-01 20:57:50 -04:00
db.sortedSetsRemove(['posts:pid', 'posts:votes', 'posts:flagged'], pid, next);
2016-02-23 13:08:38 +02:00
},
], function (err) {
2017-01-12 14:41:35 +03:00
next(err);
2016-02-23 13:08:38 +02:00
});
2017-01-12 14:41:35 +03:00
},
function (next) {
Posts.getPostData(pid, next);
},
function (postData, 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
};
2016-11-10 15:26:53 +03:00
function deletePostFromTopicUserNotification(pid, callback) {
var postData;
async.waterfall([
function (next) {
Posts.getPostFields(pid, ['tid', 'uid'], next);
},
function (_postData, next) {
postData = _postData;
db.sortedSetsRemove([
'tid:' + postData.tid + ':posts',
'tid:' + postData.tid + ':posts:votes',
2017-02-17 19:31:21 -07:00
'uid:' + postData.uid + ':posts',
2016-11-10 15:26:53 +03:00
], pid, next);
},
function (next) {
topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned'], next);
},
function (topicData, next) {
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);
},
function (next) {
topics.updateLastPostTimeFromLastPid(postData.tid, next);
2016-11-10 15:26:53 +03:00
},
function (next) {
2018-10-25 17:02:59 -04:00
if (!topicData.pinned) {
db.sortedSetIncrBy('cid:' + topicData.cid + ':tids:posts', -1, postData.tid, next);
} else {
next();
}
2016-11-10 15:26:53 +03:00
},
function (next) {
db.sortedSetIncrBy('tid:' + postData.tid + ':posters', -1, postData.uid, next);
},
function (next) {
user.incrementUserPostCountBy(postData.uid, -1, next);
},
function (next) {
notifications.rescind('new_post:tid:' + postData.tid + ':pid:' + pid + ':uid:' + postData.uid, next);
2017-02-17 19:31:21 -07:00
},
2016-11-10 15:26:53 +03:00
], 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) {
var sets = cids.map(function (cid) {
return '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) {
var sets = uids.map(function (uid) {
return 'uid:' + uid + ':bookmarks';
});
2014-06-10 14:24:50 -04:00
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) {
var upvoterSets = results.upvoters.map(function (uid) {
return 'uid:' + uid + ':upvote';
});
2017-05-25 16:40:03 -04:00
var downvoterSets = results.downvoters.map(function (uid) {
return 'uid:' + uid + ':downvote';
});
2017-05-25 16:40:03 -04:00
async.parallel([
function (next) {
db.sortedSetsRemove(upvoterSets, pid, next);
},
function (next) {
db.sortedSetsRemove(downvoterSets, pid, next);
},
function (next) {
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
},
], next);
},
], callback);
2014-06-10 14:24:50 -04:00
}
2016-11-10 15:26:53 +03:00
function deletePostFromReplies(pid, callback) {
2017-05-25 16:40:03 -04:00
async.waterfall([
function (next) {
Posts.getPostField(pid, 'toPid', next);
},
function (toPid, next) {
if (!parseInt(toPid, 10)) {
return callback(null);
}
async.parallel([
async.apply(db.sortedSetRemove, 'pid:' + toPid + ':replies', pid),
async.apply(db.decrObjectField, 'post:' + toPid, 'replies'),
], next);
},
], callback);
2016-11-10 15:26:53 +03:00
}
function deletePostFromGroups(pid, callback) {
async.waterfall([
function (next) {
Posts.getPostField(pid, 'uid', next);
},
function (uid, next) {
if (!parseInt(uid, 10)) {
return callback();
}
groups.getUserGroupMembership('groups:visible:createtime', [uid], next);
},
function (groupNames, next) {
groupNames = groupNames[0];
var keys = groupNames.map(function (groupName) {
return 'group:' + groupName + ':member:pids';
});
db.sortedSetsRemove(keys, pid, next);
},
], callback);
}
};