breaking: remove socket.emit('posts.delete')

remove socket.emit('posts.restore')
remove socket.emit('posts.purge')
remove socket.emit('posts.deletePosts')
remove socket.emit('posts.purgePosts')
isekai-main
Barış Soner Uşaklı 3 years ago
parent ec1d5e38da
commit bd1cb2d4f9

@ -1,7 +1,8 @@
'use strict'; 'use strict';
define('forum/topic/delete-posts', [
define('forum/topic/delete-posts', ['postSelect', 'alerts'], function (postSelect, alerts) { 'postSelect', 'alerts', 'api',
], function (postSelect, alerts, api) {
const DeletePosts = {}; const DeletePosts = {};
let modal; let modal;
let deleteBtn; let deleteBtn;
@ -34,10 +35,10 @@ define('forum/topic/delete-posts', ['postSelect', 'alerts'], function (postSelec
showPostsSelected(); showPostsSelected();
deleteBtn.on('click', function () { deleteBtn.on('click', function () {
deletePosts(deleteBtn, 'posts.deletePosts'); deletePosts(deleteBtn, pid => `/posts/${pid}/state`);
}); });
purgeBtn.on('click', function () { purgeBtn.on('click', function () {
deletePosts(purgeBtn, 'posts.purgePosts'); deletePosts(purgeBtn, pid => `/posts/${pid}`);
}); });
}); });
}; };
@ -49,18 +50,14 @@ define('forum/topic/delete-posts', ['postSelect', 'alerts'], function (postSelec
} }
} }
function deletePosts(btn, command) { function deletePosts(btn, route) {
btn.attr('disabled', true); btn.attr('disabled', true);
socket.emit(command, { Promise.all(postSelect.pids.map(pid => api.delete(route(pid), {})))
pids: postSelect.pids, .then(closeModal)
}, function (err) { .catch(alerts.error)
btn.removeAttr('disabled'); .finally(() => {
if (err) { btn.removeAttr('disabled');
return alerts.error(err); });
}
closeModal();
});
} }
function showPostsSelected() { function showPostsSelected() {

@ -9,9 +9,6 @@ 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('..');
module.exports = function (SocketPosts) { module.exports = function (SocketPosts) {
SocketPosts.loadPostTools = async function (socket, data) { SocketPosts.loadPostTools = async function (socket, data) {
@ -68,39 +65,6 @@ module.exports = function (SocketPosts) {
return results; return results;
}; };
SocketPosts.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid/state');
await api.posts.delete(socket, data);
};
SocketPosts.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/state');
await api.posts.restore(socket, data);
};
SocketPosts.deletePosts = async function (socket, data) {
await deletePurgePosts(socket, data, 'delete');
};
SocketPosts.purgePosts = async function (socket, data) {
await deletePurgePosts(socket, data, 'purge');
};
async function deletePurgePosts(socket, data, command) {
if (!data || !Array.isArray(data.pids)) {
throw new Error('[[error:invalid-data]]');
}
for (const pid of data.pids) {
/* eslint-disable no-await-in-loop */
await SocketPosts[command](socket, { pid: pid });
}
}
SocketPosts.purge = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/posts/:pid');
await api.posts.purge(socket, data);
};
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]]');

