Browse Source

Fixed some issues in backend due to eslint

Kristian Vos 4 years ago
parent
commit
60a3d11a20

+ 5 - 4
backend/index.js

@@ -291,12 +291,12 @@ class ModuleManager {
 			}
 		}); // ensures all modules are imported, then converts promise to the default export of the import
 
-		for (let moduleId = 0, moduleNames = Object.keys(this.modules); moduleId < moduleNames.length; moduleId += 1) {
-			const module = this.modules[moduleNames[moduleId]];
+		Object.keys(this.modules).every(moduleKey => {
+			const module = this.modules[moduleKey];
 
 			module.setModuleManager(this);
 
-			if (this.lockdown) break;
+			if (this.lockdown) return false;
 
 			module._initialize();
 
@@ -314,7 +314,8 @@ class ModuleManager {
 			// 	this.logger.info("MODULE_MANAGER", `${moduleName} dependencies have been completed`);
 			// 	module._initialize();
 			// });
-		}
+			return true;
+		});
 	}
 
 	/**

+ 2 - 2
backend/logic/actions/queueSongs.js

@@ -137,9 +137,9 @@ const lib = {
 					let updated = false;
 
 					const $set = {};
-					for (let prop = 0, songKeys = Object.keys(updatedSong); prop < songKeys.length; prop += 1) {
+					Object.keys(updatedSong).forEach(prop => {
 						if (updatedSong[prop] !== song[prop]) $set[prop] = updatedSong[prop];
-					}
+					});
 
 					updated = true;
 					if (!updated) return next("No properties changed");

+ 23 - 31
backend/logic/actions/stations.js

@@ -84,29 +84,21 @@ const usersPerStationCount = {};
 // 			// TODO Code to show users
 // 		},
 // 		() => {
-// 			for (
-// 				let stationId = 0, stationKeys = Object.keys(usersPerStationCount);
-// 				stationId < stationKeys.length;
-// 				stationId += 1
-// 			) {
-// 				if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
-// 					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(stationId);
-// 				}
-// 			}
-
-// 			for (
-// 				let stationId = 0, stationKeys = Object.keys(usersPerStation);
-// 				stationId < stationKeys.length;
-// 				stationId += 1
-// 			) {
-// 				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(usersPerStationCount).forEach(stationId => {
+//				if (oldUsersPerStationCount[stationId] !== usersPerStationCount[stationId]) {
+//					if (stationsCountUpdated.indexOf(stationId) === -1) stationsCountUpdated.push(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);
+//				}
+//			});
+//
 // 			stationsCountUpdated.forEach(stationId => {
 // 				console.log("INFO", "UPDATE_STATION_USER_COUNT", `Updating user count of ${stationId}.`);
 // 				cache.runJob("PUB", {
@@ -157,9 +149,9 @@ cache.runJob("SUB", {
 				const sockets = await utils.runJob("GET_ROOM_SOCKETS", {
 					room: "home"
 				});
-				for (let socketId = 0, socketKeys = Object.keys(sockets); socketId < socketKeys.length; socketId += 1) {
-					const socket = sockets[socketKeys[socketId]];
-					const { session } = sockets[socketKeys[socketId]];
+				Object.keys(sockets).forEach(socketKey => {
+					const socket = sockets[socketKey];
+					const { session } = socket;
 					if (session.sessionId) {
 						cache
 							.runJob("HGET", {
@@ -180,7 +172,7 @@ cache.runJob("SUB", {
 									);
 							});
 					}
-				}
+				});
 			}
 		});
 	}
@@ -298,9 +290,9 @@ cache.runJob("SUB", {
 				const sockets = await utils.runJob("GET_ROOM_SOCKETS", {
 					room: "home"
 				});
-				for (let socketId = 0, socketKeys = Object.keys(sockets); socketId < socketKeys.length; socketId += 1) {
-					const socket = sockets[socketKeys[socketId]];
-					const { session } = sockets[socketKeys[socketId]];
+				Object.keys(sockets).forEach(socketKey => {
+					const socket = sockets[socketKey];
+					const { session } = socket;
 					if (session.sessionId) {
 						cache
 							.runJob("HGET", {
@@ -317,7 +309,7 @@ cache.runJob("SUB", {
 								}
 							});
 					}
-				}
+				});
 			}
 		});
 	}

+ 3 - 7
backend/logic/punishments.js

@@ -123,15 +123,11 @@ class _PunishmentsModule extends CoreClass {
 					(punishments, next) => {
 						let filteredPunishments = [];
 
-						for (
-							let id = 0, punishmentKeys = Object.keys(punishments);
-							id < punishmentKeys.length;
-							id += 1
-						) {
-							const punishment = punishments[id];
+						Object.keys(punishments).forEach(punishmentKey => {
+							const punishment = punishments[punishmentKey];
 							punishment.punishmentId = id;
 							punishments.push(punishment);
-						}
+						});
 
 						filteredPunishments = punishments.filter(punishment => {
 							if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);

+ 4 - 8
backend/logic/stations.js

@@ -908,13 +908,9 @@ class _StationsModule extends CoreClass {
 						} else {
 							const sockets = await UtilsModule.runJob("GET_ROOM_SOCKETS", { room: "home" }, this);
 
-							for (
-								let socketId = 0, socketKeys = Object.keys(sockets);
-								socketId < socketKeys.length;
-								socketId += 1
-							) {
-								const socket = sockets[socketId];
-								const { session } = sockets[socketId];
+							Object.keys(sockets).forEach(socketKey => {
+								const socket = sockets[socketKey];
+								const { session } = socket;
 								if (session.sessionId) {
 									CacheModule.runJob(
 										"HGET",
@@ -957,7 +953,7 @@ class _StationsModule extends CoreClass {
 										}
 									});
 								}
-							}
+							});
 						}
 
 						if (station.currentSong !== null && station.currentSong.songId !== undefined) {

+ 46 - 36
backend/logic/utils.js

@@ -444,9 +444,11 @@ class _UtilsModule extends CoreClass {
 
 		return new Promise(resolve => {
 			const { rooms } = socket;
-			for (let room = 0, roomKeys = Object.keys(rooms); room < roomKeys.length; room += 1) {
+
+			Object.keys(rooms).forEach(roomKey => {
+				const room = rooms[roomKey];
 				socket.leave(room);
-			}
+			});
 
 			return resolve();
 		});
@@ -471,9 +473,10 @@ class _UtilsModule extends CoreClass {
 
 		return new Promise(resolve => {
 			const { rooms } = socket;
-			for (let room = 0, roomKeys = Object.keys(rooms); room < roomKeys.length; room += 1) {
+			Object.keys(rooms).forEach(roomKey => {
+				const room = rooms[roomKey];
 				socket.leave(room);
-			}
+			});
 
 			socket.join(payload.room);
 
@@ -495,9 +498,10 @@ class _UtilsModule extends CoreClass {
 
 		return new Promise(resolve => {
 			const { rooms } = socket;
-			for (let room = 0, roomKeys = Object.keys(rooms); room < roomKeys.length; room += 1) {
-				if (room.indexOf("song.") !== -1) socket.leave(rooms);
-			}
+			Object.keys(rooms).forEach(roomKey => {
+				const room = rooms[roomKey];
+				if (room.indexOf("song.") !== -1) socket.leave(room);
+			});
 
 			socket.join(payload.room);
 
@@ -510,16 +514,17 @@ class _UtilsModule extends CoreClass {
 	SOCKETS_JOIN_SONG_ROOM(payload) {
 		// sockets, room
 		return new Promise(resolve => {
-			for (let id = 0, socketKeys = Object.keys(payload.sockets); id < socketKeys.length; id += 1) {
-				const socket = payload.sockets[socketKeys[id]];
+			Object.keys(payload.sockets).forEach(socketKey => {
+				const socket = payload.sockets[socketKey];
 
 				const { rooms } = socket;
-				for (let room = 0, roomKeys = Object.keys(rooms); room < roomKeys.length; room += 1) {
+				Object.keys(rooms).forEach(roomKey => {
+					const room = rooms[roomKey];
 					if (room.indexOf("song.") !== -1) socket.leave(room);
-				}
+				});
 
 				socket.join(payload.room);
-			}
+			});
 
 			return resolve();
 		});
@@ -530,13 +535,14 @@ class _UtilsModule extends CoreClass {
 	SOCKETS_LEAVE_SONG_ROOMS(payload) {
 		// sockets
 		return new Promise(resolve => {
-			for (let id = 0, socketKeys = Object.keys(payload.sockets); id < socketKeys.length; id += 1) {
-				const socket = payload.sockets[socketKeys[id]];
+			Object.keys(payload.sockets).forEach(socketKey => {
+				const socket = payload.sockets[socketKey];
 				const { rooms } = socket;
-				for (let room = 0, roomKeys = Object.keys(rooms); room < roomKeys.length; room += 1) {
+				Object.keys(rooms).forEach(roomKey => {
+					const room = rooms[roomKey];
 					if (room.indexOf("song.") !== -1) socket.leave(room);
-				}
-			}
+				});
+			});
 			resolve();
 		});
 	}
@@ -554,12 +560,12 @@ class _UtilsModule extends CoreClass {
 
 		return new Promise(resolve => {
 			const { sockets } = io.sockets;
-			for (let id = 0, socketKeys = Object.keys(sockets); id < socketKeys.length; id += 1) {
-				const socket = sockets[socketKeys[id]];
+			Object.keys(sockets).forEach(socketKey => {
+				const socket = sockets[socketKey];
 				if (socket.rooms[payload.room]) {
 					socket.emit(...payload.args);
 				}
-			}
+			});
 
 			return resolve();
 		});
@@ -578,10 +584,10 @@ class _UtilsModule extends CoreClass {
 		return new Promise(resolve => {
 			const { sockets } = io.sockets;
 			const roomSockets = [];
-			for (let id = 0, socketKeys = Object.keys(sockets); id < socketKeys.length; id += 1) {
-				const socket = sockets[socketKeys[id]];
+			Object.keys(sockets).forEach(socketKey => {
+				const socket = sockets[socketKey];
 				if (socket.rooms[payload.room]) roomSockets.push(socket);
-			}
+			});
 
 			return resolve(roomSockets);
 		});
@@ -842,10 +848,12 @@ class _UtilsModule extends CoreClass {
 				if (err) console.error(err);
 				body = JSON.parse(body);
 				if (body.error) console.error(body.error);
-				for (let i = 0, bodyKeys = Object.keys(body); i < bodyKeys.length; i += 1) {
-					const { items } = body[i];
-					for (let j = 0, itemKeys = Object.keys(body); j < itemKeys.length; j += 1) {
-						const item = items[j];
+				Object.keys(body).forEach(bodyKey => {
+					const { items } = body[bodyKey];
+
+					Object.keys(items).every(itemsKey => {
+						const item = items[itemsKey];
+
 						let hasArtist = false;
 						for (let k = 0; k < item.artists.length; k += 1) {
 							const artist = item.artists[k];
@@ -857,10 +865,11 @@ class _UtilsModule extends CoreClass {
 							song.title = item.name;
 							song.explicit = item.explicit;
 							song.thumbnail = item.album.images[1].url;
-							break;
+							return false;
 						}
-					}
-				}
+						return true;
+					});
+				});
 
 				resolve({ song });
 			});
@@ -897,10 +906,11 @@ class _UtilsModule extends CoreClass {
 
 				const songs = [];
 
-				for (let i = 0, bodyKeys = Object.keys(body); i < bodyKeys.length; i += 1) {
-					const { items } = body[i];
-					for (let j = 0, itemKeys = Object.keys(body); j < itemKeys.length; j += 1) {
-						const item = items[j];
+				Object.keys(body).forEach(bodyKey => {
+					const { items } = body[bodyKey];
+
+					Object.keys(items).forEach(itemsKey => {
+						const item = items[itemsKey];
 						let hasArtist = false;
 						for (let k = 0; k < item.artists.length; k += 1) {
 							const localArtist = item.artists[k];
@@ -918,8 +928,8 @@ class _UtilsModule extends CoreClass {
 							song.thumbnail = item.album.images[1].url;
 							songs.push(song);
 						}
-					}
-				}
+					});
+				});
 
 				return resolve({ songs });
 			});