Browse Source

Fixed various issues with homepage not updating when stations updated

Kristian Vos 3 years ago
parent
commit
2702e11fb7
3 changed files with 120 additions and 93 deletions
  1. 17 14
      backend/logic/actions/stations.js
  2. 91 77
      backend/logic/stations.js
  3. 12 2
      frontend/src/pages/Home.vue

+ 17 - 14
backend/logic/actions/stations.js

@@ -78,16 +78,6 @@ CacheModule.runJob("SUB", {
 	}
 });
 
-CacheModule.runJob("SUB", {
-	channel: "station.updateTheme",
-	cb: data => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${data.stationId}`,
-			args: ["event:theme.updated", { data: { theme: data.theme } }]
-		});
-	}
-});
-
 CacheModule.runJob("SUB", {
 	channel: "station.queueLockToggled",
 	cb: data => {
@@ -101,9 +91,22 @@ CacheModule.runJob("SUB", {
 CacheModule.runJob("SUB", {
 	channel: "station.updatePartyMode",
 	cb: data => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${data.stationId}`,
-			args: ["event:partyMode.updated", { data: { partyMode: data.partyMode } }]
+		const { stationId, partyMode } = data;
+		StationsModule.runJob("GET_STATION", { stationId }).then(station => {
+			WSModule.runJob("EMIT_TO_ROOM", {
+				room: `station.${stationId}`,
+				args: ["event:partyMode.updated", { data: { partyMode } }]
+			});
+
+			StationsModule.runJob("GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION", {
+				room: `home`,
+				station
+			}).then(response => {
+				const { socketsThatCan } = response;
+				socketsThatCan.forEach(socket => {
+					socket.dispatch("event:station.updatePartyMode", { data: { stationId, partyMode } });
+				});
+			});
 		});
 	}
 });
@@ -313,7 +316,7 @@ CacheModule.runJob("SUB", {
 			}).then(res => {
 				const { socketsThatCan } = res;
 				socketsThatCan.forEach(socket => {
-					socket.dispatch("event:station.themeUpdated", { data: { theme: station.theme } });
+					socket.dispatch("event:station.updateTheme", { data: { stationId, theme: station.theme } });
 				});
 			});
 		});

+ 91 - 77
backend/logic/stations.js

@@ -1152,94 +1152,108 @@ class _StationsModule extends CoreClass {
 	GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION(payload) {
 		return new Promise((resolve, reject) => {
 			WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: payload.room }, this)
-				.then(sockets => {
-					let socketsThatCan = [];
-					const socketsThatCannot = [];
-
-					if (payload.station.privacy === "public") {
-						socketsThatCan = sockets;
-						resolve({ socketsThatCan, socketsThatCannot });
-					} else {
-						async.eachLimit(
-							sockets,
-							1,
-							(socketId, next) => {
-								WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this).then(socket => {
-									const { session } = socket;
-
-									async.waterfall(
-										[
-											next => {
-												if (!session.sessionId) next("No session id");
-												else next();
-											},
+				.then(socketIds => {
+					const sockets = [];
+					async.eachLimit(
+						socketIds,
+						1,
+						(socketId, next) => {
+							WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this).then(socket => {
+								sockets.push(socket);
+								next();
+							}).catch(err => {
+								reject(err);
+							});
+						},
+						err => {
+							if (err) reject(err);
+							else {
+								let socketsThatCan = [];
+								const socketsThatCannot = [];
+
+								if (payload.station.privacy === "public") {
+									socketsThatCan = sockets;
+									resolve({ socketsThatCan, socketsThatCannot });
+								} else {
+									async.eachLimit(
+										sockets,
+										1,
+										(socket, next) => {
+											const { session } = socket;
+
+											async.waterfall(
+												[
+													next => {
+														if (!session.sessionId) next("No session id");
+														else next();
+													},
 
-											next => {
-												CacheModule.runJob(
-													"HGET",
-													{
-														table: "sessions",
-														key: session.sessionId
+													next => {
+														CacheModule.runJob(
+															"HGET",
+															{
+																table: "sessions",
+																key: session.sessionId
+															},
+															this
+														)
+															.then(response => {
+																next(null, response);
+															})
+															.catch(next);
 													},
-													this
-												)
-													.then(response => {
-														next(null, response);
-													})
-													.catch(next);
-											},
 
-											(session, next) => {
-												if (!session) next("No session");
-												else {
-													DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
-														.then(userModel => {
-															next(null, userModel);
-														})
-														.catch(next);
-												}
-											},
+													(session, next) => {
+														if (!session) next("No session");
+														else {
+															DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
+																.then(userModel => {
+																	next(null, userModel);
+																})
+																.catch(next);
+														}
+													},
 
-											(userModel, next) => {
-												if (!userModel) next("No user model");
-												else
-													userModel.findOne(
-														{
-															_id: session.userId
-														},
-														next
-													);
-											},
+													(userModel, next) => {
+														if (!userModel) next("No user model");
+														else
+															userModel.findOne(
+																{
+																	_id: session.userId
+																},
+																next
+															);
+													},
 
-											(user, next) => {
-												if (!user) next("No user found");
-												else if (user.role === "admin") {
-													socketsThatCan.push(socket);
-													next();
-												} else if (
-													payload.station.type === "community" &&
-													payload.station.owner === session.userId
-												) {
-													socketsThatCan.push(socket);
+													(user, next) => {
+														if (!user) next("No user found");
+														else if (user.role === "admin") {
+															socketsThatCan.push(socket);
+															next();
+														} else if (
+															payload.station.type === "community" &&
+															payload.station.owner === session.userId
+														) {
+															socketsThatCan.push(socket);
+															next();
+														}
+													}
+												],
+												err => {
+													if (err) socketsThatCannot.push(socket);
 													next();
 												}
-											}
-										],
+											);
+										},
 										err => {
-											if (err) socketsThatCannot.push(socket);
-											next();
+											if (err) reject(err);
+											else resolve({ socketsThatCan, socketsThatCannot });
 										}
 									);
-								}).catch(err => {
-									next(err);
-								});
-							},
-							err => {
-								if (err) reject(err);
-								else resolve({ socketsThatCan, socketsThatCannot });
+								}
 							}
-						);
-					}
+						}
+					);
 				})
 				.catch(reject);
 		});

+ 12 - 2
frontend/src/pages/Home.vue

@@ -597,11 +597,21 @@ export default {
 		});
 
 		this.socket.on("event:station.updateTheme", res => {
+			const { stationId, theme } = res.data;
 			const station = this.stations.find(
-				station => station._id === res.stationId
+				station => station._id === stationId
+			);
+
+			if (station) station.theme = theme;
+		});
+
+		this.socket.on("event:station.updatePartyMode", res => {
+			const { stationId, partyMode } = res.data;
+			const station = this.stations.find(
+				station => station._id === stationId
 			);
 
-			if (station) station.theme = res.theme;
+			if (station) station.partyMode = partyMode;
 		});
 
 		this.socket.on("event:station.nextSong", res => {