2015-09-25 15:09:25 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
const posts = require('../../posts');
|
|
|
|
|
const topics = require('../../topics');
|
|
|
|
|
const events = require('../../events');
|
|
|
|
|
const websockets = require('../index');
|
|
|
|
|
const socketTopics = require('../topics');
|
|
|
|
|
const privileges = require('../../privileges');
|
|
|
|
|
const plugins = require('../../plugins');
|
|
|
|
|
const social = require('../../social');
|
|
|
|
|
const user = require('../../user');
|
|
|
|
|
const utils = require('../../utils');
|
2018-02-15 14:52:49 -05:00
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (SocketPosts) {
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.loadPostTools = async function (socket, data) {
|
2016-12-20 16:03:01 +03:00
|
|
|
if (!data || !data.pid || !data.cid) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2015-10-24 20:50:43 -04:00
|
|
|
}
|
2018-02-15 14:52:49 -05:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
posts: posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip']),
|
|
|
|
|
isAdmin: user.isAdministrator(socket.uid),
|
|
|
|
|
isGlobalMod: user.isGlobalModerator(socket.uid),
|
|
|
|
|
isModerator: user.isModerator(socket.uid, data.cid),
|
|
|
|
|
canEdit: privileges.posts.canEdit(data.pid, socket.uid),
|
|
|
|
|
canDelete: privileges.posts.canDelete(data.pid, socket.uid),
|
|
|
|
|
canPurge: privileges.posts.canPurge(data.pid, socket.uid),
|
|
|
|
|
canFlag: privileges.posts.canFlag(data.pid, socket.uid),
|
|
|
|
|
bookmarked: posts.hasBookmarked(data.pid, socket.uid),
|
|
|
|
|
tools: plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }),
|
|
|
|
|
postSharing: social.getActivePostSharing(),
|
|
|
|
|
history: posts.diffs.exists(data.pid),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const postData = results.posts;
|
|
|
|
|
postData.tools = results.tools.tools;
|
|
|
|
|
postData.bookmarked = results.bookmarked;
|
|
|
|
|
postData.selfPost = socket.uid && socket.uid === postData.uid;
|
|
|
|
|
postData.display_edit_tools = results.canEdit.flag;
|
|
|
|
|
postData.display_delete_tools = results.canDelete.flag;
|
|
|
|
|
postData.display_purge_tools = results.canPurge;
|
|
|
|
|
postData.display_flag_tools = socket.uid && !postData.selfPost && results.canFlag.flag;
|
|
|
|
|
postData.display_moderator_tools = postData.display_edit_tools || postData.display_delete_tools;
|
|
|
|
|
postData.display_move_tools = results.isAdmin || results.isModerator;
|
|
|
|
|
postData.display_change_owner_tools = results.isAdmin || results.isModerator;
|
|
|
|
|
postData.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !postData.selfPost;
|
|
|
|
|
postData.display_history = results.history;
|
|
|
|
|
postData.toolsVisible = postData.tools.length || postData.display_moderator_tools;
|
|
|
|
|
|
|
|
|
|
if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
|
|
|
|
|
postData.ip = undefined;
|
|
|
|
|
}
|
|
|
|
|
return results;
|
2015-10-24 20:50:43 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.delete = async function (socket, data) {
|
2016-10-12 14:46:04 +03:00
|
|
|
if (!data || !data.pid) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
const postData = await posts.tools.delete(socket.uid, data.pid);
|
|
|
|
|
const results = await isMainAndLastPost(data.pid);
|
|
|
|
|
if (results.isMain && results.isLast) {
|
|
|
|
|
await deleteOrRestoreTopicOf('delete', data.pid, socket);
|
2016-10-12 14:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
|
2016-10-12 14:46:04 +03:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
await events.log({
|
|
|
|
|
type: 'post-delete',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
pid: data.pid,
|
|
|
|
|
tid: postData.tid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
});
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.restore = async function (socket, data) {
|
2016-10-12 14:46:04 +03:00
|
|
|
if (!data || !data.pid) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2016-10-12 14:46:04 +03:00
|
|
|
}
|
2019-09-09 19:19:56 -04:00
|
|
|
const postData = await posts.tools.restore(socket.uid, data.pid);
|
|
|
|
|
const results = await isMainAndLastPost(data.pid);
|
|
|
|
|
if (results.isMain && results.isLast) {
|
|
|
|
|
await deleteOrRestoreTopicOf('restore', data.pid, socket);
|
|
|
|
|
}
|
|
|
|
|
websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
|
|
|
|
|
|
|
|
|
|
await events.log({
|
|
|
|
|
type: 'post-restore',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
pid: data.pid,
|
|
|
|
|
tid: postData.tid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
});
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.deletePosts = async function (socket, data) {
|
2015-12-24 12:08:10 +02:00
|
|
|
if (!data || !Array.isArray(data.pids)) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
for (const pid of data.pids) {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
await SocketPosts.delete(socket, { pid: pid, tid: data.tid });
|
2015-12-24 12:08:10 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.purgePosts = async function (socket, data) {
|
2015-12-24 12:08:10 +02:00
|
|
|
if (!data || !Array.isArray(data.pids)) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
for (const pid of data.pids) {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
await SocketPosts.purge(socket, { pid: pid, tid: data.tid });
|
2015-12-24 12:08:10 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.purge = async function (socket, data) {
|
2015-09-25 15:09:25 -04:00
|
|
|
if (!data || !parseInt(data.pid, 10)) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const results = await isMainAndLastPost(data.pid);
|
|
|
|
|
if (results.isMain && !results.isLast) {
|
|
|
|
|
throw new Error('[[error:cant-purge-main-post]]');
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
2017-04-26 14:19:48 -04:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
const isMainAndLast = results.isMain && results.isLast;
|
|
|
|
|
const postData = await posts.getPostFields(data.pid, ['toPid', 'tid']);
|
|
|
|
|
postData.pid = data.pid;
|
|
|
|
|
|
|
|
|
|
await posts.tools.purge(socket.uid, data.pid);
|
|
|
|
|
|
|
|
|
|
websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
|
|
|
|
|
const topicData = await topics.getTopicFields(data.tid, ['title', 'cid']);
|
|
|
|
|
|
|
|
|
|
await events.log({
|
|
|
|
|
type: 'post-purge',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
pid: data.pid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
tid: postData.tid,
|
|
|
|
|
title: String(topicData.title),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isMainAndLast) {
|
|
|
|
|
await socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
|
|
|
|
|
}
|
2015-09-25 15:09:25 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
async function deleteOrRestoreTopicOf(command, pid, socket) {
|
|
|
|
|
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
|
|
|
|
|
if (command === 'delete' && !topic.deleted) {
|
|
|
|
|
await socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
|
|
|
|
|
} else if (command === 'restore' && topic.deleted) {
|
|
|
|
|
await socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
|
|
|
|
|
}
|
2016-10-12 14:46:04 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
async function isMainAndLastPost(pid) {
|
|
|
|
|
const [isMain, topicData] = await Promise.all([
|
|
|
|
|
posts.isMain(pid),
|
|
|
|
|
posts.getTopicFields(pid, ['postcount']),
|
|
|
|
|
]);
|
|
|
|
|
return {
|
|
|
|
|
isMain: isMain,
|
|
|
|
|
isLast: topicData && topicData.postcount === 1,
|
|
|
|
|
};
|
2015-09-25 15:09:25 -04:00
|
|
|
}
|
2019-07-12 14:06:09 -04:00
|
|
|
|
|
|
|
|
SocketPosts.changeOwner = async function (socket, data) {
|
|
|
|
|
if (!data || !Array.isArray(data.pids) || !data.toUid) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
const isAdminOrGlobalMod = user.isAdminOrGlobalMod(socket.uid);
|
|
|
|
|
if (!isAdminOrGlobalMod) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await posts.changeOwner(data.pids, data.toUid);
|
|
|
|
|
};
|
2016-08-06 20:28:55 -05:00
|
|
|
};
|