Channel Rank/Auth base backend functional

This commit is contained in:
rainbow napkin 2024-11-25 00:44:07 -05:00
parent 057537341a
commit 61fab57a6d
12 changed files with 318 additions and 83 deletions

View file

@ -27,16 +27,18 @@ module.exports = class{
this.userList = new Map();
}
async handleConnection(userDB, socket){
async handleConnection(userDB, chanDB, socket){
//get current user object from the userlist
var userObj = this.userList.get(userDB.user);
//get channel rank for current user
const chanRank = await chanDB.getChannelRankByUserDoc(userDB);
//If user is already connected
if(userObj){
//Add this socket on to the userobject
userObj.sockets.push(socket.id);
}else{
userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, userDB.flair, this, socket);
userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, chanRank, userDB.flair, this, socket);
}
//Set user entry in userlist
@ -49,6 +51,8 @@ module.exports = class{
//await this.sendClientMetadata(userDB, socket);
await userObj.sendClientMetadata();
console.log(userObj);
//Send out the userlist
this.broadcastUserList(socket.chan);
}

View file

@ -45,14 +45,15 @@ module.exports = class{
const userDB = await this.authSocket(socket);
//Get the active channel based on the socket
var activeChan = await this.getActiveChan(socket);
var {activeChan, chanDB} = await this.getActiveChan(socket);
//Define listeners
this.defineListeners(socket);
this.chatHandler.defineListeners(socket);
//Connect the socket to it's given channel
activeChan.handleConnection(userDB, socket);
//Lil' hacky to pass chanDB like that, but why double up on DB calls?
activeChan.handleConnection(userDB, chanDB, socket);
}catch(err){
//Flip a table if something fucks up
return loggerUtils.socketCriticalExceptionHandler(socket, err);
@ -83,10 +84,11 @@ module.exports = class{
async getActiveChan(socket){
socket.chan = socket.handshake.headers.referer.split('/c/')[1];
const chanDB = (await channelModel.findOne({name: socket.chan}))
//Check if channel exists
if(await channelModel.findOne({name: socket.chan}) == null){
throw new Error("Channel not found!")
if(chanDB == null){
throw new Error("Channel not found!");
}
//Check if current channel is active
@ -99,7 +101,8 @@ module.exports = class{
}
//Return whatever the active channel is (new or old)
return activeChan;
return {activeChan, chanDB};
//return activeChan;
}
defineListeners(socket){

View file

@ -19,10 +19,11 @@ const flairModel = require('../../schemas/flairSchema');
const permissionModel = require('../../schemas/permissionSchema');
module.exports = class{
constructor(id, name, rank, flair, channel, socket){
constructor(id, name, rank, chanRank, flair, channel, socket){
this.id = id;
this.name = name;
this.rank = rank;
this.chanRank = chanRank;
this.flair = flair;
this.channel = channel;
this.sockets = [socket.id];