Files
NodeBB/src/socket.io/posts/tools.js

120 lines
4.2 KiB
JavaScript
Raw Normal View History

'use strict';
2019-09-09 19:19:56 -04:00
const posts = require('../../posts');
2020-07-30 09:48:14 -04:00
const flags = require('../../flags');
2019-09-09 19:19:56 -04:00
const events = require('../../events');
const privileges = require('../../privileges');
const plugins = require('../../plugins');
const social = require('../../social');
const user = require('../../user');
const utils = require('../../utils');
2020-10-17 21:24:33 -04:00
const api = require('../../api');
2018-02-15 14:52:49 -05:00
const sockets = require('..');
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', 'flagId']),
2019-09-09 19:19:56 -04:00
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),
2020-07-30 09:48:14 -04:00
flagged: flags.exists('post', data.pid, socket.uid), // specifically, whether THIS calling user flagged
2019-09-09 19:19:56 -04:00
bookmarked: posts.hasBookmarked(data.pid, socket.uid),
tools: plugins.hooks.fire('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }),
2019-09-09 19:19:56 -04:00
postSharing: social.getActivePostSharing(),
history: posts.diffs.exists(data.pid),
canViewInfo: privileges.global.can('view:users:info', socket.uid),
2019-09-09 19:19:56 -04:00
});
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 && results.canFlag.flag;
2019-09-09 19:19:56 -04:00
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;
2020-07-30 09:48:14 -04:00
postData.flags = {
flagId: parseInt(results.posts.flagId, 10) || null,
can: results.canFlag.flag,
exists: !!results.posts.flagId,
flagged: results.flagged,
};
2019-09-09 19:19:56 -04:00
if (!results.isAdmin && !results.canViewInfo) {
2019-09-09 19:19:56 -04:00
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) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/state');
2020-10-17 21:24:33 -04:00
await api.posts.delete(socket, data);
};
2019-09-09 19:19:56 -04:00
SocketPosts.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/state');
2020-10-17 21:24:33 -04:00
await api.posts.restore(socket, data);
2019-09-09 19:34:20 -04:00
};
2019-09-09 19:19:56 -04:00
SocketPosts.deletePosts = async function (socket, data) {
2019-09-09 19:34:20 -04:00
await deletePurgePosts(socket, data, 'delete');
2015-12-24 12:08:10 +02:00
};
2019-09-09 19:19:56 -04:00
SocketPosts.purgePosts = async function (socket, data) {
2019-09-09 19:34:20 -04:00
await deletePurgePosts(socket, data, 'purge');
};
async function deletePurgePosts(socket, data, command) {
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[command](socket, { pid: pid });
2015-12-24 12:08:10 +02:00
}
2019-09-09 19:34:20 -04:00
}
2015-12-24 12:08:10 +02:00
2019-09-09 19:19:56 -04:00
SocketPosts.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid');
2020-10-17 21:24:33 -04:00
await api.posts.purge(socket, data);
};
SocketPosts.changeOwner = async function (socket, data) {
if (!data || !Array.isArray(data.pids) || !data.toUid) {
throw new Error('[[error:invalid-data]]');
}
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(socket.uid);
if (!isAdminOrGlobalMod) {
throw new Error('[[error:no-privileges]]');
}
2021-02-04 00:06:15 -07:00
const postData = await posts.changeOwner(data.pids, data.toUid);
const logs = postData.map(({ pid, uid, cid }) => (events.log({
type: 'post-change-owner',
uid: socket.uid,
ip: socket.ip,
targetUid: data.toUid,
pid: pid,
originalUid: uid,
cid: cid,
})));
await Promise.all(logs);
};
};