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

@ -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){

View file

@ -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){

View file

@ -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

View file

@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//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){

View file

@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//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){

View file

@ -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{

View file

@ -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);

View file

@ -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);

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');
@ -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;

View file

@ -15,7 +15,7 @@ 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/>.*/
//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

View file

@ -15,7 +15,7 @@ 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/>.*/
//local imports
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils.js');
const {userModel} = require('../../../schemas/userSchema');
//api list account functions

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 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();

View file

@ -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);

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

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
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');

View file

@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
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

View file

@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//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');

View file

@ -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;

View file

@ -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);
}
});
});

View file

@ -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);
}
}
}

View file

@ -15,14 +15,13 @@ 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/>.*/
//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");
}

View file

@ -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{