@ -308,55 +308,48 @@ describe('Post\'s', () => {
}); });
describe('delete/restore/purge', () => { describe('delete/restore/purge', () => {
function createTopicWithReply(callback) { async function createTopicWithReply() {
topics.post({ const topicPostData = await topics.post({
uid: voterUid, uid: voterUid,
cid: cid, cid: cid,
title: 'topic to delete/restore/purge', title: 'topic to delete/restore/purge',
content: 'A post to delete/restore/purge', content: 'A post to delete/restore/purge',
}, (err, topicPostData) => {
assert.ifError(err);
topics.reply({
uid: voterUid,
tid: topicPostData.topicData.tid,
timestamp: Date.now(),
content: 'A post to delete/restore and purge',
}, (err, replyData) => {
assert.ifError(err);
callback(topicPostData, replyData);
});
}); });
const replyData = await topics.reply({
uid: voterUid,
tid: topicPostData.topicData.tid,
timestamp: Date.now(),
content: 'A post to delete/restore and purge',
});
return [topicPostData, replyData];
} }
let tid; let tid;
let mainPid; let mainPid;
let replyPid; let replyPid;
before((done) => { before(async () => {
createTopicWithReply((topicPostData, replyData) => { const [topicPostData, replyData] = await createTopicWithReply();
tid = topicPostData.topicData.tid; tid = topicPostData.topicData.tid;
mainPid = topicPostData.postData.pid; mainPid = topicPostData.postData.pid;
replyPid = replyData.pid; replyPid = replyData.pid;
privileges.categories.give(['groups:purge'], cid, 'registered-users', done); await privileges.categories.give(['groups:purge'], cid, 'registered-users');
});
}); });
it('should error with invalid data', (done) => { it('should error with invalid data', async () => {
socketPosts.delete({ uid: voterUid }, null, (err) => { try {
assert.equal(err.message, '[[error:invalid-data]]'); await apiPosts.delete({ uid: voterUid }, null);
done(); } catch (err) {
}); return assert.equal(err.message, '[[error:invalid-data]]');
}
assert(false);
}); });
it('should delete a post', (done) => { it('should delete a post', async () => {
socketPosts.delete({ uid: voterUid }, { pid: replyPid, tid: tid }, (err) => { await apiPosts.delete({ uid: voterUid }, { pid: replyPid, tid: tid });
assert.ifError(err); const isDeleted = await posts.getPostField(replyPid, 'deleted');
posts.getPostField(replyPid, 'deleted', (err, isDeleted) => { assert.strictEqual(isDeleted, 1);
assert.ifError(err);
assert.strictEqual(isDeleted, 1);
done();
});
});
}); });
it('should not see post content if global mod does not have posts:view_deleted privilege', (done) => { it('should not see post content if global mod does not have posts:view_deleted privilege', (done) => {
@ -383,64 +376,27 @@ describe('Post\'s', () => {
], done); ], done);
}); });
it('should restore a post', (done) => { it('should restore a post', async () => {
socketPosts.restore({ uid: voterUid }, { pid: replyPid, tid: tid }, (err) => { await apiPosts.restore({ uid: voterUid }, { pid: replyPid, tid: tid });
assert.ifError(err); const isDeleted = await posts.getPostField(replyPid, 'deleted');
posts.getPostField(replyPid, 'deleted', (err, isDeleted) => { assert.strictEqual(isDeleted, 0);
assert.ifError(err);
assert.strictEqual(isDeleted, 0);
done();
});
});
});
it('should delete posts', (done) => {
socketPosts.deletePosts({ uid: globalModUid }, { pids: [replyPid, mainPid] }, (err) => {
assert.ifError(err);
posts.getPostField(replyPid, 'deleted', (err, deleted) => {
assert.ifError(err);
assert.strictEqual(deleted, 1);
posts.getPostField(mainPid, 'deleted', (err, deleted) => {
assert.ifError(err);
assert.strictEqual(deleted, 1);
done();
});
});
});
}); });
it('should delete topic if last main post is deleted', (done) => { it('should delete topic if last main post is deleted', async () => {
topics.post({ uid: voterUid, cid: cid, title: 'test topic', content: 'test topic' }, (err, data) => { const data = await topics.post({ uid: voterUid, cid: cid, title: 'test topic', content: 'test topic' });
assert.ifError(err); await apiPosts.delete({ uid: globalModUid }, { pid: data.postData.pid });
socketPosts.deletePosts({ uid: globalModUid }, { pids: [data.postData.pid] }, (err) => { const deleted = await topics.getTopicField(data.topicData.tid, 'deleted');
assert.ifError(err); assert.strictEqual(deleted, 1);
topics.getTopicField(data.topicData.tid, 'deleted', (err, deleted) => {
assert.ifError(err);
assert.strictEqual(deleted, 1);
done();
});
});
});
}); });
it('should purge posts and purge topic', (done) => { it('should purge posts and purge topic', async () => {
createTopicWithReply((topicPostData, replyData) => { const [topicPostData, replyData] = await createTopicWithReply();
socketPosts.purgePosts({ uid: voterUid }, { await apiPosts.purge({ uid: voterUid }, { pid: replyData.pid });
pids: [replyData.pid, topicPostData.postData.pid], await apiPosts.purge({ uid: voterUid }, { pid: topicPostData.postData.pid });
tid: topicPostData.topicData.tid, const pidExists = await posts.exists(replyData.pid);
}, (err) => { assert.strictEqual(pidExists, false);
assert.ifError(err); const tidExists = await topics.exists(topicPostData.topicData.tid);
posts.exists(`post:${replyData.pid}`, (err, exists) => { assert.strictEqual(tidExists, false);
assert.ifError(err);
assert.equal(exists, false);
topics.exists(topicPostData.topicData.tid, (err, exists) => {
assert.ifError(err);
assert(!exists);
done();
});
});
});
});
}); });
}); });

Loading…
Cancel
Save