Cleaned up 'src/app/channel'

This commit is contained in:
rainbow napkin 2024-11-20 06:54:44 -05:00
parent 985f8a250f
commit 5b5f495853
4 changed files with 160 additions and 123 deletions

View file

@ -28,39 +28,42 @@ module.exports = class{
}
defineListeners(socket){
socket.on("chatMessage", (data) => {
//Trim and Sanatize for XSS
const msg = validator.trim(validator.escape(data.msg));
//make sure high is an int
const high = validator.toInt(data.high);
const user = this.server.socketToUser(socket);
socket.on("chatMessage", (data) => {this.relayChat(socket, data)});
socket.on("setFlair", async (data) => {this.setFlair(socket, data)});
}
//nuke the message if its empty or huge
if(!validator.isLength(msg, {min: 1, max: 255})){
return;
relayChat(socket, data){
//Trim and Sanatize for XSS
const msg = validator.trim(validator.escape(data.msg));
//make sure high is an int
const high = validator.toInt(data.high);
const user = this.server.getSocketInfo(socket);
//nuke the message if its empty or huge
if(!validator.isLength(msg, {min: 1, max: 255})){
return;
}
//nuke the message if the high number is wrong
if(high < 0 || high > 10){
return;
}
this.server.io.in(socket.chan).emit("chatMessage", {user: socket.user.user, flair: user.flair, high, msg});
}
async setFlair(socket, data){
const userDB = await userModel.findOne({user: socket.user.user});
if(userDB){
try{
//We can take this data raw since our schema checks it against existing flairs, and mongoose sanatizes queries
userDB.flair = data.flair;
await userDB.save();
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
//nuke the message if the high number is wrong
if(high < 0 || high > 10){
return;
}
this.server.io.in(socket.chan).emit("chatMessage", {user: socket.user.user, flair: user.flair, high, msg});
});
socket.on("setFlair", async (data) => {
const userDB = await userModel.findOne({user: socket.user.user});
if(userDB){
try{
//We can take this data raw since our schema checks it against existing flairs, and mongoose sanatizes queries
userDB.flair = data.flair;
await userDB.save();
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
});
}
}
}