canopy/src/app/channel/connectedUser.js

90 lines
3 KiB
JavaScript

/*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, 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];
}
socketCrawl(cb){
//Crawl through user's sockets (lol)
this.sockets.forEach((sockid) => {
//get socket based on ID
const socket = this.channel.server.io.sockets.sockets.get(sockid);
//Callback with socket
cb(socket);
});
}
emit(eventName, args){
this.socketCrawl((socket)=>{socket.emit(eventName, args)});
}
//generic disconnect function, defaults to kick
disconnect(reason, type = "kick"){
this.emit("kick",{type, reason});
this.socketCrawl((socket)=>{socket.disconnect()});
}
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();
}
}