Browse Source

Further improved backend

Kristian Vos 4 years ago
parent
commit
03a61883b3
5 changed files with 98 additions and 208 deletions
  1. 15 15
      backend/logic/io.js
  2. 19 46
      backend/logic/playlists.js
  3. 16 42
      backend/logic/punishments.js
  4. 25 61
      backend/logic/songs.js
  5. 23 44
      backend/logic/tasks.js

+ 15 - 15
backend/logic/io.js

@@ -38,6 +38,8 @@ class _IOModule extends CoreClass {
 		DBModule = this.moduleManager.modules.db;
 		PunishmentsModule = this.moduleManager.modules.punishments;
 
+		this.userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" });
+
 		this.setStage(2);
 
 		const SIDname = config.get("cookie.SIDname");
@@ -212,21 +214,19 @@ class _IOModule extends CoreClass {
 					})
 						.then(session => {
 							if (session && session.userId) {
-								DBModule.runJob("GET_MODEL", { modelName: "user" }).then(userModel => {
-									userModel.findOne({ _id: session.userId }, (err, user) => {
-										if (err || !user) return socket.emit("ready", false);
-
-										let role = "";
-										let username = "";
-										let userId = "";
-										if (user) {
-											role = user.role;
-											username = user.username;
-											userId = session.userId;
-										}
-
-										return socket.emit("ready", true, role, username, userId);
-									});
+								IOModule.userModel.findOne({ _id: session.userId }, (err, user) => {
+									if (err || !user) return socket.emit("ready", false);
+
+									let role = "";
+									let username = "";
+									let userId = "";
+									if (user) {
+										role = user.role;
+										username = user.username;
+										userId = session.userId;
+									}
+
+									return socket.emit("ready", true, role, username, userId);
 								});
 							} else socket.emit("ready", false);
 						})

+ 19 - 46
backend/logic/playlists.js

@@ -27,8 +27,8 @@ class _PlaylistsModule extends CoreClass {
 		DBModule = this.moduleManager.modules.db;
 		UtilsModule = this.moduleManager.modules.utils;
 
-		const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
-		const playlistSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "playlist" });
+		this.playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
+		this.playlistSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "playlist" });
 
 		this.setStage(2);
 
