Started work on flair

This commit is contained in:
rainbownapkin 2024-11-19 22:07:24 -05:00
parent 0fcd72b063
commit 4b4cb2ed3d
9 changed files with 149 additions and 10 deletions

10
defaultFlair.json Normal file
View file

@ -0,0 +1,10 @@
[
{
"name": "glitter",
"rank": "gold"
},
{
"name": "negativeGlitter",
"ranke": "gold"
}
]

View file

@ -16,6 +16,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//Local Imports //Local Imports
const channelModel = require('../../schemas/channelSchema'); const channelModel = require('../../schemas/channelSchema');
const flairModel = require('../../schemas/flairSchema');
const userModel = require('../../schemas/userSchema');
const activeChannel = require('./activeChannel'); const activeChannel = require('./activeChannel');
const chatHandler = require('./chatHandler'); const chatHandler = require('./chatHandler');
@ -26,8 +28,17 @@ module.exports.handleConnection = async function(io, socket){
//Prevent logged out connections and authenticate socket //Prevent logged out connections and authenticate socket
if(socket.request.session.user != null){ if(socket.request.session.user != null){
try{ try{
//Find the user in the Database since the session won't store enough data to fulfill our needs :P
const userDB = await userModel.findOne({user: socket.request.session.user.user});
//Set socket user and channel values //Set socket user and channel values
socket.user = socket.request.session.user; socket.user = {
id: userDB.id,
user: userDB.user,
rank: userDB.rank,
flair: userDB.flair
};
socket.chanName = socket.handshake.headers.referer.split('/c/')[1]; socket.chanName = socket.handshake.headers.referer.split('/c/')[1];
//Check if channel exists //Check if channel exists

View file

@ -16,9 +16,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//NPM Imports //NPM Imports
const validator = require('validator');//No express here, so regular validator it is! const validator = require('validator');//No express here, so regular validator it is!
const loggerUtils = require('../../utils/loggerUtils');
const userModel = require('../../schemas/userSchema');
module.exports.defineListeners = function(io, socket){ module.exports.defineListeners = function(io, socket){
socket.on("chat-message", (data) => { socket.on("chatMessage", (data) => {
//Trim and Sanatize for XSS //Trim and Sanatize for XSS
const msg = validator.trim(validator.escape(data.msg)); const msg = validator.trim(validator.escape(data.msg));
//make sure high is an int //make sure high is an int
@ -34,6 +36,22 @@ module.exports.defineListeners = function(io, socket){
return; return;
} }
io.in(socket.chan).emit("chat-message", {user: socket.user.user, msg, high}); io.in(socket.chan).emit("chatMessage", {user: socket.user.user, flair: socket.user.flair, high, msg});
});
socket.on("setFlair", async (data) => {
const userDB = await userModel.findOne({user: socket.user.user});
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;
await userDB.save();
}catch(err){
return loggerUtils.socketExceptionHandler(socket, err);
}
}
}); });
} }

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

View file

@ -19,8 +19,9 @@ const {mongoose} = require('mongoose');
//local imports //local imports
const server = require('../server.js'); const server = require('../server.js');
const statSchema = require('./statSchema.js'); const statModel = require('./statSchema.js');
const permissionSchema = require('./permissionSchema.js'); const flairModel = require('./flairSchema.js');
const permissionModel = require('./permissionSchema.js');
const hashUtil = require('../utils/hashUtils'); const hashUtil = require('../utils/hashUtils');
@ -48,7 +49,7 @@ const userSchema = new mongoose.Schema({
rank: { rank: {
type: mongoose.SchemaTypes.String, type: mongoose.SchemaTypes.String,
required: true, required: true,
enum: permissionSchema.rankEnum, enum: permissionModel.rankEnum,
default: "user" default: "user"
}, },
tokes: { tokes: {
@ -75,15 +76,39 @@ const userSchema = new mongoose.Schema({
type: mongoose.SchemaTypes.String, type: mongoose.SchemaTypes.String,
required: true, required: true,
default: "Signature not set!" 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! //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){ userSchema.pre('save', async function (next){
//If the password was changed
if(this.isModified("pass")){ if(this.isModified("pass")){
//Hash that sunnovabitch, no questions asked.
this.pass = hashUtil.hashPassword(this.pass); 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(); next();
}); });
@ -97,7 +122,7 @@ userSchema.statics.register = async function(userObj){
if(userDB){ if(userDB){
throw new Error("User name/email already taken!"); throw new Error("User name/email already taken!");
}else{ }else{
const id = await statSchema.incrementUserCount(); const id = await statModel.incrementUserCount();
const newUser = await this.create({id, user, pass, email}); const newUser = await this.create({id, user, pass, email});
} }
}else{ }else{

View file

@ -25,6 +25,7 @@ const mongoose = require('mongoose');
//Define Local Imports //Define Local Imports
const statModel = require('./schemas/statSchema'); const statModel = require('./schemas/statSchema');
const flairModel = require('./schemas/flairSchema');
const channelManager = require('./app/channel/channelManager'); const channelManager = require('./app/channel/channelManager');
const indexRouter = require('./routers/indexRouter'); const indexRouter = require('./routers/indexRouter');
const registerRouter = require('./routers/registerRouter'); const registerRouter = require('./routers/registerRouter');
@ -105,6 +106,9 @@ app.use(express.static(path.join(__dirname, '../www')));
//Increment launch counter //Increment launch counter
statModel.incrementLaunchCount(); statModel.incrementLaunchCount();
//Load flairs
flairModel.loadDefaults();
//Hand over general-namespace socket.io connections to the channel manager //Hand over general-namespace socket.io connections to the channel manager
io.on("connection", (socket) => {channelManager.handleConnection(io, socket)} ); io.on("connection", (socket) => {channelManager.handleConnection(io, socket)} );

View file

@ -20,3 +20,8 @@ module.exports.exceptionHandler = function(res, err){
res.status(400); res.status(400);
return res.send({errors: [{type: "Caught Exception", msg: err.message, date: new Date()}]}); return res.send({errors: [{type: "Caught Exception", msg: err.message, date: new Date()}]});
} }
module.exports.socketExceptionHandler = function(socket, err){
//if not yell at the browser for fucking up, and tell it what it did wrong.
return socket.emit("error", {errors: [{type: "Caught Exception", msg: err.message, date: new Date()}]});
}

View file

@ -42,6 +42,10 @@ class channel{
this.socket.on("connect", () => { this.socket.on("connect", () => {
document.title = `${this.channelName} - Connected` document.title = `${this.channelName} - Connected`
}); });
this.socket.on("error", (data) => {
console.log(data);
});
} }
} }

View file

@ -64,7 +64,7 @@ class chatBox{
} }
defineListeners(){ defineListeners(){
this.client.socket.on("chat-message", (data) => { this.client.socket.on("chatMessage", (data) => {
this.displayChat(data); this.displayChat(data);
}); });
} }
@ -147,7 +147,7 @@ class chatBox{
async send(event){ async send(event){
if((!event || !event.key || event.key == "Enter") && this.chatPrompt.value){ if((!event || !event.key || event.key == "Enter") && this.chatPrompt.value){
this.client.socket.emit("chat-message",{msg: this.chatPrompt.value, high: this.highLevel.value}); this.client.socket.emit("chatMessage",{msg: this.chatPrompt.value, high: this.highLevel.value});
this.chatPrompt.value = ""; this.chatPrompt.value = "";
} }
} }