Added rankfighting to ban.

This commit is contained in:
rainbow napkin 2024-12-02 19:50:26 -05:00
parent b138b26f27
commit ef4894e409
6 changed files with 41 additions and 12 deletions

View file

@ -21,6 +21,7 @@ const {validationResult, matchedData} = require('express-validator');
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {userModel} = require('../../../schemas/userSchema.js');
const channelModel = require('../../../schemas/channel/channelSchema');
const permissionModel = require('../../../schemas/permissionSchema.js')
//api account functions
module.exports.get = async function(req, res){
@ -55,10 +56,28 @@ module.exports.post = async function(req, res){
if(validResult.isEmpty()){
//Set channel object from sanatized/validated data, and get user document from session data
const {chanName, user, expirationDays, banAlts} = matchedData(req);
const userDB = await userModel.findOne({user});
const initiatorDB = await userModel.findOne({user: req.session.user.user});
const targetDB = await userModel.findOne({user});
const chanDB = await channelModel.findOne({name: chanName});
await chanDB.banByUserDoc(userDB, expirationDays, banAlts);
const initiatorRank = await chanDB.getChannelRankByUserDoc(initiatorDB);
const targetRank = await chanDB.getChannelRankByUserDoc(targetDB);
if(targetDB == null){
//If the user is null, scream and shout
res.status(400);
return res.send({errors:[{type: "Bad Query", msg: "User not found.", date: new Date()}]});
}else if(targetDB.user == req.session.user.user){
//If some smart-ass is trying to self-ban
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "Keep it up, maybe I will ban you!", date: new Date()}]});
}else if(permissionModel.rankToNum(targetRank) >= permissionModel.rankToNum(initiatorRank)){
//If the user is trying to ban a peer/outranking user
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "You cannot ban peer/outranking users.", date: new Date()}]});
}
await chanDB.banByUserDoc(targetDB, expirationDays, banAlts);
res.status(200);
return res.send(await chanDB.getChanBans());