Browse Source

fix(User List): fixed, only emits when required, user only ever counts as 1

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 years ago
parent
commit
aabe5202ee
2 changed files with 38 additions and 42 deletions
  1. 1 1
      backend/logic/actions/stations.js
  2. 37 41
      backend/logic/tasks.js

+ 1 - 1
backend/logic/actions/stations.js

@@ -17,7 +17,7 @@ const YouTubeModule = moduleManager.modules.youtube;
 CacheModule.runJob("SUB", {
 	channel: "station.updateUsers",
 	cb: stationId => {
-		const list = StationsModule.usersPerStation[stationId] || [];
+		const list = Object.values(StationsModule.usersPerStation[stationId]) || [];
 		IOModule.runJob("EMIT_TO_ROOM", {
 			room: `station.${stationId}`,
 			args: ["event:users.updated", list]

+ 37 - 41
backend/logic/tasks.js

@@ -351,8 +351,8 @@ class _TasksModule extends CoreClass {
 			const stationsCountUpdated = [];
 			const stationsUpdated = [];
 
-			// const oldUsersPerStation = JSON.parse(JSON.stringify(StationsModule.usersPerStation));
-			const usersPerStation = {};
+			const oldUsersPerStation = StationsModule.usersPerStation;
+			const usersPerStation = [];
 
 			const oldUsersPerStationCount = JSON.parse(JSON.stringify(StationsModule.usersPerStationCount));
 			const usersPerStationCount = {};
@@ -365,26 +365,26 @@ class _TasksModule extends CoreClass {
 
 						if (!socket || Object.keys(socket.rooms).indexOf(`station.${stationId}`) === -1) {
 							if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
-							if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
+							if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(String(stationId));
+
 							delete StationsModule.userList[socketId];
+
 							return next();
 						}
 
-						if (!usersPerStationCount[stationId]) usersPerStationCount[stationId] = 0;
-						usersPerStationCount[stationId] += 1;
+						if (!usersPerStationCount[stationId]) usersPerStationCount[stationId] = 0; // start count for station
 						if (!usersPerStation[stationId]) usersPerStation[stationId] = [];
 
 						return async.waterfall(
 							[
 								next => {
 									if (!socket.session || !socket.session.sessionId) return next("No session found.");
+
 									return CacheModule.runJob("HGET", {
 										table: "sessions",
 										key: socket.session.sessionId
 									})
-										.then(session => {
-											next(null, session);
-										})
+										.then(session => next(null, session))
 										.catch(next);
 								},
 
@@ -395,57 +395,53 @@ class _TasksModule extends CoreClass {
 
 								(user, next) => {
 									if (!user) return next("User not found.");
-									if (usersPerStation[stationId].indexOf(user.username) !== -1)
+
+									if (usersPerStation[stationId].some(u => user.username === u.username))
 										return next("User already in the list.");
+
+									usersPerStationCount[stationId] += 1; // increment user count for station
+
 									return next(null, { username: user.username, avatar: user.avatar });
 								}
 							],
 							(err, user) => {
-								if (!err) {
-									usersPerStation[stationId].push(user);
-								}
+								if (!err) usersPerStation[stationId].push(user);
 								next();
 							}
 						);
 					});
-					// TODO Code to show users
 				},
 				() => {
 					Object.keys(usersPerStationCount).forEach(stationId => {
-						if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
-							if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
+						if (
+							oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId] &&
+							stationsCountUpdated.indexOf(stationId) === -1
+						) {
+							this.log("INFO", "UPDATE_STATION_USER_COUNT", `Updating user count of ${stationId}.`);
+							CacheModule.runJob("PUB", {
+								channel: "station.updateUserCount",
+								value: stationId
+							});
 						}
 					});
 
-					// Object.keys(usersPerStation).forEach(stationId => {
-					// 	if (
-					// 		_.difference(usersPerStation[stationId], oldUsersPerStation[stationId]).length > 0 ||
-					// 		_.difference(oldUsersPerStation[stationId], usersPerStation[stationId]).length > 0
-					// 	) {
-					// 		if (stationsUpdated.indexOf(stationId) === -1) stationsUpdated.push(stationId);
-					// 	}
-					// });
+					Object.keys(usersPerStation).forEach(stationId => {
+						if (
+							!oldUsersPerStation[stationId] ||
+							JSON.stringify(oldUsersPerStation[stationId]) !==
+								JSON.stringify(usersPerStation[stationId]) ||
+							oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]
+						) {
+							this.log("INFO", "UPDATE_STATION_USER_LIST", `Updating user list of ${stationId}.`);
+							CacheModule.runJob("PUB", {
+								channel: "station.updateUsers",
+								value: stationId
+							});
+						}
+					});
 
 					StationsModule.usersPerStationCount = usersPerStationCount;
 					StationsModule.usersPerStation = usersPerStation;
-
-					stationsCountUpdated.forEach(stationId => {
-						this.log("INFO", "UPDATE_STATION_USER_COUNT", `Updating user count of ${stationId}.`);
-						CacheModule.runJob("PUB", {
-							channel: "station.updateUserCount",
-							value: stationId
-						});
-					});
-
-					stationsUpdated.forEach(stationId => {
-						this.log("INFO", "UPDATE_STATION_USER_LIST", `Updating user list of ${stationId}.`);
-						CacheModule.runJob("PUB", {
-							channel: "station.updateUsers",
-							value: stationId
-						});
-					});
-
-					// console.log("Userlist", StationsModule.usersPerStation);
 				}
 			);