Updated userSchema to store flair by reference

This commit is contained in:
rainbow napkin 2024-12-03 06:21:43 -05:00
parent 108290de2d
commit 279640a7e7
7 changed files with 73 additions and 54 deletions

View file

@ -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"
}
]
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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