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

265 lines
7.7 KiB
JavaScript
Raw Normal View History

'use strict';
var async = require('async');
var posts = require('../../posts');
2016-07-25 15:23:50 +03:00
var topics = require('../../topics');
var events = require('../../events');
var websockets = require('../index');
var socketTopics = require('../topics');
2015-10-24 20:50:43 -04:00
var privileges = require('../../privileges');
2015-10-28 20:13:18 -04:00
var plugins = require('../../plugins');
2016-02-25 14:05:48 -05:00
var social = require('../../social');
2018-02-15 14:52:49 -05:00
var user = require('../../user');
module.exports = function (SocketPosts) {
SocketPosts.loadPostTools = function (socket, data, callback) {
2016-12-20 16:03:01 +03:00
if (!data || !data.pid || !data.cid) {
2015-10-28 20:13:18 -04:00
return callback(new Error('[[error:invalid-data]]'));
2015-10-24 20:50:43 -04:00
}
2016-12-20 16:03:01 +03:00
async.waterfall([
function (next) {
async.parallel({
posts: function (next) {
2018-02-15 14:52:49 -05:00
posts.getPostFields(data.pid, ['deleted', 'bookmarks', 'uid', 'ip'], next);
},
isAdmin: function (next) {
user.isAdministrator(socket.uid, next);
2016-12-20 16:03:01 +03:00
},
2018-02-15 14:52:49 -05:00
isGlobalMod: function (next) {
user.isGlobalModerator(socket.uid, next);
},
isModerator: function (next) {
user.isModerator(socket.uid, data.cid, next);
2016-12-20 16:03:01 +03:00
},
canEdit: function (next) {
privileges.posts.canEdit(data.pid, socket.uid, next);
},
canDelete: function (next) {
privileges.posts.canDelete(data.pid, socket.uid, next);
},
2018-08-30 15:08:01 -04:00
canPurge: function (next) {
privileges.posts.canPurge(data.pid, socket.uid, next);
},
2017-08-18 20:08:19 -04:00
canFlag: function (next) {
privileges.posts.canFlag(data.pid, socket.uid, next);
},
2016-12-20 16:03:01 +03:00
bookmarked: function (next) {
posts.hasBookmarked(data.pid, socket.uid, next);
},
tools: function (next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:post.tools', { pid: data.pid, uid: socket.uid, tools: [] }, next);
2016-12-20 16:03:01 +03:00
},
postSharing: function (next) {
social.getActivePostSharing(next);
2017-02-17 19:31:21 -07:00
},
history: async.apply(posts.diffs.exists, data.pid),
2016-12-20 16:03:01 +03:00
}, next);
2016-02-25 14:05:48 -05:00
},
2016-12-20 16:03:01 +03:00
function (results, next) {
2018-06-03 15:44:48 -04:00
var posts = results.posts;
posts.tools = results.tools.tools;
posts.bookmarked = results.bookmarked;
2018-10-25 17:02:59 -04:00
posts.selfPost = socket.uid && socket.uid === posts.uid;
2018-06-03 15:44:48 -04:00
posts.display_edit_tools = results.canEdit.flag;
posts.display_delete_tools = results.canDelete.flag;
2018-08-30 15:08:01 -04:00
posts.display_purge_tools = results.canPurge;
2018-06-03 15:44:48 -04:00
posts.display_flag_tools = socket.uid && !posts.selfPost && results.canFlag.flag;
posts.display_moderator_tools = posts.display_edit_tools || posts.display_delete_tools;
posts.display_move_tools = results.isAdmin || results.isModerator;
2019-07-13 23:51:18 -04:00
posts.display_change_owner_tools = results.isAdmin || results.isModerator;
2018-06-03 15:44:48 -04:00
posts.display_ip_ban = (results.isAdmin || results.isGlobalMod) && !posts.selfPost;
posts.display_history = results.history;
posts.toolsVisible = posts.tools.length || posts.display_moderator_tools;
2018-02-15 14:52:49 -05:00
if (!results.isAdmin && !results.isGlobalMod && !results.isModerator) {
2018-06-03 15:44:48 -04:00
posts.ip = undefined;
2018-02-15 14:52:49 -05:00
}
2016-12-20 16:03:01 +03:00
next(null, results);
2017-02-17 19:31:21 -07:00
},
2016-12-20 16:03:01 +03:00
], callback);
2015-10-24 20:50:43 -04:00
};
SocketPosts.delete = function (socket, data, callback) {
2016-10-12 14:46:04 +03:00
if (!data || !data.pid) {
return callback(new Error('[[error:invalid-data]]'));
}
var postData;
async.waterfall([
function (next) {
2016-10-12 14:46:04 +03:00
posts.tools.delete(socket.uid, data.pid, next);
},
function (_postData, next) {
2016-10-12 14:46:04 +03:00
postData = _postData;
isMainAndLastPost(data.pid, next);
},
function (results, next) {
2016-10-12 14:46:04 +03:00
if (results.isMain && results.isLast) {
2017-11-23 14:19:22 -05:00
deleteOrRestoreTopicOf('delete', data.pid, socket, next);
2016-10-12 14:46:04 +03:00
} else {
next();
}
},
function (next) {
2016-10-12 14:46:04 +03:00
websockets.in('topic_' + data.tid).emit('event:post_deleted', postData);
events.log({
type: 'post-delete',
uid: socket.uid,
pid: data.pid,
tid: postData.tid,
2017-02-17 19:31:21 -07:00
ip: socket.ip,
2016-10-12 14:46:04 +03:00
});
next();
2017-02-17 19:31:21 -07:00
},
2016-10-12 14:46:04 +03:00
], callback);
};
SocketPosts.restore = function (socket, data, callback) {
2016-10-12 14:46:04 +03:00
if (!data || !data.pid) {
return callback(new Error('[[error:invalid-data]]'));
}
2017-11-23 14:19:22 -05:00
var postData;
2016-12-20 16:03:01 +03:00
async.waterfall([
function (next) {
posts.tools.restore(socket.uid, data.pid, next);
},
2017-11-23 14:19:22 -05:00
function (_postData, next) {
postData = _postData;
isMainAndLastPost(data.pid, next);
},
function (results, next) {
if (results.isMain && results.isLast) {
deleteOrRestoreTopicOf('restore', data.pid, socket, next);
} else {
setImmediate(next);
}
},
function (next) {
2016-12-20 16:03:01 +03:00
websockets.in('topic_' + data.tid).emit('event:post_restored', postData);
2016-10-12 14:46:04 +03:00
2016-12-20 16:03:01 +03:00
events.log({
type: 'post-restore',
uid: socket.uid,
pid: data.pid,
tid: postData.tid,
2017-02-17 19:31:21 -07:00
ip: socket.ip,
2016-12-20 16:03:01 +03:00
});
2016-10-12 14:46:04 +03:00
2016-12-20 16:03:01 +03:00
setImmediate(next);
2017-02-17 19:31:21 -07:00
},
2016-12-20 16:03:01 +03:00
], callback);
};
SocketPosts.deletePosts = function (socket, data, callback) {
2015-12-24 12:08:10 +02:00
if (!data || !Array.isArray(data.pids)) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-12-20 16:03:01 +03:00
async.eachSeries(data.pids, function (pid, next) {
2017-02-18 12:30:49 -07:00
SocketPosts.delete(socket, { pid: pid, tid: data.tid }, next);
2015-12-24 12:08:10 +02:00
}, callback);
};
SocketPosts.purgePosts = function (socket, data, callback) {
2015-12-24 12:08:10 +02:00
if (!data || !Array.isArray(data.pids)) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-12-20 16:03:01 +03:00
async.eachSeries(data.pids, function (pid, next) {
2017-02-18 12:30:49 -07:00
SocketPosts.purge(socket, { pid: pid, tid: data.tid }, next);
2015-12-24 12:08:10 +02:00
}, callback);
};
SocketPosts.purge = function (socket, data, callback) {
if (!data || !parseInt(data.pid, 10)) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-12-20 16:03:01 +03:00
var postData;
var topicData;
var isMainAndLast = false;
2016-12-20 16:03:01 +03:00
async.waterfall([
function (next) {
isMainAndLastPost(data.pid, next);
},
function (results, next) {
if (results.isMain && !results.isLast) {
return next(new Error('[[error:cant-purge-main-post]]'));
2016-12-20 16:03:01 +03:00
}
isMainAndLast = results.isMain && results.isLast;
posts.getPostFields(data.pid, ['toPid', 'tid'], next);
2016-12-20 16:03:01 +03:00
},
function (_postData, next) {
postData = _postData;
postData.pid = data.pid;
2016-12-20 16:03:01 +03:00
posts.tools.purge(socket.uid, data.pid, next);
},
function (next) {
websockets.in('topic_' + data.tid).emit('event:post_purged', postData);
topics.getTopicFields(data.tid, ['title', 'cid'], next);
2016-12-20 16:03:01 +03:00
},
function (_topicData, next) {
topicData = _topicData;
2016-12-20 16:03:01 +03:00
events.log({
type: 'post-purge',
uid: socket.uid,
pid: data.pid,
ip: socket.ip,
2018-10-04 12:18:11 -04:00
tid: postData.tid,
title: String(topicData.title),
2016-12-20 16:03:01 +03:00
}, next);
2017-02-17 19:31:21 -07:00
},
function (next) {
if (isMainAndLast) {
socketTopics.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid }, next);
} else {
setImmediate(next);
}
},
2016-12-20 16:03:01 +03:00
], callback);
};
2017-11-23 14:19:22 -05:00
function deleteOrRestoreTopicOf(command, pid, socket, callback) {
2016-12-20 16:03:01 +03:00
async.waterfall([
function (next) {
2017-11-23 14:19:22 -05:00
posts.getTopicFields(pid, ['tid', 'cid', 'deleted'], next);
2016-12-20 16:03:01 +03:00
},
function (topic, next) {
2018-10-25 17:02:59 -04:00
if (command === 'delete' && !topic.deleted) {
2017-11-23 14:19:22 -05:00
socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid }, next);
2018-10-25 17:02:59 -04:00
} else if (command === 'restore' && topic.deleted) {
2017-11-23 14:19:22 -05:00
socketTopics.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid }, next);
} else {
setImmediate(next);
}
2017-02-17 19:31:21 -07:00
},
2016-12-20 16:03:01 +03:00
], callback);
2016-10-12 14:46:04 +03:00
}
function isMainAndLastPost(pid, callback) {
async.parallel({
isMain: function (next) {
posts.isMain(pid, next);
},
isLast: function (next) {
posts.getTopicFields(pid, ['postcount'], function (err, topic) {
2018-10-25 17:02:59 -04:00
next(err, topic ? topic.postcount === 1 : false);
});
2017-02-17 19:31:21 -07:00
},
}, callback);
}
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);
};
};