diff --git a/defaultFlair.json b/defaultFlair.json index 65c2801..bb54af7 100644 --- a/defaultFlair.json +++ b/defaultFlair.json @@ -1,17 +1,24 @@ -[ - { - "name": "gold", - "displayName": "Gold", - "rank": "gold" +{ + "default": { + "name": "classic", + "displayName": "Classic", + "rank": "user" }, - { - "name": "black-gold", - "displayName": "Black Gold", - "rank": "gold" - }, - { - "name": "lightning", - "displayName": "Lightning", - "rank": "gold" - } -] + "array": [ + { + "name": "gold", + "displayName": "Gold", + "rank": "gold" + }, + { + "name": "black-gold", + "displayName": "Black Gold", + "rank": "gold" + }, + { + "name": "lightning", + "displayName": "Lightning", + "rank": "gold" + } + ] +} diff --git a/src/app/channel/activeChannel.js b/src/app/channel/activeChannel.js index e018185..c701288 100644 --- a/src/app/channel/activeChannel.js +++ b/src/app/channel/activeChannel.js @@ -38,7 +38,8 @@ module.exports = class{ //Add this socket on to the userobject userObj.sockets.push(socket.id); }else{ - userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, chanRank, userDB.flair, this, socket); + await userDB.populate('flair'); + userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, chanRank, userDB.flair.name, this, socket); } //Set user entry in userlist diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index a202a58..174bc33 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -58,13 +58,12 @@ module.exports = class{ 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; - userDB = await userDB.save(); + const flairDB = await userDB.setFlair(data.flair); const connections = this.server.getConnections(socket.user.user); connections.forEach((conn) => { - conn.updateFlair(userDB.flair); + conn.updateFlair(flairDB.name); }); }catch(err){ return loggerUtils.socketExceptionHandler(socket, err); diff --git a/src/schemas/flairSchema.js b/src/schemas/flairSchema.js index 76fb0ac..896628a 100644 --- a/src/schemas/flairSchema.js +++ b/src/schemas/flairSchema.js @@ -39,27 +39,34 @@ const flairSchema = new mongoose.Schema({ }); flairSchema.statics.loadDefaults = async function(){ + //Make sure registerFlair function is happy + const _this = this; + + //Ensure default comes first (.bind(this) doesn't seem to work here...) + await registerFlair(defaultFlair.default); //For each entry in the defaultFlair.json file - defaultFlair.forEach(async (flair) => { + defaultFlair.array.forEach(registerFlair); + + async function registerFlair(flair){ try{ - //Look for flair matching the one from our file + //Look for flair matching the one from our file + const foundFlair = await _this.findOne({name: flair.name}); - const foundFlair = await this.findOne({name: flair.name}); - - //if the flair doesn't exist - if(!foundFlair){ - const flairDB = await this.create(flair); - console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`); - } + //if the flair doesn't exist + if(!foundFlair){ + const flairDB = await _this.create(flair); + console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`); + } }catch(err){ if(flair != null){ + console.log(err); console.log(`Error loading flair '${flair.name}':`); }else{ console.log("Error, null flair:"); } } - }); + } } module.exports = mongoose.model("flair", flairSchema); \ No newline at end of file diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index 28760af..56d6723 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -78,9 +78,9 @@ const userSchema = new mongoose.Schema({ default: "Signature not set!" }, flair: { - type: mongoose.SchemaTypes.String, - required: false, - default: "" + type: mongoose.SchemaTypes.ObjectID, + default: null, + ref: "flair" } }); @@ -93,25 +93,24 @@ userSchema.pre('save', async function (next){ this.pass = hashUtil.hashPassword(this.pass); } + + //If the flair was changed if(this.isModified("flair")){ - //If we're not disabling flair - if(this.flair != ""){ - //Look for the flair that was set - const foundFlair = await flairModel.findOne({name: this.flair}); - - //If new flair value doesn't corrispond to an existing flair - if(!foundFlair){ - //Throw a shit fit. Do not pass go. Do not collect $200. - throw new Error("Invalid flair!"); - } + //Get flair properties + await this.populate('flair'); - if(permissionModel.rankToNum(this.rank) < permissionModel.rankToNum(foundFlair.rank)){ - throw new Error(`User '${this.user}' does not have a high enough rank for flair '${foundFlair.displayName}'!`); - } + if(permissionModel.rankToNum(this.rank) < permissionModel.rankToNum(this.flair.rank)){ + throw new Error(`User '${this.user}' does not have a high enough rank for flair '${this.flair.displayName}'!`); } } + //Ensure we don't have empty flair + if(this.flair == null){ + const flairDB = await flairModel.findOne({}); + this.flair = flairDB._id; + } + if(this.isModified("rank")){ await this.killAllSessions("Your site-wide rank has changed. Sign-in required."); } @@ -234,6 +233,18 @@ userSchema.statics.getUserList = async function(fullList = false){ return userList; } +//methods +userSchema.methods.setFlair = async function(flair){ + //Find flair by name + const flairDB = await flairModel.findOne({name: flair}); + //Set the users flair ref to the found flairs _id + this.flair = flairDB._id; + //Save the user + await this.save(); + //return the found flair + return flairDB; +} + //note: if you gotta call this from a request authenticated by it's user, make sure to kill that session first! userSchema.methods.killAllSessions = async function(reason = "A full log-out from all devices was requested for your account."){ //get authenticated sessions diff --git a/www/js/channel/chat.js b/www/js/channel/chat.js index 3e31d13..6a08dfc 100644 --- a/www/js/channel/chat.js +++ b/www/js/channel/chat.js @@ -87,7 +87,7 @@ class chatBox{ var userLabel = document.createElement('p'); userLabel.classList.add("chat-panel-buffer","chat-entry-username"); - if(chat.flair != ""){ + if(chat.flair != "classic"){ var flair = `flair-${chat.flair}`; }else{ var flair = this.client.userList.colorMap.get(chat.user); @@ -134,12 +134,6 @@ class chatBox{ //clear current flair select this.flairSelect.innerHTML = ""; - //Inject flair object for standard flair like the hack we are - flairList.push({ - name: "", - displayName: "Classic" - }); - //For each flair in flairlist flairList.forEach((flair) => { //Create an option diff --git a/www/js/channel/userlist.js b/www/js/channel/userlist.js index 55b9c23..18d7179 100644 --- a/www/js/channel/userlist.js +++ b/www/js/channel/userlist.js @@ -95,7 +95,7 @@ class userList{ userEntry.id = `user-entry-${user.user}`; //Override color with flair - if(user.flair != ""){ + if(user.flair != "classic"){ color = `flair-${user.flair}`; }