Files
NodeBB/src/posts/delete.js

138 lines
5.0 KiB
JavaScript
Raw Normal View History

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