refactor: post restore/delete/purge

v1.18.x
Barış Soner Uşaklı 4 years ago
parent 4bfd008781
commit 272e73da53

@ -384,16 +384,14 @@ define('forum/topic/postTools', [
} }
function postAction(action, pid) { function postAction(action, pid) {
translator.translate('[[topic:post_' + action + '_confirm]]', function (msg) { bootbox.confirm('[[topic:post_' + action + '_confirm]]', function (confirm) {
bootbox.confirm(msg, function (confirm) { if (!confirm) {
if (!confirm) { return;
return; }
}
const route = action === 'purge' ? '' : '/state'; const route = action === 'purge' ? '' : '/state';
const method = action === 'restore' ? 'put' : 'del'; const method = action === 'restore' ? 'put' : 'del';
api[method](`/posts/${pid}${route}`, undefined, undefined, 'default'); api[method](`/posts/${pid}${route}`).catch(app.alertError);
});
}); });
} }
@ -411,36 +409,34 @@ define('forum/topic/postTools', [
return callback(); return callback();
} }
translator.translate('[[topic:stale.warning]]', function (translated) { var warning = bootbox.dialog({
var warning = bootbox.dialog({ title: '[[topic:stale.title]]',
title: '[[topic:stale.title]]', message: '[[topic:stale.warning]]',
message: translated, buttons: {
buttons: { reply: {
reply: { label: '[[topic:stale.reply_anyway]]',
label: '[[topic:stale.reply_anyway]]', className: 'btn-link',
className: 'btn-link', callback: function () {
callback: function () { staleReplyAnyway = true;
staleReplyAnyway = true; callback();
callback();
},
}, },
create: { },
label: '[[topic:stale.create]]', create: {
className: 'btn-primary', label: '[[topic:stale.create]]',
callback: function () { className: 'btn-primary',
translator.translate('[[topic:link_back, ' + ajaxify.data.title + ', ' + config.relative_path + '/topic/' + ajaxify.data.slug + ']]', function (body) { callback: function () {
$(window).trigger('action:composer.topic.new', { translator.translate('[[topic:link_back, ' + ajaxify.data.title + ', ' + config.relative_path + '/topic/' + ajaxify.data.slug + ']]', function (body) {
cid: ajaxify.data.cid, $(window).trigger('action:composer.topic.new', {
body: body, cid: ajaxify.data.cid,
}); body: body,
}); });
}, });
}, },
}, },
}); },
warning.modal();
}); });
warning.modal();
} }
return PostTools; return PostTools;

@ -5,9 +5,11 @@ const _ = require('lodash');
const utils = require('../utils'); const utils = require('../utils');
const posts = require('../posts'); const posts = require('../posts');
const topics = require('../topics');
const groups = require('../groups'); const groups = require('../groups');
const meta = require('../meta'); const meta = require('../meta');
const events = require('../events'); const events = require('../events');
const privileges = require('../privileges');
const apiHelpers = require('./helpers'); const apiHelpers = require('./helpers');
const websockets = require('../socket.io'); const websockets = require('../socket.io');
@ -63,3 +65,103 @@ postsAPI.edit = async function (caller, data) {
uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult)); uids.forEach(uid => websockets.in('uid_' + uid).emit('event:post_edited', editResult));
return returnData; return returnData;
}; };
postsAPI.delete = async function (caller, data) {
await deleteOrRestore(caller, data, {
command: 'delete',
event: 'event:post_deleted',
type: 'post-delete',
});
};
postsAPI.restore = async function (caller, data) {
await deleteOrRestore(caller, data, {
command: 'restore',
event: 'event:post_restored',
type: 'post-restore',
});
};
async function deleteOrRestore(caller, data, params) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
const postData = await posts.tools[params.command](caller.uid, data.pid);
const results = await isMainAndLastPost(data.pid);
if (results.isMain && results.isLast) {
await deleteOrRestoreTopicOf(params.command, data.pid, caller);
}
websockets.in('topic_' + postData.tid).emit(params.event, postData);
await events.log({
type: params.type,
uid: caller.uid,
pid: data.pid,
tid: postData.tid,
ip: caller.ip,
});
}
async function deleteOrRestoreTopicOf(command, pid, caller) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
// command: delete/restore
await apiHelpers.doTopicAction(command,
topic.deleted ? 'event:topic_restored' : 'event:topic_deleted',
caller,
{ tids: [topic.tid], cid: topic.cid }
);
}
postsAPI.purge = async function (caller, data) {
if (!data || !parseInt(data.pid, 10)) {
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]]');
}
const isMainAndLast = results.isMain && results.isLast;
const postData = await posts.getPostFields(data.pid, ['toPid', 'tid']);
postData.pid = data.pid;
const canPurge = await privileges.posts.canPurge(data.pid, caller.uid);
if (!canPurge) {
throw new Error('[[error:no-privileges]]');
}
require('../posts/cache').del(data.pid);
await posts.purge(data.pid, caller.uid);
websockets.in('topic_' + postData.tid).emit('event:post_purged', postData);
const topicData = await topics.getTopicFields(postData.tid, ['title', 'cid']);
await events.log({
type: 'post-purge',
pid: data.pid,
uid: caller.uid,
ip: caller.ip,
tid: postData.tid,
title: String(topicData.title),
});
if (isMainAndLast) {
await apiHelpers.doTopicAction('purge', 'event:topic_purged',
caller,
{ tids: [postData.tid], cid: topicData.cid }
);
}
};
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,
};
}

