feat: Added unban, mute/unmute, kick to mod web tools (#17)

* fix: Updated ban date check logic

* chore: Added all backend endpoints for complete v1 mod web tools

* feat: Added unban, mute/unmute, kick to mod tools
This commit is contained in:
Cyperita authored and GitHub committed 2025-02-04 23:37:11 -05:00
1 parent a18ca4eb5e
commit 740a68d3a9
6 files changed
+295 -25

No files matched your search

+4 -2
View File
@@ -23,10 +23,12 @@
background-color: rgba(0, 0, 0, .65);
}
#modal-ban-player .modal-ban-player-time-container > br {
#modal-ban-player .modal-ban-player-time-container > br,
#modal-mute-player .modal-mute-player-time-container > br {
margin-bottom: 1rem;
}
#modal-ban-player > form > div:not(:last-child) {
#modal-ban-player > form > div:not(:last-child),
#modal-mute-player > form > div:not(:last-child) {
margin-bottom: 1rem;
}
+115 -4
View File
@@ -143,7 +143,7 @@ export default async function (app: FastifyInstance) {
app.post('/ban/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send();
return res.status(401).send({ error: 'Unauthorized' });
}
const { id } = req.params;
@@ -153,20 +153,130 @@ export default async function (app: FastifyInstance) {
return res.status(400).send({ error: `Missing 'banned_until' in body` });
}
const account = db.selectFrom('account')
.where('id', '=', id)
.selectAll()
.executeTakeFirst();
if (!account) {
return res.status(400).send({ error: `Account with ID '${id}' does not exist` });
}
const bannedDate = new Date(banned_until);
const isInvalidDate = isNaN(bannedDate.getTime()) || bannedDate.getTime() - (new Date()).getTime() < 0;
await db.updateTable('account')
.set({ banned_until: isInvalidDate ? null : toDbDate(banned_until) })
.set({ banned_until: isInvalidDate ? null : toDbDate(banned_until), logged_in: 0 })
.where('id', '=', id)
.execute();
return res.status(200).send();
return res.status(200).send({ success: true });
});
app.post('/unban/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send({ error: 'Unauthorized' });
}
const { id } = req.params;
const account = db.selectFrom('account')
.where('id', '=', id)
.selectAll()
.executeTakeFirst();
if (!account) {
return res.status(400).send({ error: `Account with ID '${id}' does not exist` });
}
await db.updateTable('account')
.set({ banned_until: null, logged_in: 0 })
.where('id', '=', id)
.execute();
return res.status(200).send({ success: true });
});
app.post('/mute/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send();
}
const { id } = req.params;
const { muted_until } = req.body;
if (!muted_until) {
return res.status(400).send({ error: `Missing 'muted_until' in body` });
}
const account = db.selectFrom('account')
.where('id', '=', id)
.selectAll()
.executeTakeFirst();
if (!account) {
return res.status(400).send({ error: `Account with ID '${id}' does not exist` });
}
const mutedDate = new Date(muted_until);
const isInvalidDate = isNaN(mutedDate.getTime()) || mutedDate.getTime() - (new Date()).getTime() < 0;
await db.updateTable('account')
.set({ muted_until: isInvalidDate ? null : toDbDate(muted_until) })
.where('id', '=', id)
.execute();
return res.status(200).send({ success: true });
});
app.post('/unmute/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send();
}
const { id } = req.params;
const account = db.selectFrom('account')
.where('id', '=', id)
.selectAll()
.executeTakeFirst();
if (!account) {
return res.status(400).send({ error: `Account with ID '${id}' does not exist` });
}
await db.updateTable('account')
.set({ muted_until: null, logged_in: 0 })
.where('id', '=', id)
.execute();
return res.status(200).send({ success: true });
});
app.post('/kick/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send();
}
const { id } = req.params;
const account = db.selectFrom('account')
.where('id', '=', id)
.selectAll()
.executeTakeFirst();
if (!account) {
return res.status(400).send({ error: `Account with ID '${id}' does not exist` });
}
await db.updateTable('account')
.set({ logged_in: 0 })
.where('id', '=', id)
.execute();
return res.status(200).send({ success: true });
});
app.post('/change-name/:id', async (req: any, res: any) => {
if (!req.session.account || req.session.account.staffmodlevel < 1) {
return res.status(401).send({ error: 'Unauthorized (refresh page?)' });
return res.status(401).send({ error: 'Unauthorized' });
}
const { id } = req.params;
@@ -208,6 +318,7 @@ export default async function (app: FastifyInstance) {
let updatePlayerModel: any = { username: safeNewName };
if (unban === true) {
updatePlayerModel.banned_until = null;
updatePlayerModel.logged_in = 0;
}
await db.updateTable('account')
-3
View File
@@ -6,9 +6,6 @@
<h3>Please select <span id="modal-ban-player-username" style="color:maroon">$USERNAME</span>'s ban length:</h3>
<div class="modal-ban-player-time-container">
<select id="modal-ban-player-time-select" value="1">
<% if (account.banned_until) { %>
<option value="0">Unban</option>
<% } %>
<option value="1">1 Day</option>
<option value="3">3 Days</option>
<option value="7">7 Days</option>
+25
View File
@@ -0,0 +1,25 @@
<dialog id="modal-mute-player" class="modal focused-modal">
<div>
<h2>Are You Sure?</h2>
</div>
<form method="post">
<h3>Please select <span id="modal-mute-player-username" style="color:maroon">$USERNAME</span>'s mute length:</h3>
<div class="modal-mute-player-time-container">
<select id="modal-mute-player-time-select" value="1">
<option value="1">1 Day</option>
<option value="3">3 Days</option>
<option value="7">7 Days</option>
<option value="14">14 Days</option>
<option value="36500">Permanent</option>
<option value="-1">Custom...</option>
</select>
<br>
<input class="hidden" id="modal-mute-player-time-custom" type="number" placeholder="# of days...">
</div>
<div class="row">
<%- include('stones-button.ejs', { id: 'modal-cancel-mute-btn', text: 'Mercy' }); %>
<span style="width:1rem"></span>
<%- include('stones-button.ejs', { id: 'modal-mute-player-btn', text: 'Mute', shiny: true }); %>
</div>
</form>
</dialog>
+143 -10
View File
@@ -1,7 +1,8 @@
<script>
const banButton = document.getElementById('ban-btn'),
muteButton = document.getElementById('mute-btn'),
changeUsernameButton = document.getElementById('name-change-btn');
changeUsernameButton = document.getElementById('name-change-btn'),
kickButton = document.getElementById('kick-btn');
const banModal = document.getElementById('modal-ban-player'),
banModalUsername = document.getElementById('modal-ban-player-username'),
@@ -17,6 +18,13 @@
changeUsernameTitle = document.getElementById('modal-change-username-span'),
changeUsernameInput = document.getElementById('modal-change-username-input');
const muteModal = document.getElementById('modal-mute-player'),
muteModalUsername = document.getElementById('modal-mute-player-username'),
muteModalTimePeriodSelect = document.getElementById('modal-mute-player-time-select'),
muteModalCustomTimePeriod = document.getElementById('modal-mute-player-time-custom'),
muteModalMuteButton = document.getElementById('modal-mute-player-btn'),
muteModalMercyButton = document.getElementById('modal-cancel-mute-btn');
function addDaysToCurrentDate(days) {
const currentDate = new Date();
@@ -29,6 +37,8 @@
function resetModalInputs() {
banModalTimePeriodSelect.value = 1;
banModalCustomTimePeriod.value = "";
muteModalTimePeriodSelect.value = 1;
muteModalCustomTimePeriod.value = "";
changeUsernameInput.value = "";
}
@@ -52,13 +62,15 @@
headers: {
'Content-Type': 'application/json'
}
}).then(res => {
if (res.status == 200) {
})
.then(res => res.json())
.then(json => {
if (json.success) {
banModal.close();
alert(`Banned ${account.username} for ${numberOfDays} day(s).`);
window.location.reload();
} else {
alert('Something went wrong. Please signout and back in, then try again.');
alert(json.error);
}
}).catch(err => {
alert(`An unknown error occurred. Please report this issue.\n\n${err}`);
@@ -67,8 +79,75 @@
}
}
async function muteUser() {
alert('Not yet implemented! Please try again on another date.');
async function submitUnbanUserRequest() {
if (confirm(`Are you sure you want to unban '${account.username}'?`)) {
fetch(`/mod/unban/${account.id}`, {
method: 'POST',
})
.then(res => res.json())
.then(json => {
if (json.success) {
alert(`Successfully unbanned '${account.username}'`);
location.reload();
} else {
alert(json.error);
}
});
}
}
async function submitMuteUserRequest() {
if (muteModalTimePeriodSelect.value === -1) {
if (muteModalCustomTimePeriod.value == "" || muteModalCustomTimePeriod.value == null) {
alert('Please enter a new period of time:');
return;
}
}
let numberOfDays = muteModalTimePeriodSelect.value == -1 ? muteModalCustomTimePeriod.value : muteModalTimePeriodSelect.value;
let muteDate = addDaysToCurrentDate(numberOfDays);
if (confirm(`Are you SURE you want to mute '${account.username}' for ${numberOfDays} day(s)?`)) {
fetch(`/mod/mute/${account.id}`, {
method: 'POST',
body: JSON.stringify({
muted_until: muteDate.toISOString()
}),
headers: {
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then(json => {
if (json.success) {
muteModal.close();
alert(`Muted ${account.username} for ${numberOfDays} day(s).`);
window.location.reload();
} else {
alert(json.error);
}
}).catch(err => {
alert(`An unknown error occurred. Please report this issue.\n\n${err}`);
console.log(err);
});
}
}
async function submitUnmuteUserRequest() {
if (confirm(`Are you sure you want to unmute '${account.username}'?`)) {
fetch(`/mod/unmute/${account.id}`, {
method: 'POST',
})
.then(res => res.json())
.then(json => {
if (json.success) {
alert(`Successfully unmuted '${account.username}'`);
location.reload();
} else {
alert(json.error);
}
});
}
}
async function submitChangeUsernameRequest(unban = false) {
@@ -100,7 +179,7 @@
alert(json.error);
} else {
changeUsernameModal.close();
alert(`Successfully unbanned and changed ${account.username}'s username to '${newUsername}'.`);
alert(`Successfully ${unban ? 'unbanned and ' : ''}changed ${account.username}'s username to '${newUsername}'.`);
location.href = `/mod/overview/${newUsername}`
}
}).catch(err => {
@@ -110,11 +189,30 @@
}
}
muteButton.addEventListener('click', muteUser);
async function submitKickUserRequest() {
if (confirm(`Are you sure you want to kick '${account.username}'?`)) {
fetch(`/mod/kick/${account.id}`, {
method: 'POST',
})
.then(res => res.json())
.then(json => {
if (json.success) {
alert(`Successfully kicked '${account.username}'`);
location.reload();
} else {
alert(json.error);
}
});
}
}
banButton.addEventListener('click', (event) => {
resetModalInputs();
banModal.showModal();
if (account.banned_until) {
submitUnbanUserRequest();
} else {
resetModalInputs();
banModal.showModal();
}
});
banModalMercyButton.addEventListener('click', (event) => {
banModal.close();
@@ -129,6 +227,29 @@
}
});
muteButton.addEventListener('click', (event) => {
if (account.muted_until) {
submitUnmuteUserRequest();
} else {
resetModalInputs();
muteModal.showModal();
}
});
muteModalMuteButton.addEventListener('click', (event) => {
submitMuteUserRequest();
});
muteModalMercyButton.addEventListener('click', (event) => {
muteModal.close();
});
muteModalTimePeriodSelect.addEventListener('change', (event) => {
const newValue = event.target.value;
if (newValue == -1) {
muteModalCustomTimePeriod.classList.remove('hidden');
} else {
muteModalCustomTimePeriod.classList.add('hidden');
}
});
changeUsernameButton.addEventListener('click', (event) => {
resetModalInputs();
changeUsernameModal.showModal();
@@ -143,7 +264,19 @@
changeUsernameModal.close();
});
kickButton.addEventListener('click', (event) => {
submitKickUserRequest();
});
banModalUsername.textContent = account.username;
muteModalUsername.textContent = account.username;
changeUsernameTitle.textContent = account.username;
if (account.banned_until) {
document.getElementById('banned-until-display').textContent = (new Date(account.banned_until)).toLocaleString();
}
if (account.muted_until) {
document.getElementById('muted-until-display').textContent = (new Date(account.muted_until)).toLocaleString();
}
resetModalInputs();
</script>
+8 -6
View File
@@ -9,21 +9,23 @@
<strong>Account name blocked by a moderator.</strong><br><br>
<% } %>
<%- include('../_components/modal-ban-player.ejs', { account }); %>
<%- include('../_components/modal-change-username.ejs'); %>
<%- include('../_components/modal-ban-player'); %>
<%- include('../_components/modal-mute-player'); %>
<%- include('../_components/modal-change-username'); %>
<% if (account.banned_until) { %>
Banned until: <strong><%= account.banned_until.toISOString().replace("T"," ").substring(0, 19); %></strong><br>
Banned until: <strong id="banned-until-display"><%= account.banned_until.toISOString().replace("T"," ").substring(0, 19); %></strong><br>
<% } %>
<% if (account.muted_until) { %>
Muted until: <strong><%= account.muted_until.toISOString().replace("T"," ").substring(0, 19); %></strong>
Muted until: <strong id="muted-until-display"><%= account.muted_until.toISOString().replace("T"," ").substring(0, 19); %></strong>
<% } %>
<div style="display: flex; justify-content: space-around;">
<%- include('../_components/stones-button.ejs', { id: 'ban-btn', text: 'Ban', shiny: true, account }) %>
<%- include('../_components/stones-button.ejs', { id: 'ban-btn', text: account.banned_until ? 'Unban' : 'Ban', shiny: true, account }) %>
<%- include('../_components/stones-button.ejs', { id: 'mute-btn', text: account.muted_until ? 'Unmute' : 'Mute', shiny: true }) %>
<%- include('../_components/stones-button.ejs', { id: 'name-change-btn', text: 'Change<br>Name' }); %>
<%- include('../_components/stones-button.ejs', { id: 'mute-btn', text: 'Mute' }) %>
<%- include('../_components/stones-button.ejs', { id: 'kick-btn', text: 'Kick' }); %>
</div>
<h3>Sessions</h3>