Pārlūkot izejas kodu

Worked on userlist.

KrisVos130 8 gadi atpakaļ
vecāks
revīzija
df5676dfc8
2 mainītis faili ar 84 papildinājumiem un 2 dzēšanām
  1. 69 0
      backend/logic/actions/stations.js
  2. 15 2
      frontend/components/pages/Home.vue

+ 69 - 0
backend/logic/actions/stations.js

@@ -13,6 +13,70 @@ const logger = require('../logger');
 const stations = require('../stations');
 const songs = require('../songs');
 const hooks = require('./hooks');
+let userList = {};
+let usersPerStation = {};
+let usersPerStationCount = {};
+
+setInterval(() => {
+	let stationsUpdated = [];
+	let oldUsersPerStation = usersPerStation;
+	usersPerStation = {};
+	let oldUsersPerStationCount = usersPerStationCount;
+	usersPerStationCount = {};
+	for (let socketId in userList) {
+		let socket = utils.socketFromSession(socketId);
+		let stationId = userList[socketId];
+		console.log(socketId, !socket, stationId);
+		if (!socket || Object.keys(socket.rooms).indexOf(`station.${stationId}`) === -1) {
+			if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
+			console.log(12222333, userList);
+			return delete userList[socketId];
+		}
+		if (!usersPerStationCount[stationId]) usersPerStationCount[stationId] = 0;
+		if (!usersPerStation[stationId]) usersPerStation[stationId] = [];
+		usersPerStationCount[stationId]++;
+		//TODO Code to show users
+	}
+	for (let stationId in usersPerStationCount) {
+		if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
+			if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
+		}
+	}
+	stationsUpdated.forEach((stationId) => {
+		console.log("Updating ", stationId);
+		cache.pub('station.updateUserCount', stationId);
+	});
+
+	console.log("Userlist", userList, usersPerStationCount);
+}, 3000);
+
+cache.sub('station.updateUserCount', stationId => {
+	let count = usersPerStationCount[stationId] | 0;
+	console.log(12321, count, usersPerStationCount);
+	utils.emitToRoom(`station.${stationId}`, "event:userCount.updated", count);
+	stations.getStation(stationId, (err, station) => {
+		console.log(421123);
+		if (station.privacy === 'public') utils.emitToRoom('home', "event:userCount.updated", stationId, count);
+		else {
+			console.log(42112345345);
+			let sockets = utils.getRoomSockets('home');
+			for (let socketId in sockets) {
+				let socket = sockets[socketId];
+				let session = sockets[socketId].session;
+				if (session.sessionId) {
+					cache.hget('sessions', session.sessionId, (err, session) => {
+						if (!err && session) {
+							db.models.user.findOne({_id: session.userId}, (err, user) => {
+								if (user.role === 'admin') socket.emit("event:userCount.updated", stationId, count);
+								else if (station.type === "community" && station.owner === session.userId) socket.emit("event:userCount.updated", stationId, count);
+							});
+						}
+					});
+				}
+			}
+		}
+	})
+});
 
 cache.sub('station.updatePartyMode', data => {
 	utils.emitToRoom(`station.${data.stationId}`, "event:partyMode.updated", data.partyMode);
@@ -48,6 +112,7 @@ cache.sub('station.remove', stationId => {
 
 cache.sub('station.create', stationId => {
 	stations.initializeStation(stationId, (err, station) => {
+		station.userCount = usersPerStationCount[stationId] | 0;
 		if (err) console.error(err);
 		utils.emitToRoom('admin.stations', 'event:admin.station.added', station);
 		// TODO If community, check if on whitelist
@@ -118,6 +183,7 @@ module.exports = {
 							next(`Insufficient permissions.`);
 						}
 					], (err) => {
+						station.userCount = usersPerStationCount[station._id] | 0;
 						if (err === true) resultStations.push(station);
 						next();
 					});
@@ -258,6 +324,7 @@ module.exports = {
 					owner: station.owner,
 					privatePlaylist: station.privatePlaylist
 				};
+				userList[session.socketId] = station._id;
 				next(null, data);
 			},
 
@@ -275,6 +342,7 @@ module.exports = {
 					}
 					next(null, data);
 				});
+				data.userCount = usersPerStationCount[station._id] | 0;
 			}
 		], (err, data) => {
 			if (err) {
@@ -393,6 +461,7 @@ module.exports = {
 			}
 			logger.success("STATIONS_LEAVE", `Left station "${stationId}" successfully.`);
 			utils.socketLeaveRooms(session);
+			delete userList[session.socketId];
 			return cb({'status': 'success', 'message': 'Successfully left station.', userCount});
 		});
 	},

+ 15 - 2
frontend/components/pages/Home.vue

@@ -12,7 +12,7 @@
 				<div class="card-content">
 					<div class="media">
 						<div class="media-left displayName">
-							<h5>{{ station.displayName }}</h5>
+							<h5>{{ station.displayName }} - {{station.userCount}}</h5>
 						</div>
 						<div class="media-right">
 							<div v-if="station.privacy !== 'public'" title="This station is not visible to other users." class="station-status">
@@ -43,7 +43,7 @@
 				<div class="card-content">
 					<div class="media">
 						<div class="media-left displayName">
-							<h5>{{ station.displayName }}</h5>
+							<h5>{{ station.displayName }} - {{station.userCount}}</h5>
 						</div>
 						<div class="media-right">
 							<div v-if="station.privacy !== 'public'" title="This station is not visible to other users." class="station-status">
@@ -103,6 +103,19 @@
 						if (station.currentSong && !station.currentSong.thumbnail) station.currentSong.thumbnail = "/assets/notes-transparent.png";
 						_this.stations[station.type].push(station);
 					});
+					_this.socket.on('event:userCount.updated', (stationId, userCount) => {
+						_this.stations.official.forEach((station) => {
+							if (station._id === stationId) {
+								station.userCount = userCount;
+							}
+						});
+
+						_this.stations.community.forEach((station) => {
+							if (station._id === stationId) {
+								station.userCount = userCount;
+							}
+						});
+					});
 					_this.socket.on('event:station.nextSong', (stationId, newSong) => {
 						_this.stations.official.forEach((station) => {
 							if (station._id === stationId) {