highLevel syncs across chans, shows in userlist

This commit is contained in:
rainbow napkin 2024-12-03 20:27:37 -05:00
parent 279640a7e7
commit 8ccb9003cc
8 changed files with 102 additions and 38 deletions

View file

@ -39,7 +39,7 @@ module.exports = class{
userObj.sockets.push(socket.id);
}else{
await userDB.populate('flair');
userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, chanRank, userDB.flair.name, this, socket);
userObj = new connectedUser(userDB, chanRank, this, socket);
}
//Set user entry in userlist
@ -84,7 +84,8 @@ module.exports = class{
this.userList.forEach((userObj, user) => {
userList.push({
user: user,
flair: userObj.flair
flair: userObj.flair,
highLevel: userObj.highLevel
});
});

View file

@ -28,15 +28,13 @@ module.exports = class{
defineListeners(socket){
socket.on("chatMessage", (data) => {this.relayChat(socket, data)});
socket.on("setFlair", async (data) => {this.setFlair(socket, data)});
socket.on("setFlair", (data) => {this.setFlair(socket, data)});
socket.on("setHighLevel", (data) => {this.setHighLevel(socket, data)});
}
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
@ -44,12 +42,7 @@ module.exports = class{
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});
this.server.io.in(socket.chan).emit("chatMessage", {user: user.user, flair: user.flair, highLevel: user.highLevel, msg});
}
async setFlair(socket, data){
@ -60,8 +53,10 @@ module.exports = class{
//We can take this data raw since our schema checks it against existing flairs, and mongoose sanatizes queries
const flairDB = await userDB.setFlair(data.flair);
//GetConnects across all channels
const connections = this.server.getConnections(socket.user.user);
//For each connection
connections.forEach((conn) => {
conn.updateFlair(flairDB.name);
});
@ -70,4 +65,27 @@ module.exports = class{
}
}
}
async setHighLevel(socket, data){
var userDB = await userModel.findOne({user: socket.user.user});
if(userDB){
try{
//Set high level
userDB.highLevel = data.highLevel;
//Save user DB Document
await userDB.save();
//GetConnects across all channels
const connections = this.server.getConnections(socket.user.user);
//For each connection
connections.forEach((conn) => {
conn.updateHighLevel(userDB.highLevel);
});
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
}
}

View file

@ -19,12 +19,13 @@ const flairModel = require('../../schemas/flairSchema');
const permissionModel = require('../../schemas/permissionSchema');
module.exports = class{
constructor(id, name, rank, chanRank, flair, channel, socket){
this.id = id;
this.name = name;
this.rank = rank;
constructor(userDB, chanRank, channel, socket){
this.id = userDB.id;
this.user = userDB.user;
this.rank = userDB.rank;
this.highLevel = userDB.highLevel;
this.flair = userDB.flair.name;
this.chanRank = chanRank;
this.flair = flair;
this.channel = channel;
this.sockets = [socket.id];
}
@ -59,6 +60,8 @@ module.exports = class{
id: this.id,
user: this.user,
rank: this.rank,
chanRank: this.chanRank,
highLevel: this.highLevel,
flair: this.flair
}
@ -79,12 +82,17 @@ module.exports = class{
}
updateFlair(flair){
//This will run multiple times in a row, we don't need to broadcast the userlist every time
if(this.flair != flair){
this.flair = flair;
this.channel.broadcastUserList();
}
this.flair = flair;
this.channel.broadcastUserList();
this.sendClientMetadata();
}
updateHighLevel(highLevel){
this.highLevel = highLevel;
//TODO: show high-level in userlist
this.channel.broadcastUserList();
this.sendClientMetadata();
}
}

View file

@ -77,6 +77,13 @@ const userSchema = new mongoose.Schema({
required: true,
default: "Signature not set!"
},
highLevel: {
type: mongoose.SchemaTypes.Number,
required: true,
min: 0,
max: 10,
default: 0
},
flair: {
type: mongoose.SchemaTypes.ObjectID,
default: null,
@ -93,8 +100,6 @@ userSchema.pre('save', async function (next){
this.pass = hashUtil.hashPassword(this.pass);
}
//If the flair was changed
if(this.isModified("flair")){
//Get flair properties