From bd1cb2d4f9abcd99e1414a8c3316ec9947b02d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 9 Dec 2021 19:58:40 -0500 Subject: [PATCH] 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') --- public/src/client/topic/delete-posts.js | 27 +++-- src/socket.io/posts/tools.js | 36 ------- test/posts.js | 132 ++++++++---------------- 3 files changed, 56 insertions(+), 139 deletions(-) diff --git a/public/src/client/topic/delete-posts.js b/public/src/client/topic/delete-posts.js index 1c027a29ed..93d3e0259e 100644 --- a/public/src/client/topic/delete-posts.js +++ b/public/src/client/topic/delete-posts.js @@ -1,7 +1,8 @@ 'use strict'; - -define('forum/topic/delete-posts', ['postSelect', 'alerts'], function (postSelect, alerts) { +define('forum/topic/delete-posts', [ + 'postSelect', 'alerts', 'api', +], function (postSelect, alerts, api) { const DeletePosts = {}; let modal; let deleteBtn; @@ -34,10 +35,10 @@ define('forum/topic/delete-posts', ['postSelect', 'alerts'], function (postSelec showPostsSelected(); deleteBtn.on('click', function () { - deletePosts(deleteBtn, 'posts.deletePosts'); + deletePosts(deleteBtn, pid => `/posts/${pid}/state`); }); 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); - socket.emit(command, { - pids: postSelect.pids, - }, function (err) { - btn.removeAttr('disabled'); - if (err) { - return alerts.error(err); - } - - closeModal(); - }); + Promise.all(postSelect.pids.map(pid => api.delete(route(pid), {}))) + .then(closeModal) + .catch(alerts.error) + .finally(() => { + btn.removeAttr('disabled'); + }); } function showPostsSelected() { diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js index e9908a919d..3aa37d7a57 100644 --- a/src/socket.io/posts/tools.js +++ b/src/socket.io/posts/tools.js @@ -9,9 +9,6 @@ const plugins = require('../../plugins'); const social = require('../../social'); const user = require('../../user'); const utils = require('../../utils'); -const api = require('../../api'); - -const sockets = require('..'); module.exports = function (SocketPosts) { SocketPosts.loadPostTools = async function (socket, data) { @@ -68,39 +65,6 @@ module.exports = function (SocketPosts) { 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) { if (!data || !Array.isArray(data.pids) || !data.toUid) { throw new Error('[[error:invalid-data]]'); diff --git a/test/posts.js b/test/posts.js index cbfcddfc4b..a4d865cd55 100644 --- a/test/posts.js +++ b/test/posts.js @@ -308,55 +308,48 @@ describe('Post\'s', () => { }); describe('delete/restore/purge', () => { - function createTopicWithReply(callback) { - topics.post({ + async function createTopicWithReply() { + const topicPostData = await topics.post({ uid: voterUid, cid: cid, title: 'topic 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 mainPid; let replyPid; - before((done) => { - createTopicWithReply((topicPostData, replyData) => { - tid = topicPostData.topicData.tid; - mainPid = topicPostData.postData.pid; - replyPid = replyData.pid; - privileges.categories.give(['groups:purge'], cid, 'registered-users', done); - }); + before(async () => { + const [topicPostData, replyData] = await createTopicWithReply(); + tid = topicPostData.topicData.tid; + mainPid = topicPostData.postData.pid; + replyPid = replyData.pid; + await privileges.categories.give(['groups:purge'], cid, 'registered-users'); }); - it('should error with invalid data', (done) => { - socketPosts.delete({ uid: voterUid }, null, (err) => { - assert.equal(err.message, '[[error:invalid-data]]'); - done(); - }); + it('should error with invalid data', async () => { + try { + await apiPosts.delete({ uid: voterUid }, null); + } catch (err) { + return assert.equal(err.message, '[[error:invalid-data]]'); + } + assert(false); }); - it('should delete a post', (done) => { - socketPosts.delete({ uid: voterUid }, { pid: replyPid, tid: tid }, (err) => { - assert.ifError(err); - posts.getPostField(replyPid, 'deleted', (err, isDeleted) => { - assert.ifError(err); - assert.strictEqual(isDeleted, 1); - done(); - }); - }); + it('should delete a post', async () => { + await apiPosts.delete({ uid: voterUid }, { pid: replyPid, tid: tid }); + const isDeleted = await posts.getPostField(replyPid, 'deleted'); + assert.strictEqual(isDeleted, 1); }); 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); }); - it('should restore a post', (done) => { - socketPosts.restore({ uid: voterUid }, { pid: replyPid, tid: tid }, (err) => { - assert.ifError(err); - posts.getPostField(replyPid, 'deleted', (err, isDeleted) => { - 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 restore a post', async () => { + await apiPosts.restore({ uid: voterUid }, { pid: replyPid, tid: tid }); + const isDeleted = await posts.getPostField(replyPid, 'deleted'); + assert.strictEqual(isDeleted, 0); }); - it('should delete topic if last main post is deleted', (done) => { - topics.post({ uid: voterUid, cid: cid, title: 'test topic', content: 'test topic' }, (err, data) => { - assert.ifError(err); - socketPosts.deletePosts({ uid: globalModUid }, { pids: [data.postData.pid] }, (err) => { - assert.ifError(err); - topics.getTopicField(data.topicData.tid, 'deleted', (err, deleted) => { - assert.ifError(err); - assert.strictEqual(deleted, 1); - done(); - }); - }); - }); + it('should delete topic if last main post is deleted', async () => { + const data = await topics.post({ uid: voterUid, cid: cid, title: 'test topic', content: 'test topic' }); + await apiPosts.delete({ uid: globalModUid }, { pid: data.postData.pid }); + const deleted = await topics.getTopicField(data.topicData.tid, 'deleted'); + assert.strictEqual(deleted, 1); }); - it('should purge posts and purge topic', (done) => { - createTopicWithReply((topicPostData, replyData) => { - socketPosts.purgePosts({ uid: voterUid }, { - pids: [replyData.pid, topicPostData.postData.pid], - tid: topicPostData.topicData.tid, - }, (err) => { - assert.ifError(err); - posts.exists(`post:${replyData.pid}`, (err, exists) => { - assert.ifError(err); - assert.equal(exists, false); - topics.exists(topicPostData.topicData.tid, (err, exists) => { - assert.ifError(err); - assert(!exists); - done(); - }); - }); - }); - }); + it('should purge posts and purge topic', async () => { + const [topicPostData, replyData] = await createTopicWithReply(); + await apiPosts.purge({ uid: voterUid }, { pid: replyData.pid }); + await apiPosts.purge({ uid: voterUid }, { pid: topicPostData.postData.pid }); + const pidExists = await posts.exists(replyData.pid); + assert.strictEqual(pidExists, false); + const tidExists = await topics.exists(topicPostData.topicData.tid); + assert.strictEqual(tidExists, false); }); });