refactor: post-queue frontend logic

isekai-main
Julian Lam 2 years ago
parent 4305c0a123
commit ddcdaacc94

@ -100,7 +100,7 @@
"nodebb-plugin-ntfy": "1.0.15",
"nodebb-plugin-spam-be-gone": "2.0.6",
"nodebb-rewards-essentials": "0.2.2",
"nodebb-theme-harmony": "1.0.0-beta.80",
"nodebb-theme-harmony": "1.0.0-beta.81",
"nodebb-theme-lavender": "7.0.9",
"nodebb-theme-peace": "2.0.19",
"nodebb-theme-persona": "13.0.51",

@ -6,6 +6,7 @@
"enabling-help": "To enable this feature, go to <a href=\"%1\">Settings &rarr; Post &rarr; Post Queue</a> and enable <strong>Post Queue</strong>.",
"back-to-list": "Back to Post Queue",
"user": "User",
"when": "When",
"category": "Category",
"title": "Title",
"content": "Content",

@ -3,7 +3,11 @@
define('forum/post-queue', [
'categoryFilter', 'categorySelector', 'api', 'alerts', 'bootbox',
], function (categoryFilter, categorySelector, api, alerts, bootbox) {
'accounts/moderate', 'accounts/delete',
], function (
categoryFilter, categorySelector, api, alerts, bootbox,
AccountModerate, AccountsDelete
) {
const PostQueue = {};
PostQueue.init = function () {
@ -13,61 +17,8 @@ define('forum/post-queue', [
privilege: 'moderate',
});
handleActions();
handleBulkActions();
$('.posts-list').on('click', '[data-action]', async function () {
function getMessage() {
return new Promise((resolve) => {
const modal = bootbox.dialog({
title: '[[post-queue:notify-user]]',
message: '<textarea class="form-control"></textarea>',
buttons: {
OK: {
label: '[[modules:bootbox.send]]',
callback: function () {
const val = modal.find('textarea').val();
if (val) {
resolve(val);
}
},
},
},
});
});
}
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(ajaxify.data.canAccept ? '[[post-queue:confirm-reject]]' : '[[post-queue:confirm-remove]]'))) {
return;
}
socket.emit('posts.' + action, {
id: id,
message: action === 'notify' ? await getMessage() : undefined,
}, function (err) {
if (err) {
return alerts.error(err);
}
if (action === 'accept' || action === 'reject') {
parent.remove();
}
if (listContainer.childElementCount === 0) {
if (ajaxify.data.singlePost) {
ajaxify.go('/post-queue' + window.location.search);
} else {
ajaxify.refresh();
}
}
});
return false;
});
handleContentEdit('.post-content', '.post-content-editable', 'textarea');
handleContentEdit('.topic-title', '.topic-title-editable', 'input');
@ -152,6 +103,106 @@ define('forum/post-queue', [
});
}
function handleActions() {
const listEl = document.querySelector('.posts-list');
if (listEl) {
listEl.addEventListener('click', (e) => {
const subselector = e.target.closest('[data-action]');
if (subselector) {
const action = subselector.getAttribute('data-action');
const uid = subselector.closest('[data-uid]').getAttribute('data-uid');
switch (action) {
case 'ban':
AccountModerate.banAccount(uid, ajaxify.refresh);
break;
case 'unban':
AccountModerate.unbanAccount(uid);
break;
case 'mute':
AccountModerate.muteAccount(uid, ajaxify.refresh);
break;
case 'unmute':
AccountModerate.unmuteAccount(uid);
break;
case 'delete-account':
AccountsDelete.account(uid, ajaxify.refresh);
break;
case 'delete-content':
AccountsDelete.content(uid, ajaxify.refresh);
break;
case 'delete-all':
AccountsDelete.purge(uid, ajaxify.refresh);
break;
default:
handleQueueActions.call(e.target);
break;
}
}
});
}
}
async function handleQueueActions() {
function getMessage() {
return new Promise((resolve) => {
const modal = bootbox.dialog({
title: '[[post-queue:notify-user]]',
message: '<textarea class="form-control"></textarea>',
buttons: {
OK: {
label: '[[modules:bootbox.send]]',
callback: function () {
const val = modal.find('textarea').val();
if (val) {
resolve(val);
}
},
},
},
});
});
}
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(ajaxify.data.canAccept ? '[[post-queue:confirm-reject]]' : '[[post-queue:confirm-remove]]'))) {
return;
}
socket.emit('posts.' + action, {
id: id,
message: action === 'notify' ? await getMessage() : undefined,
}, function (err) {
if (err) {
return alerts.error(err);
}
if (action === 'accept' || action === 'reject') {
parent.remove();
}
if (listContainer.childElementCount === 0) {
if (ajaxify.data.singlePost) {
ajaxify.go('/post-queue' + window.location.search);
} else {
ajaxify.refresh();
}
}
});
return false;
}
function handleBulkActions() {
$('[component="post-queue/bulk-actions"]').on('click', '[data-action]', async function () {
const bulkAction = $(this).attr('data-action');

@ -203,12 +203,14 @@ modsController.postQueue = async function (req, res, next) {
const postsPerPage = 20;
let postData = await posts.getQueuedPosts({ id: id });
const [isAdmin, isGlobalMod, moderatedCids, categoriesData] = await Promise.all([
let [isAdmin, isGlobalMod, moderatedCids, categoriesData, _privileges] = await Promise.all([
user.isAdministrator(req.uid),
user.isGlobalModerator(req.uid),
user.getModeratedCids(req.uid),
helpers.getSelectedCategory(cid),
Promise.all(['global', 'admin'].map(async type => privileges[type].get(req.uid))),
]);
_privileges = { ..._privileges[0], ..._privileges[1] };
postData = postData
.filter(p => p &&
@ -245,5 +247,6 @@ modsController.postQueue = async function (req, res, next) {
breadcrumbs: helpers.buildBreadcrumbs(crumbs),
enabled: meta.config.postQueue,
singlePost: !!id,
privileges: _privileges,
});
};

Loading…
Cancel
Save