Started work on flair
This commit is contained in:
parent
0fcd72b063
commit
4b4cb2ed3d
9 changed files with 149 additions and 10 deletions
62
src/schemas/flairSchema.js
Normal file
62
src/schemas/flairSchema.js
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*Canopy - The next generation of stoner streaming software
|
||||
Copyright (C) 2024 Rainbownapkin and the TTN Community
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||
|
||||
//NPM Imports
|
||||
const {mongoose} = require('mongoose');
|
||||
|
||||
//Local Imports
|
||||
const permissionModel = require("./permissionSchema");
|
||||
const defaultFlair = require("../../defaultFlair.json");
|
||||
|
||||
const flairSchema = new mongoose.Schema({
|
||||
name:{
|
||||
type: mongoose.SchemaTypes.String,
|
||||
required: true
|
||||
},
|
||||
rank: {
|
||||
type: mongoose.SchemaTypes.String,
|
||||
enum: permissionModel.rankEnum,
|
||||
default: "user",
|
||||
required: true
|
||||
}
|
||||
});
|
||||
|
||||
flairSchema.statics.loadDefaults = async function(){
|
||||
//For each entry in the defaultFlair.json file
|
||||
defaultFlair.forEach(async (flair) => {
|
||||
try{
|
||||
//Look for flair matching the one from our file
|
||||
|
||||
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`);
|
||||
}
|
||||
|
||||
}catch(err){
|
||||
if(flair != null){
|
||||
console.log(`Error loading flair '${flair.name}':`);
|
||||
}else{
|
||||
console.log("Error, null flair:");
|
||||
}
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("flair", flairSchema);
|
||||
|
|
@ -19,8 +19,9 @@ const {mongoose} = require('mongoose');
|
|||
|
||||
//local imports
|
||||
const server = require('../server.js');
|
||||
const statSchema = require('./statSchema.js');
|
||||
const permissionSchema = require('./permissionSchema.js');
|
||||
const statModel = require('./statSchema.js');
|
||||
const flairModel = require('./flairSchema.js');
|
||||
const permissionModel = require('./permissionSchema.js');
|
||||
const hashUtil = require('../utils/hashUtils');
|
||||
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ const userSchema = new mongoose.Schema({
|
|||
rank: {
|
||||
type: mongoose.SchemaTypes.String,
|
||||
required: true,
|
||||
enum: permissionSchema.rankEnum,
|
||||
enum: permissionModel.rankEnum,
|
||||
default: "user"
|
||||
},
|
||||
tokes: {
|
||||
|
|
@ -75,15 +76,39 @@ const userSchema = new mongoose.Schema({
|
|||
type: mongoose.SchemaTypes.String,
|
||||
required: true,
|
||||
default: "Signature not set!"
|
||||
}
|
||||
},
|
||||
flair: {
|
||||
type: mongoose.SchemaTypes.String,
|
||||
required: false,
|
||||
default: ""
|
||||
}
|
||||
});
|
||||
|
||||
//This is one of those places where you really DON'T want to use an arrow function over an anonymous one!
|
||||
userSchema.pre('save', async function (next){
|
||||
|
||||
//If the password was changed
|
||||
if(this.isModified("pass")){
|
||||
//Hash that sunnovabitch, no questions asked.
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//All is good, continue on saving.
|
||||
next();
|
||||
});
|
||||
|
||||
|
|
@ -97,7 +122,7 @@ userSchema.statics.register = async function(userObj){
|
|||
if(userDB){
|
||||
throw new Error("User name/email already taken!");
|
||||
}else{
|
||||
const id = await statSchema.incrementUserCount();
|
||||
const id = await statModel.incrementUserCount();
|
||||
const newUser = await this.create({id, user, pass, email});
|
||||
}
|
||||
}else{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue