From 23175110a29640e6fa052db1079bfedb34a61055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 30 Apr 2022 19:07:49 -0400 Subject: [PATCH] feat: post queue bulk actions closes #10520, fix #10555, --- public/language/en-GB/post-queue.json | 11 +++++- public/src/client/post-queue.js | 52 +++++++++++++++++++++++---- src/controllers/mods.js | 1 + 3 files changed, 56 insertions(+), 8 deletions(-) diff --git a/public/language/en-GB/post-queue.json b/public/language/en-GB/post-queue.json index 85bda81ac3..952342286f 100644 --- a/public/language/en-GB/post-queue.json +++ b/public/language/en-GB/post-queue.json @@ -18,5 +18,14 @@ "remove": "Remove", "notify": "Notify", "notify-user": "Notify User", - "confirm-reject": "Do you want to reject this post?" + "confirm-reject": "Do you want to reject this post?", + "bulk-actions": "Bulk Actions", + "accept-all": "Accept All", + "accept-selected": "Accept Selected", + "reject-all": "Reject All", + "reject-all-confirm": "Do you want to reject all posts?", + "reject-selected": "Reject Selected", + "reject-selected-confirm": "Do you want to reject selected posts?", + "bulk-accept-success": "%1 posts accepted", + "bulk-reject-success": "%1 posts rejected" } \ No newline at end of file diff --git a/public/src/client/post-queue.js b/public/src/client/post-queue.js index f5c471ca30..4d6ce858ff 100644 --- a/public/src/client/post-queue.js +++ b/public/src/client/post-queue.js @@ -13,6 +13,8 @@ define('forum/post-queue', [ privilege: 'moderate', }); + handleBulkActions(); + $('.posts-list').on('click', '[data-action]', async function () { function getMessage() { return new Promise((resolve) => { @@ -33,17 +35,13 @@ define('forum/post-queue', [ }); }); } - function confirmReject() { - return new Promise((resolve) => { - bootbox.confirm('[[post-queue:confirm-reject]]', resolve); - }); - } + const parent = $(this).parents('[data-id]'); const action = $(this).attr('data-action'); const id = parent.attr('data-id'); const listContainer = parent.get(0).parentNode; - if ((!['accept', 'reject', 'notify'].includes(action)) || (action === 'reject' && !await confirmReject())) { + if ((!['accept', 'reject', 'notify'].includes(action)) || (action === 'reject' && !await confirmReject('[[post-queue:confirm-reject]]'))) { return; } @@ -59,7 +57,11 @@ define('forum/post-queue', [ } if (listContainer.childElementCount === 0) { - ajaxify.refresh(); + if (ajaxify.data.singlePost) { + ajaxify.go('/post-queue' + window.location.search); + } else { + ajaxify.refresh(); + } } }); return false; @@ -102,6 +104,12 @@ define('forum/post-queue', [ $('[component="post/content"] img:not(.not-responsive)').addClass('img-responsive'); }; + function confirmReject(msg) { + return new Promise((resolve) => { + bootbox.confirm(msg, resolve); + }); + } + function handleContentEdit(displayClass, editableClass, inputSelector) { $('.posts-list').on('click', displayClass, function () { const el = $(this); @@ -143,5 +151,35 @@ define('forum/post-queue', [ }); } + function handleBulkActions() { + $('[component="post-queue/bulk-actions"]').on('click', '[data-action]', async function () { + const bulkAction = $(this).attr('data-action'); + let queueEls = $('.posts-list [data-id]'); + if (bulkAction === 'accept-selected' || bulkAction === 'reject-selected') { + queueEls = queueEls.filter( + (i, el) => $(el).find('input[type="checkbox"]').is(':checked') + ); + } + const ids = queueEls.map((i, el) => $(el).attr('data-id')).get(); + const showConfirm = bulkAction === 'reject-all' || bulkAction === 'reject-selected'; + if (!ids.length || (showConfirm && !(await confirmReject(`[[post-queue:${bulkAction}-confirm]]`)))) { + return; + } + const action = bulkAction.split('-')[0]; + const promises = ids.map(id => socket.emit('posts.' + action, { id: id })); + + Promise.allSettled(promises).then(function (results) { + const fulfilled = results.filter(res => res.status === 'fulfilled').length; + const errors = results.filter(res => res.status === 'rejected'); + if (fulfilled) { + alerts.success(`[[post-queue:bulk-${action}-success, ${fulfilled}]]`); + ajaxify.refresh(); + } + + errors.forEach(res => alerts.error(res.reason)); + }); + }); + } + return PostQueue; }); diff --git a/src/controllers/mods.js b/src/controllers/mods.js index 14ce492303..f80e9230f3 100644 --- a/src/controllers/mods.js +++ b/src/controllers/mods.js @@ -194,5 +194,6 @@ modsController.postQueue = async function (req, res, next) { allCategoriesUrl: `post-queue${helpers.buildQueryString(req.query, 'cid', '')}`, pagination: pagination.create(page, pageCount), breadcrumbs: helpers.buildBreadcrumbs(crumbs), + singlePost: !!id, }); };