Created connectedUser.js, cleaned activeChannel.js
This commit is contained in:
parent
8384e732f3
commit
68f9d5af06
|
|
@ -15,6 +15,7 @@ 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/>.*/
|
||||
|
||||
//local imports
|
||||
const connectedUser = require('./connectedUser');
|
||||
const flairModel = require('../../schemas/flairSchema');
|
||||
const permissionModel = require('../../schemas/permissionSchema');
|
||||
|
||||
|
|
@ -22,6 +23,7 @@ module.exports = class{
|
|||
constructor(server, name){
|
||||
this.server = server;
|
||||
this.name = name;
|
||||
//Keeping these in a map was originally a vestige but it's more preformant than an array or object so :P
|
||||
this.userList = new Map();
|
||||
}
|
||||
|
||||
|
|
@ -34,13 +36,7 @@ module.exports = class{
|
|||
//Add this socket on to the userobject
|
||||
userObj.sockets.push(socket.id);
|
||||
}else{
|
||||
//if this is the first connection, initialize the userObject w/ the current socked id
|
||||
userObj = {
|
||||
id: userDB.id,
|
||||
rank: userDB.rank,
|
||||
flair: userDB.flair,
|
||||
sockets: [socket.id]
|
||||
}
|
||||
userObj = new connectedUser(userDB.user, userDB.id, userDB.rank, userDB.flair, this, socket);
|
||||
}
|
||||
|
||||
//Set user entry in userlist
|
||||
|
|
@ -50,7 +46,8 @@ module.exports = class{
|
|||
socket.join(socket.chan);
|
||||
|
||||
//Make sure the client receives important client-info before userlist
|
||||
await this.sendClientMetadata(userDB, socket);
|
||||
//await this.sendClientMetadata(userDB, socket);
|
||||
await userObj.sendClientMetadata();
|
||||
|
||||
//Send out the userlist
|
||||
this.broadcastUserList(socket.chan);
|
||||
|
|
@ -78,29 +75,6 @@ module.exports = class{
|
|||
this.broadcastUserList(socket.chan);
|
||||
}
|
||||
|
||||
async sendClientMetadata(userObj, socket){
|
||||
const flairListDB = await flairModel.find({});
|
||||
var flairList = [];
|
||||
|
||||
const user = {
|
||||
id: userObj.id,
|
||||
user: userObj.user,
|
||||
rank: userObj.rank,
|
||||
flair: userObj.flair
|
||||
}
|
||||
|
||||
flairListDB.forEach((flair)=>{
|
||||
if(permissionModel.rankToNum(flair.rank) <= permissionModel.rankToNum(userObj.rank)){
|
||||
flairList.push({
|
||||
name: flair.name,
|
||||
displayName: flair.displayName
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
socket.emit("clientMetadata", {user, flairList});
|
||||
}
|
||||
|
||||
broadcastUserList(){
|
||||
var userList = [];
|
||||
|
||||
|
|
@ -113,24 +87,4 @@ module.exports = class{
|
|||
|
||||
this.server.io.in(this.name).emit("userList", userList);
|
||||
}
|
||||
|
||||
updateFlair(user, flair){
|
||||
const userObj = this.userList.get(user);
|
||||
|
||||
userObj.flair = flair;
|
||||
|
||||
this.userList.set(user, userObj);
|
||||
|
||||
//Quick hack to make this compatible with sendClientMetadata. make sure we do this AFTER setting the object in the userList map...
|
||||
userObj.name = user;
|
||||
|
||||
//Crawl through user's sockets (lol)
|
||||
userObj.sockets.forEach((sockid) => {
|
||||
//Send metadata to each one
|
||||
const socket = this.server.io.sockets.sockets.get(sockid);
|
||||
this.sendClientMetadata(userObj, socket);
|
||||
});
|
||||
|
||||
this.broadcastUserList();
|
||||
}
|
||||
}
|
||||
|
|
@ -118,9 +118,12 @@ module.exports = class{
|
|||
}
|
||||
|
||||
getConnectedChannels(socket){
|
||||
//Create a list to hold connected channels
|
||||
var chanList = [];
|
||||
|
||||
//For each channel
|
||||
this.activeChannels.forEach((channel) => {
|
||||
//Check and see if the user is connected
|
||||
const foundUser = channel.userList.get(socket.user.user);
|
||||
|
||||
//If we found a user and this channel hasn't been added to the list
|
||||
|
|
@ -129,6 +132,25 @@ module.exports = class{
|
|||
}
|
||||
});
|
||||
|
||||
//return the channels this user is connected to
|
||||
return chanList;
|
||||
}
|
||||
|
||||
getConnections(socket){
|
||||
//Create a list to store our connections
|
||||
var connections = [];
|
||||
|
||||
//For each channel
|
||||
this.activeChannels.forEach((channel) => {
|
||||
//Check and see if the user is connected
|
||||
const foundUser = channel.userList.get(socket.user.user);
|
||||
|
||||
//If we found a user and this channel hasn't been added to the list
|
||||
if(foundUser){
|
||||
connections.push(foundUser);
|
||||
}
|
||||
});
|
||||
|
||||
return connections;
|
||||
}
|
||||
}
|
||||
|
|
@ -62,11 +62,11 @@ module.exports = class{
|
|||
userDB.flair = data.flair;
|
||||
userDB = await userDB.save();
|
||||
|
||||
chanList.forEach((channel) => {
|
||||
channel.updateFlair(userDB.user, userDB.flair);
|
||||
const connections = this.server.getConnections(socket);
|
||||
|
||||
connections.forEach((conn) => {
|
||||
conn.updateFlair(userDB.flair);
|
||||
});
|
||||
|
||||
|
||||
}catch(err){
|
||||
return loggerUtils.socketExceptionHandler(socket, err);
|
||||
}
|
||||
|
|
|
|||
78
src/app/channel/connectedUser.js
Normal file
78
src/app/channel/connectedUser.js
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
/*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/>.*/
|
||||
|
||||
//local imports
|
||||
const flairModel = require('../../schemas/flairSchema');
|
||||
const permissionModel = require('../../schemas/permissionSchema');
|
||||
|
||||
module.exports = class{
|
||||
constructor(id, name, rank, flair, channel, socket){
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.rank = rank;
|
||||
this.flair = flair;
|
||||
this.channel = channel;
|
||||
this.sockets = [socket.id];
|
||||
}
|
||||
|
||||
emit(eventName, args){
|
||||
//Crawl through user's sockets (lol)
|
||||
this.sockets.forEach((sockid) => {
|
||||
//Send event out to each one
|
||||
const socket = this.channel.server.io.sockets.sockets.get(sockid);
|
||||
socket.emit(eventName, args);
|
||||
});
|
||||
}
|
||||
|
||||
async sendClientMetadata(){
|
||||
//Get flairList from DB and setup flairList array
|
||||
const flairListDB = await flairModel.find({});
|
||||
var flairList = [];
|
||||
|
||||
//Setup our userObj
|
||||
const userObj = {
|
||||
id: this.id,
|
||||
user: this.user,
|
||||
rank: this.rank,
|
||||
flair: this.flair
|
||||
}
|
||||
|
||||
//For each flair listed in the Database
|
||||
flairListDB.forEach((flair)=>{
|
||||
//Check if the user has permission to use the current flair
|
||||
if(permissionModel.rankToNum(flair.rank) <= permissionModel.rankToNum(this.rank)){
|
||||
//If so push a light version of the flair object into our final flair list
|
||||
flairList.push({
|
||||
name: flair.name,
|
||||
displayName: flair.displayName
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//Send off the metadata to our user's clients
|
||||
this.emit("clientMetadata", {user: userObj, flairList});
|
||||
}
|
||||
|
||||
updateFlair(flair){
|
||||
//This will run multiple times in a row, we don't need to broadcast the userlist every time
|
||||
if(this.flair != flair){
|
||||
this.flair = flair;
|
||||
this.channel.broadcastUserList();
|
||||
}
|
||||
|
||||
this.sendClientMetadata();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue