From e8787944abdbcbf455ab832ddbe3e7b5422f3466 Mon Sep 17 00:00:00 2001 From: Victor Kuo <42633156+vck3000@users.noreply.github.com> Date: Fri, 3 Jan 2025 18:58:46 +1100 Subject: [PATCH] Percy ban (#728) --- assets/scripts/lobby/sockets/commands.js | 53 ------------------------ assets/scripts/lobby/sockets/mods.js | 48 +++++++++++++++++++++ src/modsadmins/percivals.ts | 6 ++- src/routes/mod.js | 29 ++++++++++++- src/routes/tests/mod.test.ts | 14 +++++++ src/sockets/commands/mod/mban.ts | 1 - src/sockets/commands/percival/index.ts | 2 + src/sockets/commands/percival/pban.ts | 18 ++++++++ src/views/lobby.ejs | 8 +++- 9 files changed, 120 insertions(+), 59 deletions(-) create mode 100644 assets/scripts/lobby/sockets/mods.js create mode 100644 src/routes/tests/mod.test.ts create mode 100644 src/sockets/commands/percival/pban.ts diff --git a/assets/scripts/lobby/sockets/commands.js b/assets/scripts/lobby/sockets/commands.js index 60eaf821e..87779b024 100644 --- a/assets/scripts/lobby/sockets/commands.js +++ b/assets/scripts/lobby/sockets/commands.js @@ -14,59 +14,6 @@ socket.on('adminCommands', (commands) => { let modCommands; socket.on('modCommands', (commands) => { - if (!modCommands) { - $('#modActionCloseButton').on('click', () => { - // Send request out. - var formElement = document.querySelector('#modactionform'); - var bodyFormData = new FormData(formElement); - - const banData = {}; - for (var [key, value] of bodyFormData.entries()) { - banData[key] = value; - } - - Swal.fire({ - title: 'Sending your request...', - onOpen: () => { - Swal.showLoading(); - - axios({ - method: 'POST', - url: '/mod/ban', - data: banData, - config: { headers: { 'Content-Type': 'application/json' } }, - }) - .then(function (response) { - //handle success - // console.log(response); - - $('#modModal').modal('hide'); - - // Clear the form for next input. - $('#modactionform')[0].reset(); - - Swal.close(); - Swal.fire({ - title: response.data, - type: 'success', - }); - }) - .catch(function (err) { - Swal.close(); - Swal.fire({ - title: err.response.data, - type: 'error', - }); - }); - }, - }); - - // console.log($("#modactionform").serializeArray()); - // const data = $('#modactionform').serializeArray(); - - // socket.emit('modAction', data); - }); - } modCommands = commands; }); diff --git a/assets/scripts/lobby/sockets/mods.js b/assets/scripts/lobby/sockets/mods.js new file mode 100644 index 000000000..c43da4d1a --- /dev/null +++ b/assets/scripts/lobby/sockets/mods.js @@ -0,0 +1,48 @@ +$(document).ready(function () { + $('#modActionCloseButton').on('click', () => { + // Send request out. + var formElement = document.querySelector('#modactionform'); + var bodyFormData = new FormData(formElement); + + const banData = {}; + for (var [key, value] of bodyFormData.entries()) { + banData[key] = value; + } + + Swal.fire({ + title: 'Sending your request...', + onOpen: () => { + Swal.showLoading(); + + axios({ + method: 'POST', + url: '/mod/ban', + data: banData, + config: { headers: { 'Content-Type': 'application/json' } }, + }) + .then(function(response) { + //handle success + // console.log(response); + + $('#modModal').modal('hide'); + + // Clear the form for next input. + $('#modactionform')[0].reset(); + + Swal.close(); + Swal.fire({ + title: response.data, + type: 'success', + }); + }) + .catch(function(err) { + Swal.close(); + Swal.fire({ + title: err.response.data, + type: 'error', + }); + }); + }, + }); + }); +}); \ No newline at end of file diff --git a/src/modsadmins/percivals.ts b/src/modsadmins/percivals.ts index 273021efe..a8c84fe57 100644 --- a/src/modsadmins/percivals.ts +++ b/src/modsadmins/percivals.ts @@ -2,8 +2,10 @@ // It is not the Percival role in Avalon gameplay. // all in lower case -export const percivalsArray: string[] = []; +export const percivalsArray: string[] = [ + 'percytest' +]; export function isPercival(username: string): boolean { return percivalsArray.includes(username.toLowerCase()); -} +} \ No newline at end of file diff --git a/src/routes/mod.js b/src/routes/mod.js index b7b014dcb..e9e4d18dc 100644 --- a/src/routes/mod.js +++ b/src/routes/mod.js @@ -19,6 +19,8 @@ import ModLogComponent from '../views/components/mod/mod_log'; import ReportLog from '../views/components/mod/report'; import { MongoClient } from 'mongodb'; import { SESSIONS_COLLECTION_NAME } from '../constants'; +import { isMod } from '../modsadmins/mods'; +import { isPercival } from '../modsadmins/percivals'; const router = new Router(); @@ -41,7 +43,20 @@ const requiredFields = [ 'descriptionByMod', ]; -router.post('/ban', isModMiddleware, async (req, res) => { +router.post('/ban', async (req, res) => { + if (!isMod(req.user.username) && !isPercival(req.user.username)) { + res.status(401); + res.send(`You are not a moderator or percival.`); + return; + } + + const userIsMod = isMod(req.user.username); + const userIsPercy = isPercival(req.user.username); + + if ((userIsMod ^ userIsPercy) !== 1) { + throw Error("Expected requesting user to either be a mod or a Percy."); + } + try { // Catch errors so that it's not shown to users. // Multiple checks: @@ -143,6 +158,18 @@ router.post('/ban', isModMiddleware, async (req, res) => { return; } + // Percies can only ban up to 24 hrs. No greater. + if (userIsPercy) { + const cutoff = new Date(); + cutoff.setDate(cutoff.getDate() + 1); + + if (whenRelease > cutoff) { + res.status(400); + res.send(`Percival roles cannot ban for more than 24 hours.`); + return; + } + } + // Create the data object const banData = { ipBan: diff --git a/src/routes/tests/mod.test.ts b/src/routes/tests/mod.test.ts new file mode 100644 index 000000000..46530f3be --- /dev/null +++ b/src/routes/tests/mod.test.ts @@ -0,0 +1,14 @@ +describe('TestDates', () => { + it('SetMonthBeyond11', () => { + const a = new Date(); + + a.setMonth(11); + console.log(a); + + a.setMonth(12); + console.log(a); + + a.setMonth(13); + console.log(a); + }); +}); diff --git a/src/sockets/commands/mod/mban.ts b/src/sockets/commands/mod/mban.ts index aae85106f..8d2b72dea 100644 --- a/src/sockets/commands/mod/mban.ts +++ b/src/sockets/commands/mod/mban.ts @@ -5,7 +5,6 @@ export const mban: Command = { command: 'mban', help: '/mban: Open the ban interface', run: async (data, senderSocket) => { - // TODO // @ts-ignore if (isMod(senderSocket.request.user.username)) { senderSocket.emit('openModModal'); diff --git a/src/sockets/commands/percival/index.ts b/src/sockets/commands/percival/index.ts index b58ec21cb..9aac5bcd2 100644 --- a/src/sockets/commands/percival/index.ts +++ b/src/sockets/commands/percival/index.ts @@ -3,6 +3,7 @@ import { p } from './p'; import { mdc } from '../mod/mdc'; import { mforcemove } from '../mod/mforcemove'; import { mclose } from '../mod/mclose'; +import { pban } from './pban'; // Percival commands are intended to be a strict subset of mod commands. @@ -39,6 +40,7 @@ const pclose = convertModCommandToPercivalCommand(mclose); export const percivalCommands: Commands = { [p.command]: p, + [pban.command]: pban, [pdc.command]: pdc, [pforcemove.command]: pforcemove, [pclose.command]: pclose, diff --git a/src/sockets/commands/percival/pban.ts b/src/sockets/commands/percival/pban.ts new file mode 100644 index 000000000..29783f3f9 --- /dev/null +++ b/src/sockets/commands/percival/pban.ts @@ -0,0 +1,18 @@ +import { Command } from '../types'; +import { isPercival } from '../../../modsadmins/percivals'; + +export const pban: Command = { + command: 'pban', + help: '/pban: Open the ban interface', + run: async (data, senderSocket) => { + // @ts-ignore + if (isPercival(senderSocket.request.user.username)) { + senderSocket.emit('openModModal'); + + senderSocket.emit('messageCommandReturnStr', { + message: 'May your judgement bring peace to all!', + classStr: 'server-text', + }); + } + }, +}; diff --git a/src/views/lobby.ejs b/src/views/lobby.ejs index 6d0c19085..f29ced7b9 100644 --- a/src/views/lobby.ejs +++ b/src/views/lobby.ejs @@ -1339,11 +1339,15 @@ > +