Finished basic channel ban DB mgmt and ajax calls.

This commit is contained in:
rainbow napkin 2024-11-30 06:38:38 -05:00
parent 2b52fe7f2f
commit ef79e9941c
4 changed files with 293 additions and 3 deletions

View file

@ -67,6 +67,29 @@ const channelSchema = new mongoose.Schema({
required: true,
enum: permissionModel.rankEnum
}
}],
//Thankfully we don't have to keep track of alts, ips, or deleted users so this should be a little easier :P
banList: [{
user: {
type: mongoose.SchemaTypes.ObjectID,
required: true,
ref: "user"
},
banDate: {
type: mongoose.SchemaTypes.Date,
required: true,
default: new Date()
},
expirationDays: {
type: mongoose.SchemaTypes.Number,
required: true,
default: 14
},
banAlts: {
type: mongoose.SchemaTypes.Boolean,
required: true,
default: false
}
}]
});
@ -323,6 +346,113 @@ channelSchema.methods.permCheck = async function (user, perm){
return await this.permCheckByUserDoc(userDB, perm)
}
channelSchema.methods.checkBanByUserDoc = async function(userDB){
var foundBan = null;
this.banList.forEach((ban) => {
if(ban.user != null){
if(ban.user.toString() == userDB._id.toString()){
foundBan = ban;
}
}
});
return foundBan;
}
channelSchema.methods.getChanBans = async function(){
//Create an empty list to hold our found bans
var banList = [];
//Populate the users in the banList
await this.populate('banList.user');
//Crawl through known bans
this.banList.forEach((ban) => {
var banObj = {
banDate: ban.banDate,
expirationDays: ban.expirationDays,
}
//Check if the ban was permanent (expiration set before ban date)
if(ban.expirationDays > 0){
//if not calculate expiration date
var expirationDate = new Date(ban.banDate);
expirationDate.setDate(expirationDate.getDate() + ban.expirationDays);
//Set calculated expiration date
banObj.expirationDate = expirationDate;
}
//Setup user object (Do this last to keep it at bottom for human-readibility of json :P)
banObj.user = {
id: ban.user.id,
user: ban.user.user,
img: ban.user.img,
date: ban.user.date
}
banList.push(banObj);
});
return banList;
}
channelSchema.methods.banByUserDoc = async function(userDB, expirationDays, banAlts){
//Throw a shitfit if the user doesn't exist
if(userDB == null){
throw new Error("Cannot ban non-existant user!");
}
const foundBan = await this.checkBanByUserDoc(userDB);
if(foundBan != null){
throw new Error("User already banned!");
}
//Create a new ban document based on input
const banDoc = {
user: userDB._id,
expirationDays,
banAlts
}
//Push the ban to the list
this.banList.push(banDoc);
await this.save();
}
channelSchema.methods.ban = async function(user, expirationDays, banAlts){
const userDB = await userModel.find({user});
return await this.banByUserDoc(userDB, expirationDays, banAlts);
}
channelSchema.methods.unbanByUserDoc = async function(userDB){
//Throw a shitfit if the user doesn't exist
if(userDB == null){
throw new Error("Cannot ban non-existant user!");
}
const foundBan = await this.checkBanByUserDoc(userDB);
if(foundBan == null){
throw new Error("User already unbanned!");
}
//You know I can't help but feel like an asshole for looking for the index of something I just pulled out of an array using forEach...
//Then again this is such an un-used function that the issue of code re-use overshadows performance
//I mean how often are we REALLY going to be un-banning users from channels?
const banIndex = this.banList.indexOf(foundBan);
this.banList.splice(banIndex,1);
return await this.save();
}
channelSchema.methods.unban = async function(user){
const userDB = await userModel.find({user});
return await this.unbanByUserDoc(userDB);
}
channelSchema.methods.nuke = async function(confirm){
if(confirm == "" || confirm == null){
throw new Error("Empty Confirmation String!");