diff --git a/src/controllers/adminPanelController.js b/src/controllers/adminPanelController.js index 42ba146..d126eba 100644 --- a/src/controllers/adminPanelController.js +++ b/src/controllers/adminPanelController.js @@ -19,7 +19,7 @@ const config = require('../../config.json'); const {userModel} = require('../schemas/userSchema'); const permissionModel = require('../schemas/permissionSchema'); const channelModel = require('../schemas/channel/channelSchema'); -const {exceptionHandler} = require("../utils/loggerUtils"); +const {exceptionHandler, errorHandler} = require("../utils/loggerUtils"); //register page functions module.exports.get = async function(req, res){ diff --git a/src/controllers/api/account/deleteController.js b/src/controllers/api/account/deleteController.js index 45ed2c7..eaf4e39 100644 --- a/src/controllers/api/account/deleteController.js +++ b/src/controllers/api/account/deleteController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {userModel} = require('../../../schemas/userSchema'); const accountUtils = require('../../../utils/sessionUtils.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); //api account functions module.exports.post = async function(req, res){ diff --git a/src/controllers/api/account/loginController.js b/src/controllers/api/account/loginController.js index 2bdde09..5ea734d 100644 --- a/src/controllers/api/account/loginController.js +++ b/src/controllers/api/account/loginController.js @@ -19,7 +19,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const accountUtils = require('../../../utils/sessionUtils.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); //api account functions diff --git a/src/controllers/api/account/logoutController.js b/src/controllers/api/account/logoutController.js index 3e2132c..89a37b2 100644 --- a/src/controllers/api/account/logoutController.js +++ b/src/controllers/api/account/logoutController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //local imports const accountUtils = require('../../../utils/sessionUtils.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); module.exports.get = async function(req, res){ if(req.session.user){ diff --git a/src/controllers/api/account/rankEnumController.js b/src/controllers/api/account/rankEnumController.js index 0776293..a176cdc 100644 --- a/src/controllers/api/account/rankEnumController.js +++ b/src/controllers/api/account/rankEnumController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //local imports const permissionModel = require('../../../schemas/permissionSchema.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); //api account functions module.exports.get = async function(req, res){ diff --git a/src/controllers/api/account/registerController.js b/src/controllers/api/account/registerController.js index bcd5411..160139f 100644 --- a/src/controllers/api/account/registerController.js +++ b/src/controllers/api/account/registerController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {userModel} = require('../../../schemas/userSchema'); const userBanModel = require('../../../schemas/userBanSchema.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); module.exports.post = async function(req, res){ try{ diff --git a/src/controllers/api/account/updateController.js b/src/controllers/api/account/updateController.js index 50ffd0e..0698002 100644 --- a/src/controllers/api/account/updateController.js +++ b/src/controllers/api/account/updateController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {userModel} = require('../../../schemas/userSchema'); const accountUtils = require('../../../utils/sessionUtils.js'); -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); module.exports.post = async function(req, res){ const validResult = validationResult(req); diff --git a/src/controllers/api/admin/banController.js b/src/controllers/api/admin/banController.js index f0880e2..bd5d85b 100644 --- a/src/controllers/api/admin/banController.js +++ b/src/controllers/api/admin/banController.js @@ -21,7 +21,7 @@ const {validationResult, matchedData} = require('express-validator'); const banModel = require('../../../schemas/userBanSchema'); const permissionModel = require('../../../schemas/permissionSchema'); const {userModel} = require('../../../schemas/userSchema'); -const {exceptionHandler} = require('../../../utils/loggerUtils'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); module.exports.get = async function(req, res){ try{ @@ -45,16 +45,13 @@ module.exports.post = async function(req, res){ if(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', 400); }else if(userDB.user == req.session.user.user){ //If some smart-ass is trying self-privelege escalation - 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(userDB.rank) >= permissionModel.rankToNum(req.session.user.rank)){ //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 ban peer/outranking users.", date: new Date()}]}); + return errorHandler(res, 'You cannot ban peer/outranking users', 'Unauthorized', 401); } await banModel.banByUserDoc(userDB, permanent, expirationDays); diff --git a/src/controllers/api/admin/changeRankController.js b/src/controllers/api/admin/changeRankController.js index cf51d84..503ec89 100644 --- a/src/controllers/api/admin/changeRankController.js +++ b/src/controllers/api/admin/changeRankController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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'); @@ -36,20 +36,16 @@ module.exports.post = async function(req, res){ if(userDB == null){ //If the user is null, scream and shout - res.status(400); - res.send({errors:[{type: "Bad Query", msg: "User not found.", date: new Date()}]}); + return errorHandler(res, 'User not found.', 'Bad Query'); }else if(userDB.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(req.session.user.rank)){ //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(userDB.rank) >= permissionModel.rankToNum(req.session.user.rank)){ //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); } userDB.rank = data.rank; diff --git a/src/controllers/api/admin/listChannelsController.js b/src/controllers/api/admin/listChannelsController.js index 089a378..15af92b 100644 --- a/src/controllers/api/admin/listChannelsController.js +++ b/src/controllers/api/admin/listChannelsController.js @@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ //local imports -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); const channelModel = require('../../../schemas/channel/channelSchema.js'); //api list channel functions diff --git a/src/controllers/api/admin/listUsersController.js b/src/controllers/api/admin/listUsersController.js index 217c3ef..4b6706e 100644 --- a/src/controllers/api/admin/listUsersController.js +++ b/src/controllers/api/admin/listUsersController.js @@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ //local imports -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); const {userModel} = require('../../../schemas/userSchema'); //api list account functions diff --git a/src/controllers/api/admin/permissionsController.js b/src/controllers/api/admin/permissionsController.js index 2442139..498471b 100644 --- a/src/controllers/api/admin/permissionsController.js +++ b/src/controllers/api/admin/permissionsController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const {validationResult, matchedData} = require('express-validator'); //local imports -const {exceptionHandler} = require('../../../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js'); const permissionModel = require('../../../schemas/permissionSchema.js'); //api permissions functions @@ -76,8 +76,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 perms.save(); diff --git a/src/controllers/api/admin/tokeCommandController.js b/src/controllers/api/admin/tokeCommandController.js index 8231fc6..ce4a452 100644 --- a/src/controllers/api/admin/tokeCommandController.js +++ b/src/controllers/api/admin/tokeCommandController.js @@ -40,14 +40,44 @@ module.exports.post = async function(req, res){ //if they're empty if(validResult.isEmpty()){ const {command} = matchedData(req); - const foundToke = await tokeCommandModel.findOne({command}); + const tokeDB = await tokeCommandModel.findOne({command}); - if(foundToke != null){ + if(tokeDB != null){ return errorHandler(res, `Toke command '!${command}' already exists!`); } //Add the toke - const tokeDB = await tokeCommandModel.create({command}); + await tokeCommandModel.create({command}); + + //Return the updated command list + res.status(200); + return res.send(await tokeCommandModel.getCommandStrings()); + }else{ + //otherwise scream + res.status(400); + return res.send({errors: validResult.array()}) + } + + }catch(err){ + return exceptionHandler(res, err); + } +} + +module.exports.delete = async function(req, res){ + try{ + //get validation error results + const validResult = validationResult(req); + + //if they're empty + if(validResult.isEmpty()){ + const {command} = matchedData(req); + const tokeDB = await tokeCommandModel.findOne({command}); + + if(tokeDB == null){ + return errorHandler(res, `Cannot delete non-existant toke command '!${command}'!`); + } + + await tokeCommandModel.deleteOne({_id: tokeDB._id}); //Return the updated command list res.status(200); diff --git a/src/controllers/api/channel/banController.js b/src/controllers/api/channel/banController.js index 49a2245..8748731 100644 --- a/src/controllers/api/channel/banController.js +++ b/src/controllers/api/channel/banController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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); diff --git a/src/controllers/api/channel/deleteController.js b/src/controllers/api/channel/deleteController.js index 3d96fbb..4932a6d 100644 --- a/src/controllers/api/channel/deleteController.js +++ b/src/controllers/api/channel/deleteController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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 diff --git a/src/controllers/api/channel/listController.js b/src/controllers/api/channel/listController.js index b751b85..19797f8 100644 --- a/src/controllers/api/channel/listController.js +++ b/src/controllers/api/channel/listController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //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){ diff --git a/src/controllers/api/channel/permissionsController.js b/src/controllers/api/channel/permissionsController.js index 8bd8eed..d44c27f 100644 --- a/src/controllers/api/channel/permissionsController.js +++ b/src/controllers/api/channel/permissionsController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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(); diff --git a/src/controllers/api/channel/rankController.js b/src/controllers/api/channel/rankController.js index be7641b..14ed882 100644 --- a/src/controllers/api/channel/rankController.js +++ b/src/controllers/api/channel/rankController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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 diff --git a/src/controllers/api/channel/registerController.js b/src/controllers/api/channel/registerController.js index d299e09..cc3b038 100644 --- a/src/controllers/api/channel/registerController.js +++ b/src/controllers/api/channel/registerController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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'); diff --git a/src/controllers/api/channel/settingsController.js b/src/controllers/api/channel/settingsController.js index 7c73411..e55fb85 100644 --- a/src/controllers/api/channel/settingsController.js +++ b/src/controllers/api/channel/settingsController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ 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 diff --git a/src/controllers/channelSettingsController.js b/src/controllers/channelSettingsController.js index a79e1a9..4aa024f 100644 --- a/src/controllers/channelSettingsController.js +++ b/src/controllers/channelSettingsController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const config = require('../../config.json'); //local imports -const {exceptionHandler} = require('../utils/loggerUtils'); +const {exceptionHandler, errorHandler} = require('../utils/loggerUtils'); const channelModel = require('../schemas/channel/channelSchema'); const permissionModel = require('../schemas/permissionSchema'); diff --git a/src/controllers/indexController.js b/src/controllers/indexController.js index 4132071..42dbc60 100644 --- a/src/controllers/indexController.js +++ b/src/controllers/indexController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const config = require('../../config.json'); //local imports -const {exceptionHandler} = require('../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../utils/loggerUtils.js'); const channelModel = require('../schemas/channel/channelSchema'); //root index functions diff --git a/src/controllers/profileController.js b/src/controllers/profileController.js index 322891e..f6d9032 100644 --- a/src/controllers/profileController.js +++ b/src/controllers/profileController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //Local Imports const {userModel} = require('../schemas/userSchema'); -const {exceptionHandler} = require('../utils/loggerUtils.js'); +const {exceptionHandler, errorHandler} = require('../utils/loggerUtils.js'); //Config const config = require('../../config.json'); diff --git a/src/routers/api/adminRouter.js b/src/routers/api/adminRouter.js index d065d1a..048d9c0 100644 --- a/src/routers/api/adminRouter.js +++ b/src/routers/api/adminRouter.js @@ -46,5 +46,6 @@ router.post('/ban', permissionSchema.reqPermCheck("banUser"), accountValidator. router.delete('/ban', permissionSchema.reqPermCheck("banUser"), accountValidator.user(), banController.delete); router.get('/tokeCommands', permissionSchema.reqPermCheck("adminPanel"), tokeCommandController.get); router.post('/tokeCommands', permissionSchema.reqPermCheck("editTokeCommands"), tokebotValidator.command(), tokeCommandController.post); +router.delete('/tokeCommands', permissionSchema.reqPermCheck("editTokeCommands"), tokebotValidator.command(), tokeCommandController.delete); module.exports = router; diff --git a/src/schemas/channel/channelSchema.js b/src/schemas/channel/channelSchema.js index 083408b..d4a3471 100644 --- a/src/schemas/channel/channelSchema.js +++ b/src/schemas/channel/channelSchema.js @@ -225,8 +225,7 @@ channelSchema.statics.reqPermCheck = function(perm, chanField = "chanName"){ next(); }else{ //If not, prevent the request from going through and tell them why - res.status(401); - return res.send({errors:[{type: "Unauthorized", msg: "You do not have a high enough rank to access this resource.", date: new Date()}]}); + return errorHandler(res, "You do not have a high enough rank to access this resource.", 'Unauthorized', 401); } }); }); diff --git a/src/schemas/permissionSchema.js b/src/schemas/permissionSchema.js index a8354e9..e0ba7c7 100644 --- a/src/schemas/permissionSchema.js +++ b/src/schemas/permissionSchema.js @@ -155,8 +155,7 @@ permissionSchema.statics.reqPermCheck = function(perm){ if(await permissionSchema.statics.permCheck(req.session.user, perm)){ next(); }else{ - res.status(401); - return res.send({errors:[{type: "Unauthorized", msg: "You do not have a high enough rank to access this resource.", date: new Date()}]}); + return errorHandler(res, "You do not have a high enough rank to access this resource.", 'Unauthorized', 401); } } } diff --git a/src/utils/loggerUtils.js b/src/utils/loggerUtils.js index 049f716..fa7dc4e 100644 --- a/src/utils/loggerUtils.js +++ b/src/utils/loggerUtils.js @@ -15,14 +15,13 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ //At some point this will be a bit more advanced, right now it's just a placeholder :P -module.exports.errorHandler = function(res, msg, type = "Generic"){ - res.status(400); +module.exports.errorHandler = function(res, msg, type = "Generic", status = 400){ + res.status(status); return res.send({errors: [{type, msg, date: new Date()}]}); } module.exports.exceptionHandler = function(res, err){ //if not yell at the browser for fucking up, and tell it what it did wrong. - res.status(400); module.exports.errorHandler(res, err.message, "Caught Exception"); } diff --git a/www/js/adminPanel.js b/www/js/adminPanel.js index 8637d90..410d943 100644 --- a/www/js/adminPanel.js +++ b/www/js/adminPanel.js @@ -198,6 +198,23 @@ class canopyAdminUtils{ utils.ux.displayResponseError(await response.json()); } } + + async deleteTokeCommand(command){ + var response = await fetch(`/api/admin/tokeCommands`,{ + method: "DELETE", + 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({command}) + }); + + if(response.status == 200){ + return await response.json(); + }else{ + utils.ux.displayResponseError(await response.json()); + } + } } class adminUserList{