@@ -83,7 +83,7 @@ class _PlaylistsModule extends CoreClass {
 								CacheModule.runJob("HSET", {
 									table: "playlists",
 									key: playlist._id,
-									value: playlistSchema(playlist)
+									value: playlistSchemaCache(playlist)
 								})
 									.then(() => cb())
 									.catch(next);
@@ -112,17 +112,8 @@ class _PlaylistsModule extends CoreClass {
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	GET_PLAYLIST(payload) {
-		return new Promise((resolve, reject) => {
-			let playlistModel;
-
-			PlaylistsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
-				.then(model => {
-					playlistModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
 						CacheModule.runJob("HGETALL", { table: "playlists" }, this)
@@ -140,7 +131,7 @@ class _PlaylistsModule extends CoreClass {
 						return async.each(
 							playlistIds,
 							(playlistId, next) => {
-								playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
+								PlaylistsModule.playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
 									if (err) next(err);
 									else if (!playlist) {
 										CacheModule.runJob(
@@ -175,7 +166,7 @@ class _PlaylistsModule extends CoreClass {
 
 					(playlist, next) => {
 						if (playlist) return next(true, playlist);
-						return playlistModel.findOne({ _id: payload.playlistId }, next);
+						return PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
 					},
 
 					(playlist, next) => {
@@ -200,8 +191,8 @@ class _PlaylistsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve(playlist);
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -213,20 +204,11 @@ class _PlaylistsModule extends CoreClass {
 	 */
 	UPDATE_PLAYLIST(payload) {
 		// playlistId, cb
-		return new Promise((resolve, reject) => {
-			let playlistModel;
-
-			PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
-				.then(model => {
-					playlistModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						playlistModel.findOne({ _id: payload.playlistId }, next);
+						PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
 					},
 
 					(playlist, next) => {
@@ -258,8 +240,8 @@ class _PlaylistsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve(playlist);
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -271,20 +253,11 @@ class _PlaylistsModule extends CoreClass {
 	 */
 	DELETE_PLAYLIST(payload) {
 		// playlistId, cb
-		return new Promise((resolve, reject) => {
-			let playlistModel;
-
-			PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
-				.then(model => {
-					playlistModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						playlistModel.deleteOne({ _id: payload.playlistId }, next);
+						PlaylistsModule.playlistModel.deleteOne({ _id: payload.playlistId }, next);
 					},
 
 					(res, next) => {
@@ -304,8 +277,8 @@ class _PlaylistsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve();
 				}
-			);
-		});
+			)
+		);
 	}
 }
 

+ 16 - 42
backend/logic/punishments.js

@@ -27,8 +27,8 @@ class _PunishmentsModule extends CoreClass {
 		DBModule = this.moduleManager.modules.db;
 		UtilsModule = this.moduleManager.modules.utils;
 
-		const punishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" });
-		const punishmentSchema = await DBModule.runJob("GET_SCHEMA", { schemaName: "punishment" });
+		this.punishmentModel = await DBModule.runJob("GET_MODEL", { modelName: "punishment" });
+		this.punishmentSchemaCache = await DBModule.runJob("GET_SCHEMA", { schemaName: "punishment" });
 
 		return new Promise((resolve, reject) =>
 			async.waterfall(
@@ -52,7 +52,7 @@ class _PunishmentsModule extends CoreClass {
 						return async.each(
 							punishmentIds,
 							(punishmentId, cb) => {
-								punishmentModel.findOne({ _id: punishmentId }, (err, punishment) => {
+								PunishmentsModule.punishmentModel.findOne({ _id: punishmentId }, (err, punishment) => {
 									if (err) next(err);
 									else if (!punishment)
 										CacheModule.runJob("HDEL", {
@@ -72,7 +72,7 @@ class _PunishmentsModule extends CoreClass {
 
 					next => {
 						this.setStage(4);
-						punishmentModel.find({}, next);
+						PunishmentsModule.punishmentModel.find({}, next);
 					},
 
 					(punishments, next) => {
@@ -85,7 +85,7 @@ class _PunishmentsModule extends CoreClass {
 								return CacheModule.runJob("HSET", {
 									table: "punishments",
 									key: punishment._id,
-									value: punishmentSchema(punishment, punishment._id)
+									value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
 								})
 									.then(() => next())
 									.catch(next);
@@ -176,17 +176,8 @@ class _PunishmentsModule extends CoreClass {
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	GET_PUNISHMENT(payload) {
-		return new Promise((resolve, reject) => {
-			let punishmentModel;
-
-			PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "punishment" }, this)
-				.then(model => {
-					punishmentModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
 						if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
@@ -204,7 +195,7 @@ class _PunishmentsModule extends CoreClass {
 
 					(punishment, next) => {
 						if (punishment) return next(true, punishment);
-						return punishmentModel.findOne({ _id: payload.id }, next);
+						return PunishmentsModule.punishmentModel.findOne({ _id: payload.id }, next);
 					},
 
 					(punishment, next) => {
@@ -229,8 +220,8 @@ class _PunishmentsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve(punishment);
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -278,28 +269,11 @@ class _PunishmentsModule extends CoreClass {
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	ADD_PUNISHMENT(payload) {
-		return new Promise((resolve, reject) => {
-			let PunishmentModel;
-			let PunishmentSchema;
-
-			PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "punishment" }, this)
-				.then(model => {
-					PunishmentModel = model;
-				})
-				.catch(console.error);
-
-			PunishmentsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_SCHEMA", { schemaName: "punishment" }, this)
-				.then(model => {
-					PunishmentSchema = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						const punishment = new PunishmentModel({
+						const punishment = new PunishmentsModule.punishmentModel({
 							type: payload.type,
 							value: payload.value,
 							reason: payload.reason,
@@ -320,7 +294,7 @@ class _PunishmentsModule extends CoreClass {
 							{
 								table: "punishments",
 								key: punishment._id,
-								value: PunishmentSchema(punishment, punishment._id)
+								value: PunishmentsModule.punishmentSchemaCache(punishment, punishment._id)
 							},
 							this
 						)
@@ -337,8 +311,8 @@ class _PunishmentsModule extends CoreClass {
 					if (err) return reject(new Error(err));
 					return resolve(punishment);
 				}
-			);
-		});
+			)
+		);
 	}
 }
 

+ 25 - 61
backend/logic/songs.js

@@ -27,8 +27,8 @@ class _SongsModule extends CoreClass {
 		DBModule = this.moduleManager.modules.db;
 		UtilsModule = this.moduleManager.modules.utils;
 
-		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-		const songSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
+		this.songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
+		this.songSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
 
 		this.setStage(2);
 
@@ -54,7 +54,7 @@ class _SongsModule extends CoreClass {
 						return async.each(
 							songIds,
 							(songId, next) => {
-								songModel.findOne({ songId }, (err, song) => {
+								SongsModule.songModel.findOne({ songId }, (err, song) => {
 									if (err) next(err);
 									else if (!song)
 										CacheModule.runJob("HDEL", {
@@ -72,7 +72,7 @@ class _SongsModule extends CoreClass {
 
 					next => {
 						this.setStage(4);
-						songModel.find({}, next);
+						SongsModule.songModel.find({}, next);
 					},
 
 					(songs, next) => {
@@ -83,7 +83,7 @@ class _SongsModule extends CoreClass {
 								CacheModule.runJob("HSET", {
 									table: "songs",
 									key: song.songId,
-									value: songSchema(song)
+									value: SongsModule.songSchemaCache(song)
 								})
 									.then(() => next())
 									.catch(next);
@@ -110,17 +110,8 @@ class _SongsModule extends CoreClass {
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	GET_SONG(payload) {
-		return new Promise((resolve, reject) => {
-			let songModel;
-
-			SongsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "song" }, this)
-				.then(model => {
-					songModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
 						if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
@@ -133,7 +124,7 @@ class _SongsModule extends CoreClass {
 
 					(song, next) => {
 						if (song) return next(true, song);
-						return songModel.findOne({ _id: payload.id }, next);
+						return SongsModule.songModel.findOne({ _id: payload.id }, next);
 					},
 
 					(song, next) => {
@@ -154,8 +145,8 @@ class _SongsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve({ song });
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -166,28 +157,19 @@ class _SongsModule extends CoreClass {
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	GET_SONG_FROM_ID(payload) {
-		return new Promise((resolve, reject) => {
-			let songModel;
-
-			SongsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "song" }, this)
-				.then(model => {
-					songModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						songModel.findOne({ songId: payload.songId }, next);
+						SongsModule.songModel.findOne({ songId: payload.songId }, next);
 					}
 				],
 				(err, song) => {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve({ song });
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -199,20 +181,11 @@ class _SongsModule extends CoreClass {
 	 */
 	UPDATE_SONG(payload) {
 		// songId, cb
-		return new Promise((resolve, reject) => {
-			let songModel;
-
-			SongsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "song" }, this)
-				.then(model => {
-					songModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						songModel.findOne({ _id: payload.songId }, next);
+						SongsModule.songModel.findOne({ _id: payload.songId }, next);
 					},
 
 					(song, next) => {
@@ -243,8 +216,8 @@ class _SongsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve(song);
 				}
-			);
-		});
+			)
+		);
 	}
 
 	/**
@@ -256,20 +229,11 @@ class _SongsModule extends CoreClass {
 	 */
 	DELETE_SONG(payload) {
 		// songId, cb
-		return new Promise((resolve, reject) => {
-			let songModel;
-
-			SongsModule.log("ERROR", "HOW DOES THIS WORK!?!?!??!?!?!?!??!?!??!?!?!?!");
-			DBModule.runJob("GET_MODEL", { modelName: "song" })
-				.then(model => {
-					songModel = model;
-				})
-				.catch(console.error);
-
-			return async.waterfall(
+		return new Promise((resolve, reject) =>
+			async.waterfall(
 				[
 					next => {
-						songModel.deleteOne({ songId: payload.songId }, next);
+						SongsModule.songModel.deleteOne({ songId: payload.songId }, next);
 					},
 
 					next => {
@@ -289,8 +253,8 @@ class _SongsModule extends CoreClass {
 					if (err && err !== true) return reject(new Error(err));
 					return resolve();
 				}
-			);
-		});
+			)
+		);
 	}
 }
 

+ 23 - 44
backend/logic/tasks.js

@@ -130,8 +130,7 @@ class _TasksModule extends CoreClass {
 			const task = TasksModule.tasks[payload.name];
 			if (task.timer) task.timer.pause();
 
-			TasksModule.log("ERROR", "CHECK THIS?!?!?!??!?!?!?!?!??!?!");
-			task.fn.apply(this).then(() => {
+			task.fn.apply(null).then(() => {
 				task.lastRan = Date.now();
 				task.timer = new Timer(
 					() => TasksModule.runJob("RUN_TASK", { name: payload.name }),
@@ -154,7 +153,7 @@ class _TasksModule extends CoreClass {
 			async.waterfall(
 				[
 					next => {
-						CacheModule.runJob("HGETALL", { table: "stations" }, this)
+						CacheModule.runJob("HGETALL", { table: "stations" })
 							.then(response => next(null, response))
 							.catch(next);
 					},
@@ -172,13 +171,9 @@ class _TasksModule extends CoreClass {
 									"TASK_STATIONS_SKIP_CHECK",
 									`Skipping ${station._id} as it should have skipped already.`
 								);
-								return StationsModule.runJob(
-									"INITIALIZE_STATION",
-									{
-										stationId: station._id
-									},
-									this
-								).then(() => next2());
+								return StationsModule.runJob("INITIALIZE_STATION", {
+									stationId: station._id
+								}).then(() => next2());
 							},
 							() => next()
 						);
@@ -201,7 +196,7 @@ class _TasksModule extends CoreClass {
 			async.waterfall(
 				[
 					next => {
-						CacheModule.runJob("HGETALL", { table: "sessions" }, this)
+						CacheModule.runJob("HGETALL", { table: "sessions" })
 							.then(sessions => next(null, sessions))
 							.catch(next);
 					},
@@ -224,14 +219,10 @@ class _TasksModule extends CoreClass {
 
 								if (!session) {
 									TasksModule.log("INFO", "TASK_SESSION_CLEAR", "Removing an empty session.");
-									return CacheModule.runJob(
-										"HDEL",
-										{
-											table: "sessions",
-											key: sessionId
-										},
-										this
-									).finally(() => {
+									return CacheModule.runJob("HDEL", {
+										table: "sessions",
+										key: sessionId
+									}).finally(() => {
 										next2();
 									});
 								}
@@ -244,24 +235,16 @@ class _TasksModule extends CoreClass {
 									}).finally(() => next2());
 								}
 								if (Date.now() - session.refreshDate > 60 * 60 * 24 * 30 * 1000) {
-									return UtilsModule.runJob(
-										"SOCKETS_FROM_SESSION_ID",
-										{
-											sessionId: session.sessionId
-										},
-										this
-									).then(response => {
+									return UtilsModule.runJob("SOCKETS_FROM_SESSION_ID", {
+										sessionId: session.sessionId
+									}).then(response => {
 										if (response.sockets.length > 0) {
 											session.refreshDate = Date.now();
-											CacheModule.runJob(
-												"HSET",
-												{
-													table: "sessions",
-													key: sessionId,
-													value: session
-												},
-												this
-											).finally(() => {
+											CacheModule.runJob("HSET", {
+												table: "sessions",
+												key: sessionId,
+												value: session
+											}).finally(() => {
 												next2();
 											});
 										} else {
@@ -270,14 +253,10 @@ class _TasksModule extends CoreClass {
 												"TASK_SESSION_CLEAR",
 												`Removing session ${sessionId} for user ${session.userId} since inactive for 30 days and not currently in use.`
 											);
-											CacheModule.runJob(
-												"HDEL",
-												{
-													table: "sessions",
-													key: session.sessionId
-												},
-												this
-											).finally(() => next2());
+											CacheModule.runJob("HDEL", {
+												table: "sessions",
+												key: session.sessionId
+											}).finally(() => next2());
 										}
 									});
 								}
@@ -316,7 +295,7 @@ class _TasksModule extends CoreClass {
 				},
 				async err => {
 					if (err && err !== true) {
-						err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+						err = await UtilsModule.runJob("GET_ERROR", { error: err });
 						return reject(new Error(err));
 					}
 					if (err === true) {