Improved channelManager and chatHandler

This commit is contained in:
rainbow napkin 2024-11-20 05:27:05 -05:00
parent 4b4cb2ed3d
commit 985f8a250f
3 changed files with 154 additions and 117 deletions

View file

@ -16,42 +16,51 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports
const validator = require('validator');//No express here, so regular validator it is!
//local imports
const loggerUtils = require('../../utils/loggerUtils');
const userModel = require('../../schemas/userSchema');
const channelManager = require('./channelManager');
module.exports.defineListeners = function(io, 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);
module.exports = class{
constructor(server){
this.server = server;
}
//nuke the message if its empty or huge
if(!validator.isLength(msg, {min: 1, max: 255})){
return;
}
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);
//nuke the message if the high number is wrong
if(high < 0 || high > 10){
return;
}
io.in(socket.chan).emit("chatMessage", {user: socket.user.user, flair: socket.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);
//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});
});
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);
}
}
});
}
}