Updated userSchema to store flair by reference
This commit is contained in:
parent
108290de2d
commit
279640a7e7
|
|
@ -1,17 +1,24 @@
|
||||||
[
|
{
|
||||||
{
|
"default": {
|
||||||
"name": "gold",
|
"name": "classic",
|
||||||
"displayName": "Gold",
|
"displayName": "Classic",
|
||||||
"rank": "gold"
|
"rank": "user"
|
||||||
},
|
},
|
||||||
{
|
"array": [
|
||||||
"name": "black-gold",
|
{
|
||||||
"displayName": "Black Gold",
|
"name": "gold",
|
||||||
"rank": "gold"
|
"displayName": "Gold",
|
||||||
},
|
"rank": "gold"
|
||||||
{
|
},
|
||||||
"name": "lightning",
|
{
|
||||||
"displayName": "Lightning",
|
"name": "black-gold",
|
||||||
"rank": "gold"
|
"displayName": "Black Gold",
|
||||||
}
|
"rank": "gold"
|
||||||
]
|
},
|
||||||
|
{
|
||||||
|
"name": "lightning",
|
||||||
|
"displayName": "Lightning",
|
||||||
|
"rank": "gold"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,8 @@ module.exports = class{
|
||||||
//Add this socket on to the userobject
|
//Add this socket on to the userobject
|
||||||
userObj.sockets.push(socket.id);
|
userObj.sockets.push(socket.id);
|
||||||
}else{
|
}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
|
//Set user entry in userlist
|
||||||
|
|
|
||||||
|
|
@ -58,13 +58,12 @@ module.exports = class{
|
||||||
if(userDB){
|
if(userDB){
|
||||||
try{
|
try{
|
||||||
//We can take this data raw since our schema checks it against existing flairs, and mongoose sanatizes queries
|
//We can take this data raw since our schema checks it against existing flairs, and mongoose sanatizes queries
|
||||||
userDB.flair = data.flair;
|
const flairDB = await userDB.setFlair(data.flair);
|
||||||
userDB = await userDB.save();
|
|
||||||
|
|
||||||
const connections = this.server.getConnections(socket.user.user);
|
const connections = this.server.getConnections(socket.user.user);
|
||||||
|
|
||||||
connections.forEach((conn) => {
|
connections.forEach((conn) => {
|
||||||
conn.updateFlair(userDB.flair);
|
conn.updateFlair(flairDB.name);
|
||||||
});
|
});
|
||||||
}catch(err){
|
}catch(err){
|
||||||
return loggerUtils.socketExceptionHandler(socket, err);
|
return loggerUtils.socketExceptionHandler(socket, err);
|
||||||
|
|
|
||||||
|
|
@ -39,27 +39,34 @@ const flairSchema = new mongoose.Schema({
|
||||||
});
|
});
|
||||||
|
|
||||||
flairSchema.statics.loadDefaults = async function(){
|
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
|
//For each entry in the defaultFlair.json file
|
||||||
defaultFlair.forEach(async (flair) => {
|
defaultFlair.array.forEach(registerFlair);
|
||||||
|
|
||||||
|
async function registerFlair(flair){
|
||||||
try{
|
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){
|
||||||
//if the flair doesn't exist
|
const flairDB = await _this.create(flair);
|
||||||
if(!foundFlair){
|
console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`);
|
||||||
const flairDB = await this.create(flair);
|
}
|
||||||
console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`);
|
|
||||||
}
|
|
||||||
|
|
||||||
}catch(err){
|
}catch(err){
|
||||||
if(flair != null){
|
if(flair != null){
|
||||||
|
console.log(err);
|
||||||
console.log(`Error loading flair '${flair.name}':`);
|
console.log(`Error loading flair '${flair.name}':`);
|
||||||
}else{
|
}else{
|
||||||
console.log("Error, null flair:");
|
console.log("Error, null flair:");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = mongoose.model("flair", flairSchema);
|
module.exports = mongoose.model("flair", flairSchema);
|
||||||
|
|
@ -78,9 +78,9 @@ const userSchema = new mongoose.Schema({
|
||||||
default: "Signature not set!"
|
default: "Signature not set!"
|
||||||
},
|
},
|
||||||
flair: {
|
flair: {
|
||||||
type: mongoose.SchemaTypes.String,
|
type: mongoose.SchemaTypes.ObjectID,
|
||||||
required: false,
|
default: null,
|
||||||
default: ""
|
ref: "flair"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -93,25 +93,24 @@ userSchema.pre('save', async function (next){
|
||||||
this.pass = hashUtil.hashPassword(this.pass);
|
this.pass = hashUtil.hashPassword(this.pass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//If the flair was changed
|
//If the flair was changed
|
||||||
if(this.isModified("flair")){
|
if(this.isModified("flair")){
|
||||||
//If we're not disabling flair
|
//Get flair properties
|
||||||
if(this.flair != ""){
|
await this.populate('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!");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(permissionModel.rankToNum(this.rank) < permissionModel.rankToNum(foundFlair.rank)){
|
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 '${foundFlair.displayName}'!`);
|
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")){
|
if(this.isModified("rank")){
|
||||||
await this.killAllSessions("Your site-wide rank has changed. Sign-in required.");
|
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;
|
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!
|
//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."){
|
userSchema.methods.killAllSessions = async function(reason = "A full log-out from all devices was requested for your account."){
|
||||||
//get authenticated sessions
|
//get authenticated sessions
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,7 @@ class chatBox{
|
||||||
var userLabel = document.createElement('p');
|
var userLabel = document.createElement('p');
|
||||||
userLabel.classList.add("chat-panel-buffer","chat-entry-username");
|
userLabel.classList.add("chat-panel-buffer","chat-entry-username");
|
||||||
|
|
||||||
if(chat.flair != ""){
|
if(chat.flair != "classic"){
|
||||||
var flair = `flair-${chat.flair}`;
|
var flair = `flair-${chat.flair}`;
|
||||||
}else{
|
}else{
|
||||||
var flair = this.client.userList.colorMap.get(chat.user);
|
var flair = this.client.userList.colorMap.get(chat.user);
|
||||||
|
|
@ -134,12 +134,6 @@ class chatBox{
|
||||||
//clear current flair select
|
//clear current flair select
|
||||||
this.flairSelect.innerHTML = "";
|
this.flairSelect.innerHTML = "";
|
||||||
|
|
||||||
//Inject flair object for standard flair like the hack we are
|
|
||||||
flairList.push({
|
|
||||||
name: "",
|
|
||||||
displayName: "Classic"
|
|
||||||
});
|
|
||||||
|
|
||||||
//For each flair in flairlist
|
//For each flair in flairlist
|
||||||
flairList.forEach((flair) => {
|
flairList.forEach((flair) => {
|
||||||
//Create an option
|
//Create an option
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ class userList{
|
||||||
userEntry.id = `user-entry-${user.user}`;
|
userEntry.id = `user-entry-${user.user}`;
|
||||||
|
|
||||||
//Override color with flair
|
//Override color with flair
|
||||||
if(user.flair != ""){
|
if(user.flair != "classic"){
|
||||||
color = `flair-${user.flair}`;
|
color = `flair-${user.flair}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue