Browse Source

feat(WS): added backend function to emit to multiple rooms

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 3 years ago
parent
commit
8e70c6ac42
4 changed files with 76 additions and 124 deletions
  1. 10 32
      backend/logic/actions/reports.js
  2. 15 30
      backend/logic/actions/songs.js
  3. 22 62
      backend/logic/actions/stations.js
  4. 29 0
      backend/logic/ws.js

+ 10 - 32
backend/logic/actions/reports.js

@@ -13,37 +13,20 @@ const ActivitiesModule = moduleManager.modules.activities;
 
 CacheModule.runJob("SUB", {
 	channel: "report.issue.toggle",
-	cb: data => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `edit-song.${data.songId}`,
+	cb: data =>
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`edit-song.${data.songId}`, `view-report.${data.reportId}`],
 			args: ["event:admin.report.issue.toggled", { data: { issueId: data.issueId, reportId: data.reportId } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `view-report.${data.reportId}`,
-			args: ["event:admin.report.issue.toggled", { data: { issueId: data.issueId, reportId: data.reportId } }]
-		});
-	}
+		})
 });
 
 CacheModule.runJob("SUB", {
 	channel: "report.resolve",
-	cb: ({ reportId, songId }) => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: "admin.reports",
-			args: ["event:admin.report.resolved", { data: { reportId } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `edit-song.${songId}`,
+	cb: ({ reportId, songId }) =>
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: ["admin.reports", `edit-song.${songId}`, `view-report.${reportId}`],
 			args: ["event:admin.report.resolved", { data: { reportId } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `view-report.${reportId}`,
-			args: ["event:admin.report.resolved", { data: { reportId } }]
-		});
-	}
+		})
 });
 
 CacheModule.runJob("SUB", {
@@ -63,13 +46,8 @@ CacheModule.runJob("SUB", {
 						_id: report.createdBy
 					};
 
-					WSModule.runJob("EMIT_TO_ROOM", {
-						room: "admin.reports",
-						args: ["event:admin.report.created", { data: { report } }]
-					});
-
-					WSModule.runJob("EMIT_TO_ROOM", {
-						room: `edit-song.${report.song._id}`,
+					WSModule.runJob("EMIT_TO_ROOMS", {
+						rooms: ["admin.reports", `edit-song.${report.song._id}`],
 						args: ["event:admin.report.created", { data: { report } }]
 					});
 				});

+ 15 - 30
backend/logic/actions/songs.js

@@ -20,17 +20,12 @@ CacheModule.runJob("SUB", {
 			modelName: "song"
 		});
 
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.unverifiedSongs",
+		songModel.findOne({ _id: songId }, (err, song) =>
+			WSModule.runJob("EMIT_TO_ROOMS", {
+				rooms: ["admin.unverifiedSongs", `edit-song.${songId}`],
 				args: ["event:admin.unverifiedSong.created", { data: { song } }]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `edit-song.${songId}`,
-				args: ["event:admin.unverifiedSong.created", { data: { song } }]
-			});
-		});
+			})
+		);
 	}
 });
 
@@ -64,17 +59,12 @@ CacheModule.runJob("SUB", {
 	channel: "song.newVerifiedSong",
 	cb: async songId => {
 		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.songs",
+		songModel.findOne({ _id: songId }, (err, song) =>
+			WSModule.runJob("EMIT_TO_ROOMS", {
+				rooms: ["admin.songs", `edit-song.${songId}`],
 				args: ["event:admin.verifiedSong.created", { data: { song } }]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `edit-song.${songId}`,
-				args: ["event:admin.verifiedSong.created", { data: { song } }]
-			});
-		});
+			})
+		);
 	}
 });
 
@@ -108,17 +98,12 @@ CacheModule.runJob("SUB", {
 			modelName: "song"
 		});
 
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.hiddenSongs",
-				args: ["event:admin.hiddenSong.created", { data: { song } }]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `edit-song.${songId}`,
+		songModel.findOne({ _id: songId }, (err, song) =>
+			WSModule.runJob("EMIT_TO_ROOMS", {
+				rooms: ["admin.hiddenSongs", `edit-song.${songId}`],
 				args: ["event:admin.hiddenSong.created", { data: { song } }]
-			});
-		});
+			})
+		);
 	}
 });
 

+ 22 - 62
backend/logic/actions/stations.js

@@ -168,17 +168,12 @@ CacheModule.runJob("SUB", {
 	cb: data => {
 		const { stationId, playlistId } = data;
 
-		PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }).then(playlist => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `station.${stationId}`,
-				args: ["event:station.includedPlaylist", { data: { stationId, playlist } }]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `manage-station.${stationId}`,
+		PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }).then(playlist =>
+			WSModule.runJob("EMIT_TO_ROOMS", {
+				rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 				args: ["event:station.includedPlaylist", { data: { stationId, playlist } }]
-			});
-		});
+			})
+		);
 	}
 });
 
@@ -187,17 +182,12 @@ CacheModule.runJob("SUB", {
 	cb: data => {
 		const { stationId, playlistId } = data;
 
-		PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }).then(playlist => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `station.${stationId}`,
+		PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }).then(playlist =>
+			WSModule.runJob("EMIT_TO_ROOMS", {
+				rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 				args: ["event:station.excludedPlaylist", { data: { stationId, playlist } }]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `manage-station.${stationId}`,
-				args: ["event:station.excludedPlaylist", { data: { stationId, playlist } }]
-			});
-		});
+			})
+		);
 	}
 });
 
@@ -205,13 +195,8 @@ CacheModule.runJob("SUB", {
 	channel: "station.removedIncludedPlaylist",
 	cb: data => {
 		const { stationId, playlistId } = data;
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${stationId}`,
-			args: ["event:station.removedIncludedPlaylist", { data: { stationId, playlistId } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 			args: ["event:station.removedIncludedPlaylist", { data: { stationId, playlistId } }]
 		});
 	}
@@ -221,13 +206,8 @@ CacheModule.runJob("SUB", {
 	channel: "station.removedExcludedPlaylist",
 	cb: data => {
 		const { stationId, playlistId } = data;
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${stationId}`,
-			args: ["event:station.removedExcludedPlaylist", { data: { stationId, playlistId } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 			args: ["event:station.removedExcludedPlaylist", { data: { stationId, playlistId } }]
 		});
 	}
@@ -366,13 +346,8 @@ CacheModule.runJob("SUB", {
 			});
 		});
 
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${stationId}`,
-			args: ["event:station.name.updated", { data: { stationId, name } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 			args: ["event:station.name.updated", { data: { stationId, name } }]
 		});
 	}
@@ -395,13 +370,8 @@ CacheModule.runJob("SUB", {
 			})
 		);
 
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${stationId}`,
-			args: ["event:station.displayName.updated", { data: { stationId, displayName } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 			args: ["event:station.displayName.updated", { data: { stationId, displayName } }]
 		});
 	}
@@ -424,13 +394,8 @@ CacheModule.runJob("SUB", {
 			})
 		);
 
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${stationId}`,
-			args: ["event:station.description.updated", { data: { stationId, description } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${stationId}`, `manage-station.${stationId}`],
 			args: ["event:station.description.updated", { data: { stationId, description } }]
 		});
 	}
@@ -485,13 +450,8 @@ CacheModule.runJob("SUB", {
 CacheModule.runJob("SUB", {
 	channel: "station.repositionSongInQueue",
 	cb: res => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${res.stationId}`,
-			args: ["event:station.queue.song.repositioned", { data: { song: res.song } }]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `manage-station.${res.stationId}`,
+		WSModule.runJob("EMIT_TO_ROOMS", {
+			rooms: [`station.${res.stationId}`, `manage-station.${res.stationId}`],
 			args: ["event:station.queue.song.repositioned", { data: { song: res.song } }]
 		});
 	}

+ 29 - 0
backend/logic/ws.js

@@ -325,6 +325,35 @@ class _WSModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Emits arguments to any sockets that are in specified rooms
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {Array} payload.rooms - array of strings with the name of each room e.g. ["station-page", "song.1234"]
+	 * @param {object} payload.args - any arguments to be emitted to the sockets in the specific room
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	async EMIT_TO_ROOMS(payload) {
+		return new Promise(resolve =>
+			async.each(
+				payload.rooms,
+				(room, next) => {
+					// if the room exists
+					if (WSModule.rooms[room] && WSModule.rooms[room].length > 0)
+						return WSModule.rooms[room].forEach(async socketId => {
+							// get every socketId (and thus every socket) in the room, and dispatch to each
+							const socket = await WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this);
+							socket.dispatch(...payload.args);
+							return next();
+						});
+
+					return next();
+				},
+				() => resolve()
+			)
+		);
+	}
+
 	/**
 	 * Allows a socket to join a 'song' room
 	 *