Added tokeCommand delete endpoint and ajax call, replaced all raw error responses.

This commit is contained in:
rainbow napkin 2024-12-12 05:11:47 -05:00
parent af7f4219a5
commit 864304f13b
28 changed files with 93 additions and 64 deletions

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const {userModel} = require('../../../schemas/userSchema.js');
const channelModel = require('../../../schemas/channel/channelSchema');
const permissionModel = require('../../../schemas/permissionSchema.js')
@ -65,16 +65,13 @@ module.exports.post = async function(req, res){
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()}]});
return errorHandler(res, `User not found.`, 'Bad Query', 400);
}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()}]});
return errorHandler(res, `Keep it up, maybe I will ban you!`, 'Unauthorized', 401);
}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()}]});
return errorHandler(res, 'You cannot ban peer/outranking users', 'Unauthorized', 401);
}
await chanDB.banByUserDoc(targetDB, expirationDays, banAlts);

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const channelModel = require('../../../schemas/channel/channelSchema');
//api account functions

View file

@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//local imports
const channelModel = require('../../../schemas/channel/channelSchema');
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
//api account functions
module.exports.get = async function(req, res){

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const channelModel = require('../../../schemas/channel/channelSchema.js');
const permissionModel = require('../../../schemas/permissionSchema.js');
@ -81,8 +81,7 @@ module.exports.post = async function(req, res){
//Flip our shit if something's wrong.
if(permError){
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "New rank must be equal to or below that of the user changing it.", date: new Date()}]});
return errorHandler(res, "New rank must be equal to or below that of the user changing it.", 'Unauthorized', 401);
}
await chanDB.save();

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils');
const permissionModel = require('../../../schemas/permissionSchema');
const {userModel} = require('../../../schemas/userSchema');
const channelModel = require('../../../schemas/channel/channelSchema');
@ -80,20 +80,16 @@ module.exports.post = async function(req, res){
if(data.user == null || userDB == 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()}]});
return errorHandler(res, 'User not found.', 'Bad Query');
}else if(data.user == req.session.user.user){
//If some smart-ass is trying self-privelege escalation
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "No, you can't change your own rank. Fuck off.", date: new Date()}]});
return errorHandler(res, "No, you can't change your own rank, fuck off.", 'Unauthorized', 401);
}else if(permissionModel.rankToNum(data.rank) >= permissionModel.rankToNum(chanRank)){
//If the user is below the new rank of the user they're setting, scream and shout
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "New rank must be below that of the user changing it.", date: new Date()}]});
return errorHandler(res, "New rank must be below that of the user changing it.", 'Unauthorized', 401);
}else if(permissionModel.rankToNum(targetChanRank) >= permissionModel.rankToNum(chanRank)){
//If the user is below the original rank of the user they're setting, scream and shout
res.status(401);
return res.send({errors:[{type: "Unauthorized", msg: "You cannot promote/demote peer/outranking users.", date: new Date()}]});
return errorHandler(res, "You cannot promote/demote peer/outranking users.", 'Unauthorized', 401);
}
//Set rank

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const {userModel} = require('../../../schemas/userSchema.js');
const channelModel = require('../../../schemas/channel/channelSchema');

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const channelModel = require('../../../schemas/channel/channelSchema');
//api account functions