Procházet zdrojové kódy

fix(Playlists): fixed shuffle logic, removed unnecessary array sorting

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan před 4 roky
rodič
revize
8c50a42799

+ 5 - 13
backend/logic/actions/playlists.js

@@ -461,9 +461,6 @@ export default {
 					return cb({ status: "failure", message: err });
 				}
 
-				const sortedSongs = playlist.songs.sort((a, b) => a.position - b.position);
-				playlist.songs = sortedSongs;
-
 				this.log(
 					"SUCCESS",
 					"PLAYLIST_GET",
@@ -588,13 +585,8 @@ export default {
 	 * @param {Function} cb - gets called with the result
 	 */
 	shuffle: isLoginRequired(async function shuffle(session, playlistId, cb) {
-		const playlistModel = await DBModule.runJob(
-			"GET_MODEL",
-			{
-				modelName: "playlist"
-			},
-			this
-		);
+		const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
+
 		async.waterfall(
 			[
 				next => {
@@ -616,9 +608,7 @@ export default {
 
 				(res, next) => {
 					PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
-						.then(playlist => {
-							next(null, playlist);
-						})
+						.then(playlist => next(null, playlist))
 						.catch(next);
 				}
 			],
@@ -632,11 +622,13 @@ export default {
 					);
 					return cb({ status: "failure", message: err });
 				}
+
 				this.log(
 					"SUCCESS",
 					"PLAYLIST_SHUFFLE",
 					`Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
 				);
+
 				return cb({
 					status: "success",
 					message: "Successfully shuffled playlist.",

+ 16 - 6
backend/logic/utils.js

@@ -247,7 +247,7 @@ class _UtilsModule extends CoreClass {
 	}
 
 	/**
-	 * Shuffles an array of songs
+	 * Shuffles an array of songs by their position property
 	 *
 	 * @param {object} payload - object that contains the payload
 	 * @param {object} payload.array - an array of songs that should be shuffled
@@ -256,9 +256,14 @@ class _UtilsModule extends CoreClass {
 	SHUFFLE(payload) {
 		// array
 		return new Promise(resolve => {
-			const array = payload.array.slice();
+			const { array } = payload;
 
-			let currentIndex = payload.array.length;
+			// get array of positions
+			const positions = [];
+			array.forEach(song => positions.push(song.position));
+
+			// sort the positions array
+			let currentIndex = positions.length;
 			let temporaryValue;
 			let randomIndex;
 
@@ -269,11 +274,16 @@ class _UtilsModule extends CoreClass {
 				currentIndex -= 1;
 
 				// And swap it with the current element.
-				temporaryValue = array[currentIndex];
-				array[currentIndex] = array[randomIndex];
-				array[randomIndex] = temporaryValue;
+				temporaryValue = positions[currentIndex];
+				positions[currentIndex] = positions[randomIndex];
+				positions[randomIndex] = temporaryValue;
 			}
 
+			// assign new positions
+			array.forEach((song, index) => {
+				song.position = positions[index];
+			});
+
 			resolve({ array });
 		});
 	}

+ 7 - 2
frontend/src/components/modals/EditPlaylist.vue

@@ -248,7 +248,10 @@ export default {
 			this.socket = socket;
 
 			this.socket.emit("playlists.getPlaylist", this.editing, res => {
-				if (res.status === "success") this.playlist = res.data;
+				if (res.status === "success") {
+					this.playlist = res.data;
+					this.playlist.songs.sort((a, b) => a.position - b.position);
+				}
 				this.playlist.oldId = res.data._id;
 			});
 
@@ -382,7 +385,9 @@ export default {
 			this.socket.emit("playlists.shuffle", this.playlist._id, res => {
 				new Toast({ content: res.message, timeout: 4000 });
 				if (res.status === "success") {
-					this.playlist = res.data;
+					this.playlist.songs = res.data.songs.sort(
+						(a, b) => a.position - b.position
+					);
 				}
 			});
 		},