Started global user bans (DB schema/admin panel)

This commit is contained in:
rainbow napkin 2024-11-28 17:37:26 -05:00
parent 5c30508e96
commit f996018ea5
20 changed files with 339 additions and 15 deletions

View file

@ -69,6 +69,35 @@ class canopyAdminUtils{
utils.ux.displayResponseError(await response.json());
}
}
async getBans(){
var response = await fetch(`/api/admin/ban`,{
method: "GET"
});
if(response.status == 200){
return await response.json();
}else{
utils.ux.displayResponseError(await response.json());
}
}
async banUser(user){
var response = await fetch(`/api/admin/ban`,{
method: "POST",
headers: {
"Content-Type": "application/json"
},
//Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
body: JSON.stringify({user})
});
if(response.status == 200){
return await response.json();
}else{
utils.ux.displayResponseError(await response.json());
}
}
}
class adminUserList{
@ -146,6 +175,73 @@ class adminPermissionList{
}
}
class adminUserBanList{
constructor(){
this.table = document.querySelector("#admin-ban-list-table");
this.getBanList();
}
async getBanList(){
this.renderBanList(await adminUtil.getBans());
}
renderBanList(banList){
banList.forEach((ban) => {
//Create entry row
const entryRow = document.createElement('tr');
entryRow.classList.add("admin-list-entry");
//Create IMG node inside of IMG cell
const imgNode = document.createElement('img');
imgNode.classList.add("admin-list-entry","admin-list-entry-item");
imgNode.src = ban.user.img;
console.log(new Date(ban.user.date).toDateString());
const expirationDate = new Date(ban.expirationDate);
const expirationDays = Math.floor((expirationDate - new Date()) / (1000 * 60 * 60 * 24));
//Append cells to row
entryRow.appendChild(newCell(imgNode, true, true));
entryRow.appendChild(newCell(ban.user.id));
entryRow.appendChild(newCell(ban.user.user));
entryRow.appendChild(newCell(new Date(ban.user.date).toDateString()));
entryRow.appendChild(newCell(new Date(ban.banDate).toDateString()));
entryRow.appendChild(newCell(`${expirationDate.toDateString()} (${expirationDays} days left)`));
entryRow.appendChild(newCell(ban.deleteAccountOnExpire ? "Delete" : "Un-Ban"));
//Append row to table
this.table.appendChild(entryRow);
});
function newCell(content, addAsNode = false, firstCol = false){
//Create a new 'td' element
const cell = document.createElement('td');
cell.classList.add("admin-list-entry","admin-list-entry-item");
//If it's not the first column, mention it!
if(!firstCol){
cell.classList.add("admin-list-entry-not-first-col");
}
//If we're adding as node
if(addAsNode){
//append it like it's a node
cell.appendChild(content);
}else{
//otherwise use it as innerHTML
cell.innerHTML = content;
}
//return the resulting cell
return cell;
}
}
}
const adminUtil = new canopyAdminUtils();
const userList = new adminUserList();
const permissionList = new adminPermissionList();
const permissionList = new adminPermissionList();
const userBanList = new adminUserBanList();