Created connectedUser.js, cleaned activeChannel.js
This commit is contained in:
parent
8384e732f3
commit
68f9d5af06
4 changed files with 109 additions and 55 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue