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');
|
2020-07-25 18:26:09 -04:00
|
|
|
const flags = require('../flags');
|
2014-06-09 12:51:49 -04:00
|
|
|
|
2016-10-13 11:43:39 +02: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';
|
2021-02-03 23:59:08 -07:00
|
|
|
await plugins.hooks.fire(`filter:post.${type}`, { pid: pid, uid: uid });
|
2019-07-17 00:17:21 -04:00
|
|
|
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 ?
|
2021-02-03 23:59:08 -07:00
|
|
|
db.sortedSetRemove(`cid:${topicData.cid}:pids`, pid) :
|
|
|
|
|
db.sortedSetAdd(`cid:${topicData.cid}:pids`, postData.timestamp, pid),
|
2019-07-17 00:17:21 -04:00
|
|
|
]);
|
2019-07-23 21:11:04 -04:00
|
|
|
await categories.updateRecentTidForCid(postData.cid);
|
2021-02-03 23:59:08 -07:00
|
|
|
plugins.hooks.fire(`action:post.${type}`, { post: _.clone(postData), uid: uid });
|
2020-07-25 18:26:09 -04:00
|
|
|
if (type === 'delete') {
|
|
|
|
|
await flags.resolveFlag('post', pid, uid);
|
|
|
|
|
}
|
2019-07-17 00:17:21 -04:00
|
|
|
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;
|
|
|
|
|
}
|
2019-07-23 21:11:04 -04:00
|
|
|
const topicData = await topics.getTopicFields(postData.tid, ['tid', 'cid', 'pinned']);
|
|
|
|
|
postData.cid = topicData.cid;
|
2020-11-20 16:06:26 -05:00
|
|
|
await plugins.hooks.fire('filter:post.purge', { post: postData, pid: pid, uid: uid });
|
2019-07-17 00:17:21 -04:00
|
|
|
await Promise.all([
|
2019-07-23 21:11:04 -04:00
|
|
|
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
|
|
|
]);
|
2020-07-25 18:26:09 -04:00
|
|
|
await flags.resolveFlag('post', pid, uid);
|
2020-11-20 16:06:26 -05:00
|
|
|
plugins.hooks.fire('action:post.purge', { post: postData, uid: uid });
|
2021-02-03 23:59:08 -07:00
|
|
|
await db.delete(`post:${pid}`);
|
2014-06-09 12:51:49 -04:00
|
|
|
};
|
|
|
|
|
|
2019-07-23 21:11:04 -04:00
|
|
|
async function deletePostFromTopicUserNotification(postData, topicData) {
|
2019-07-17 00:17:21 -04:00
|
|
|
await db.sortedSetsRemove([
|
2021-02-03 23:59:08 -07:00
|
|
|
`tid:${postData.tid}:posts`,
|
|
|
|
|
`tid:${postData.tid}:posts:votes`,
|
|
|
|
|
`uid:${postData.uid}:posts`,
|
2019-07-17 00:17:21 -04:00
|
|
|
], postData.pid);
|
2019-07-23 21:11:04 -04:00
|
|
|
|
2019-07-17 00:17:21 -04:00
|
|
|
const tasks = [
|
|
|
|
|
db.decrObjectField('global', 'postCount'),
|
2021-02-03 23:59:08 -07:00
|
|
|
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),
|
2019-07-17 00:17:21 -04:00
|
|
|
topics.decreasePostCount(postData.tid),
|
|
|
|
|
topics.updateTeaser(postData.tid),
|
|
|
|
|
topics.updateLastPostTimeFromLastPid(postData.tid),
|
2021-02-03 23:59:08 -07:00
|
|
|
db.sortedSetIncrBy(`tid:${postData.tid}:posters`, -1, postData.uid),
|
2021-12-07 18:40:23 -05:00
|
|
|
user.updatePostCount(postData.uid),
|
2021-02-03 23:59:08 -07:00
|
|
|
notifications.rescind(`new_post:tid:${postData.tid}:pid:${postData.pid}:uid:${postData.uid}`),
|
2019-07-17 00:17:21 -04:00
|
|
|
];
|
2019-07-23 21:11:04 -04:00
|
|
|
|
2019-07-17 00:17:21 -04:00
|
|
|
if (!topicData.pinned) {
|
2021-02-03 23:59:08 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-07-23 21:11:04 -04:00
|
|
|
async function deletePostFromCategoryRecentPosts(postData) {
|
2019-07-17 00:17:21 -04:00
|
|
|
const cids = await categories.getAllCidsFromSet('categories:cid');
|
2021-02-03 23:59:08 -07:00
|
|
|
const sets = cids.map(cid => `cid:${cid}:pids`);
|
2019-07-23 21:11:04 -04:00
|
|
|
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) {
|
2021-02-03 23:59:08 -07:00
|
|
|
const uids = await db.getSetMembers(`pid:${pid}:users_bookmarked`);
|
|
|
|
|
const sets = uids.map(uid => `uid:${uid}:bookmarks`);
|
2019-07-17 00:17:21 -04:00
|
|
|
await db.sortedSetsRemove(sets, pid);
|
2021-02-03 23:59:08 -07:00
|
|
|
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([
|
2021-02-03 23:59:08 -07:00
|
|
|
db.getSetMembers(`pid:${pid}:upvote`),
|
|
|
|
|
db.getSetMembers(`pid:${pid}:downvote`),
|
2019-07-17 00:17:21 -04:00
|
|
|
]);
|
2021-02-03 23:59:08 -07:00
|
|
|
const upvoterSets = upvoters.map(uid => `uid:${uid}:upvote`);
|
|
|
|
|
const downvoterSets = downvoters.map(uid => `uid:${uid}:downvote`);
|
2019-07-17 00:17:21 -04:00
|
|
|
await Promise.all([
|
|
|
|
|
db.sortedSetsRemove(upvoterSets.concat(downvoterSets), pid),
|
2021-02-03 23:59:08 -07:00
|
|
|
db.deleteAll([`pid:${pid}:upvote`, `pid:${pid}:downvote`]),
|
2019-07-17 00:17:21 -04:00
|
|
|
]);
|
2014-06-10 14:24:50 -04:00
|
|
|
}
|
|
|
|
|
|
2019-07-17 00:17:21 -04:00
|
|
|
async function deletePostFromReplies(postData) {
|
2021-02-03 23:59:08 -07:00
|
|
|
const replyPids = await db.getSortedSetMembers(`pid:${postData.pid}:replies`);
|
2020-11-26 12:04:01 -05:00
|
|
|
const promises = [
|
|
|
|
|
db.deleteObjectFields(
|
2021-02-03 23:59:08 -07:00
|
|
|
replyPids.map(pid => `post:${pid}`), ['toPid']
|
2020-11-26 12:04:01 -05:00
|
|
|
),
|
2021-02-03 23:59:08 -07:00
|
|
|
db.delete(`pid:${postData.pid}:replies`),
|
2020-11-26 12:04:01 -05:00
|
|
|
];
|
|
|
|
|
if (parseInt(postData.toPid, 10)) {
|
2021-02-03 23:59:08 -07:00
|
|
|
promises.push(db.sortedSetRemove(`pid:${postData.toPid}:replies`, postData.pid));
|
|
|
|
|
promises.push(db.decrObjectField(`post:${postData.toPid}`, 'replies'));
|
2018-10-30 20:25:29 -04:00
|
|
|
}
|
2020-11-26 12:04:01 -05:00
|
|
|
await Promise.all(promises);
|
2016-11-10 15:26:53 +03:00
|
|
|
}
|
2017-05-04 16:32:51 -04:00
|
|
|
|
2019-07-17 00:17:21 -04:00
|
|
|
async function deletePostFromGroups(postData) {
|
2018-10-30 20:25:29 -04:00
|
|
|
if (!parseInt(postData.uid, 10)) {
|
2019-07-17 00:17:21 -04:00
|
|
|
return;
|
2018-10-30 20:25:29 -04:00
|
|
|
}
|
2019-07-17 00:17:21 -04:00
|
|
|
const groupNames = await groups.getUserGroupMembership('groups:visible:createtime', [postData.uid]);
|
2021-02-03 23:59:08 -07:00
|
|
|
const keys = groupNames[0].map(groupName => `group:${groupName}:member:pids`);
|
2019-07-17 00:17:21 -04:00
|
|
|
await db.sortedSetsRemove(keys, postData.pid);
|
2017-05-04 16:32:51 -04:00
|
|
|
}
|
2014-06-17 12:34:09 +07:00
|
|
|
};
|