@ -1,13 +1,9 @@
'use strict'; 'use strict';
const privileges = require('../../privileges');
const posts = require('../../posts'); const posts = require('../../posts');
const topics = require('../../topics');
const events = require('../../events');
const api = require('../../api'); const api = require('../../api');
const helpers = require('../helpers'); const helpers = require('../helpers');
const sockets = require('../../socket.io');
const apiHelpers = require('../../api/helpers'); const apiHelpers = require('../../api/helpers');
const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh... const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh...
@ -25,64 +21,17 @@ Posts.edit = async (req, res) => {
}; };
Posts.purge = async (req, res) => { Posts.purge = async (req, res) => {
const results = await isMainAndLastPost(req.params.pid); await api.posts.purge(req, { pid: req.params.pid });
if (results.isMain && !results.isLast) {
throw new Error('[[error:cant-purge-main-post]]');
}
const isMainAndLast = results.isMain && results.isLast;
const postData = await posts.getPostFields(req.params.pid, ['pid', 'toPid', 'tid']);
const canPurge = await privileges.posts.canPurge(req.params.pid, req.user.uid);
if (!canPurge) {
throw new Error('[[error:no-privileges]]');
}
require('../../posts/cache').del(req.params.pid);
await posts.purge(req.params.pid, req.user.uid);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
sockets.in('topic_' + postData.tid).emit('event:post_purged', postData);
const topicData = await topics.getTopicFields(postData.tid, ['title', 'cid']);
await events.log({
type: 'post-purge',
uid: req.user.uid,
pid: req.params.pid,
ip: req.ip,
tid: postData.tid,
title: String(topicData.title),
});
if (isMainAndLast) {
await apiHelpers.doTopicAction('purge', 'event:topic_purged', {
ip: req.ip,
uid: req.user.uid,
}, { tids: [postData.tid], cid: topicData.cid });
}
}; };
Posts.restore = async (req, res) => { Posts.restore = async (req, res) => {
await deleteOrRestore(req, { await api.posts.restore(req, { pid: req.params.pid });
pid: req.params.pid,
}, {
command: 'restore',
event: 'event:post_restored',
type: 'post-restore',
});
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
}; };
Posts.delete = async (req, res) => { Posts.delete = async (req, res) => {
await deleteOrRestore(req, { await api.posts.delete(req, { pid: req.params.pid });
pid: req.params.pid,
}, {
command: 'delete',
event: 'event:post_deleted',
type: 'post-delete',
});
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
}; };
@ -128,50 +77,3 @@ Posts.unbookmark = async (req, res) => {
await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data); await socketPostHelpers.postCommand(socketMock, 'unbookmark', 'bookmarked', '', data);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
}; };
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,
};
}
async function deleteOrRestoreTopicOf(command, pid, req) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
if (command === 'delete' && !topic.deleted) {
await apiHelpers.doTopicAction('delete', 'event:topic_deleted', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
} else if (command === 'restore' && topic.deleted) {
await apiHelpers.doTopicAction('restore', 'event:topic_restored', {
uid: req.user.uid,
ip: req.ip,
}, { tids: [topic.tid], cid: topic.cid });
}
}
async function deleteOrRestore(req, data, params) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
const postData = await posts.tools[params.command](req.user.uid, data.pid);
const results = await isMainAndLastPost(data.pid);
if (results.isMain && results.isLast) {
await deleteOrRestoreTopicOf(params.command, data.pid, req);
}
sockets.in('topic_' + postData.tid).emit(params.event, postData);
await events.log({
type: params.type,
uid: req.user.uid,
pid: data.pid,
tid: postData.tid,
ip: req.ip,
});
}

