Finished up implementing channel-based user bans.

This commit is contained in:
rainbow napkin 2024-12-01 17:18:43 -05:00
parent ef79e9941c
commit 96953979a2
19 changed files with 518 additions and 202 deletions

View file

@ -0,0 +1,53 @@
/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const {mongoose} = require('mongoose');
const channelBanSchema = new mongoose.Schema({
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
}
});
//methods
channelBanSchema.methods.getDaysUntilExpiration = function(){
//Get ban date
const expirationDate = new Date(this.banDate);
//Get expiration days and calculate expiration date
expirationDate.setDate(expirationDate.getDate() + this.expirationDays);
//Calculate and return days until ban expiration
return ((expirationDate - new Date()) / (1000 * 60 * 60 * 24)).toFixed(1);
}
module.exports = channelBanSchema;

View file

@ -19,11 +19,12 @@ const {mongoose} = require('mongoose');
const {validationResult, matchedData} = require('express-validator');
//Local Imports
const statModel = require('../statSchema.js');
const {userModel} = require('../userSchema.js');
const permissionModel = require('../permissionSchema.js');
const channelPermissionSchema = require('./channelPermissionSchema.js');
const { exceptionHandler } = require('../../utils/loggerUtils.js');
const statModel = require('../statSchema');
const {userModel} = require('../userSchema');
const permissionModel = require('../permissionSchema');
const channelPermissionSchema = require('./channelPermissionSchema');
const channelBanSchema = require('./channelBanSchema');
const { exceptionHandler } = require('../../utils/loggerUtils');
const channelSchema = new mongoose.Schema({
id: {
@ -69,28 +70,7 @@ const channelSchema = new mongoose.Schema({
}
}],
//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
}
}]
banList: [channelBanSchema]
});
@ -194,6 +174,21 @@ channelSchema.statics.reqPermCheck = function(perm, chanField = "chanName"){
}
}
channelSchema.statics.processExpiredBans = async function(){
const chanDB = await this.find({});
chanDB.forEach((channel) => {
channel.banList.forEach(async (ban, i) => {
//ignore permanent and non-expired bans
if(ban.expirationDays >= 0 && ban.getDaysUntilExpiration() <= 0){
//Get the index of the ban
channel.banList.splice(i,1);
return await channel.save();
}
})
});
}
//methods
channelSchema.methods.updateSettings = async function(settingsMap){
settingsMap.forEach((value, key) => {
@ -372,6 +367,7 @@ channelSchema.methods.getChanBans = async function(){
var banObj = {
banDate: ban.banDate,
expirationDays: ban.expirationDays,
banAlts: ban.banAlts,
}
//Check if the ban was permanent (expiration set before ban date)
@ -382,6 +378,7 @@ channelSchema.methods.getChanBans = async function(){
//Set calculated expiration date
banObj.expirationDate = expirationDate;
banObj.daysUntilExpiration = ban.getDaysUntilExpiration();
}
//Setup user object (Do this last to keep it at bottom for human-readibility of json :P)

View file

@ -196,6 +196,7 @@ userBanSchema.statics.getBans = async function(){
banDate: ban.banDate,
expirationDays: ban.expirationDays,
expirationDate: expirationDate,
daysUntilExpiration: ban.getDaysUntilExpiration(),
user: userObj,
ips: ban.ips,
alts: ban.alts,
@ -247,7 +248,7 @@ userBanSchema.methods.getDaysUntilExpiration = function(){
//Get expiration days and calculate expiration date
expirationDate.setDate(expirationDate.getDate() + this.expirationDays);
//Calculate and return days until ban expiration
return daysUntilExpiraiton = ((expirationDate - new Date()) / (1000 * 60 * 60 * 24)).toFixed(1);
return ((expirationDate - new Date()) / (1000 * 60 * 60 * 24)).toFixed(1);
}
module.exports = mongoose.model("userBan", userBanSchema);