Browse Source

Fixed some backend action issues due to last commit

Kristian Vos 4 years ago
parent
commit
8d75dfe4c8

+ 27 - 12
backend/logic/actions/playlists.js

@@ -105,7 +105,7 @@ CacheModule.runJob("SUB", {
 	}
 });
 
-const lib = {
+export default {
 	/**
 	 * Gets the first song from a private playlist
 	 *
@@ -634,15 +634,32 @@ const lib = {
 						if (processed === songIds.length) next();
 					}
 					for (let s = 0; s < songIds.length; s += 1) {
-						// eslint-disable-next-line no-loop-func
-						lib.addSongToPlaylist(session, true, songIds[s], playlistId, res => {
-							processed += 1;
-							if (res.status === "success") {
-								addedSongs.push(songIds[s]);
-								songsSuccess += 1;
-							} else songsFail += 1;
-							checkDone();
-						});
+						IOModule.runJob(
+							"RUN_ACTION2",
+							{
+								session,
+								namespace: "playlists",
+								action: "addSongToPlaylist",
+								args: [true, songIds[s], playlistId]
+							},
+							this
+						)
+							// eslint-disable-next-line no-loop-func
+							.then(res => {
+								if (res.status === "success") {
+									addedSongs.push(songIds[s]);
+									songsSuccess += 1;
+								} else songsFail += 1;
+							})
+							// eslint-disable-next-line no-loop-func
+							.catch(() => {
+								songsFail += 1;
+							})
+							// eslint-disable-next-line no-loop-func
+							.finally(() => {
+								processed += 1;
+								checkDone();
+							});
 					}
 				},
 
@@ -1159,5 +1176,3 @@ const lib = {
 		);
 	})
 };
-
-export default lib;

+ 11 - 4
backend/logic/actions/queueSongs.js

@@ -52,7 +52,7 @@ CacheModule.runJob("SUB", {
 	}
 });
 
-const lib = {
+export default {
 	/**
 	 * Returns the length of the queue songs list
 	 *
@@ -358,7 +358,16 @@ const lib = {
 						if (processed === songIds.length) next();
 					}
 					songIds.forEach(songId => {
-						lib.add(session, songId, () => {
+						IOModule.runJob(
+							"RUN_ACTION2",
+							{
+								session,
+								namespace: "queueSongs",
+								action: "add",
+								args: [songId]
+							},
+							this
+						).finally(() => {
 							processed += 1;
 							checkDone();
 						});
@@ -388,5 +397,3 @@ const lib = {
 		);
 	})
 };
-
-export default lib;

+ 30 - 21
backend/logic/actions/users.js

@@ -117,7 +117,7 @@ CacheModule.runJob("SUB", {
 	}
 });
 
-const thisExport = {
+export default {
 	/**
 	 * Lists all Users
 	 *
@@ -404,25 +404,36 @@ const thisExport = {
 					return cb({ status: "failure", message: err });
 				}
 
-				return thisExport.login(session, email, password, result => {
-					const obj = {
-						status: "success",
-						message: "Successfully registered."
-					};
-					if (result.status === "success") {
-						obj.SID = result.SID;
-					}
-					ActivitiesModule.runJob("ADD_ACTIVITY", {
-						userId: user._id,
-						activityType: "created_account"
-					});
-					this.log(
-						"SUCCESS",
-						"USER_PASSWORD_REGISTER",
-						`Register successful with password for user "${username}".`
-					);
-					return cb(obj);
+				ActivitiesModule.runJob("ADD_ACTIVITY", {
+					userId: user._id,
+					activityType: "created_account"
 				});
+				this.log(
+					"SUCCESS",
+					"USER_PASSWORD_REGISTER",
+					`Register successful with password for user "${username}".`
+				);
+
+				const result = await this.module.runJob(
+					"RUN_ACTION2",
+					{
+						session,
+						namespace: "users",
+						action: "login",
+						args: [email, password]
+					},
+					this
+				);
+
+				const obj = {
+					status: "success",
+					message: "Successfully registered."
+				};
+				if (result.status === "success") {
+					obj.SID = result.SID;
+				}
+
+				return cb(obj);
 			}
 		);
 	},
@@ -1956,5 +1967,3 @@ const thisExport = {
 		);
 	})
 };
-
-export default thisExport;

+ 56 - 31
backend/logic/io.js

@@ -731,42 +731,67 @@ class _IOModule extends CoreClass {
 						});
 			})
 				.then(() => {
-					try {
-						// call the action, passing it the session, and the arguments socket.io passed us
-
-						IOModule.actions[namespace][action].apply(
-							this,
-							[socket.session].concat(args).concat([
-								result => {
-									IOModule.log(
-										"INFO",
-										"RUN_ACTION",
-										`Response to action. Action: ${namespace}.${action}. Response status: ${result.status}`
-									);
-									// respond to the socket with our message
-									if (typeof cb === "function") cb(result);
-									resolve();
-								}
-							])
-						);
-					} catch (err) {
-						if (typeof cb === "function")
-							cb({
-								status: "error",
-								message: "An error occurred while executing the specified action."
-							});
-						reject(err);
+					// call the job that calls the action, passing it the session, and the arguments socket.io passed us
 
-						IOModule.log(
-							"ERROR",
-							"IO_ACTION_ERROR",
-							`Some type of exception occurred in the action ${namespace}.${action}. Error message: ${err.message}`
-						);
-					}
+					IOModule.runJob("RUN_ACTION2", { session: socket.session, namespace, action, args })
+						.then(response => {
+							cb(response);
+						})
+						.catch(err => {
+							if (typeof cb === "function")
+								cb({
+									status: "error",
+									message: "An error occurred while executing the specified action."
+								});
+							reject(err);
+
+							IOModule.log(
+								"ERROR",
+								"IO_ACTION_ERROR",
+								`Some type of exception occurred in the action ${namespace}.${action}. Error message: ${err.message}`
+							);
+						});
 				})
 				.catch(reject);
 		});
 	}
+
+	/**
+	 * Runs an action
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	async RUN_ACTION2(payload) {
+		return new Promise((resolve, reject) => {
+			const { session, namespace, action, args } = payload;
+
+			try {
+				// call the the action, passing it the session, and the arguments socket.io passed us
+				IOModule.actions[namespace][action].apply(
+					this,
+					[session].concat(args).concat([
+						result => {
+							IOModule.log(
+								"INFO",
+								"RUN_ACTION2",
+								`Response to action. Action: ${namespace}.${action}. Response status: ${result.status}`
+							);
+							resolve(result);
+						}
+					])
+				);
+			} catch (err) {
+				reject(err);
+
+				IOModule.log(
+					"ERROR",
+					"IO_ACTION_ERROR",
+					`Some type of exception occurred in the action ${namespace}.${action}. Error message: ${err.message}`
+				);
+			}
+		});
+	}
 }
 
 export default new _IOModule();