@ -1,16 +1,14 @@
'use strict'; 'use strict';
const posts = require('../../posts'); const posts = require('../../posts');
const topics = require('../../topics');
const flags = require('../../flags'); const flags = require('../../flags');
const events = require('../../events'); const events = require('../../events');
const websockets = require('../index');
const apiHelpers = require('../../api/helpers');
const privileges = require('../../privileges'); const privileges = require('../../privileges');
const plugins = require('../../plugins'); const plugins = require('../../plugins');
const social = require('../../social'); const social = require('../../social');
const user = require('../../user'); const user = require('../../user');
const utils = require('../../utils'); const utils = require('../../utils');
const api = require('../../api');
const sockets = require('..'); const sockets = require('..');
@ -65,45 +63,14 @@ module.exports = function (SocketPosts) {
SocketPosts.delete = async function (socket, data) { SocketPosts.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/state'); sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/state');
await api.posts.delete(socket, data);
await deleteOrRestore(socket, data, {
command: 'delete',
event: 'event:post_deleted',
type: 'post-delete',
});
}; };
SocketPosts.restore = async function (socket, data) { SocketPosts.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/state'); sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/state');
await api.posts.restore(socket, data);
await deleteOrRestore(socket, data, {
command: 'restore',
event: 'event:post_restored',
type: 'post-restore',
});
}; };
async function deleteOrRestore(socket, data, params) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
const postData = await posts.tools[params.command](socket.uid, data.pid);
const results = await isMainAndLastPost(data.pid);
if (results.isMain && results.isLast) {
await deleteOrRestoreTopicOf(params.command, data.pid, socket);
}
websockets.in('topic_' + postData.tid).emit(params.event, postData);
await events.log({
type: params.type,
uid: socket.uid,
pid: data.pid,
tid: postData.tid,
ip: socket.ip,
});
}
SocketPosts.deletePosts = async function (socket, data) { SocketPosts.deletePosts = async function (socket, data) {
await deletePurgePosts(socket, data, 'delete'); await deletePurgePosts(socket, data, 'delete');
}; };
@ -124,64 +91,9 @@ module.exports = function (SocketPosts) {
SocketPosts.purge = async function (socket, data) { SocketPosts.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid'); sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid');
await api.posts.purge(socket, data);
if (!data || !parseInt(data.pid, 10)) {
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]]');
}
const isMainAndLast = results.isMain && results.isLast;
const postData = await posts.getPostFields(data.pid, ['toPid', 'tid']);
postData.pid = data.pid;
const canPurge = await privileges.posts.canPurge(data.pid, socket.uid);
if (!canPurge) {
throw new Error('[[error:no-privileges]]');
}
require('../../posts/cache').del(data.pid);
await posts.purge(data.pid, socket.uid);
websockets.in('topic_' + postData.tid).emit('event:post_purged', postData);
const topicData = await topics.getTopicFields(postData.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 apiHelpers.doTopicAction('purge', 'event:topic_purged', socket, { tids: [postData.tid], cid: topicData.cid });
}
}; };
async function deleteOrRestoreTopicOf(command, pid, socket) {
const topic = await posts.getTopicFields(pid, ['tid', 'cid', 'deleted']);
if (command === 'delete' && !topic.deleted) {
await apiHelpers.doTopicAction('delete', 'event:topic_deleted', socket, { tids: [topic.tid], cid: topic.cid });
} else if (command === 'restore' && topic.deleted) {
await apiHelpers.doTopicAction('restore', 'event:topic_restored', socket, { tids: [topic.tid], cid: topic.cid });
}
}
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,
};
}
SocketPosts.changeOwner = async function (socket, data) { SocketPosts.changeOwner = async function (socket, data) {
if (!data || !Array.isArray(data.pids) || !data.toUid) { if (!data || !Array.isArray(data.pids) || !data.toUid) {
throw new Error('[[error:invalid-data]]'); throw new Error('[[error:invalid-data]]');

Loading…
Cancel
Save