Pārlūkot izejas kodu

refactor: re-added shuffle code

Kristian Vos 3 gadi atpakaļ
vecāks
revīzija
677cd89787

+ 61 - 0
backend/logic/actions/playlists.js

@@ -783,6 +783,67 @@ export default {
 		);
 	},
 
+	/**
+	 * Shuffles songs in a private playlist
+	 *
+	 * @param {object} session - the session object automatically added by the websocket
+	 * @param {string} playlistId - the id of the playlist we are updating
+	 * @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);
+
+		async.waterfall(
+			[
+				next => {
+					if (!playlistId) return next("No playlist id.");
+					return playlistModel.findById(playlistId, next);
+				},
+
+				(playlist, next) => {
+					if (!playlist.isUserModifiable) return next("Playlist cannot be shuffled.");
+
+					return UtilsModule.runJob("SHUFFLE_SONG_POSITIONS", { array: playlist.songs }, this)
+						.then(result => next(null, result.array))
+						.catch(next);
+				},
+
+				(songs, next) => {
+					playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
+				},
+
+				(res, next) => {
+					PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
+						.then(playlist => next(null, playlist))
+						.catch(next);
+				}
+			],
+			async (err, playlist) => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"PLAYLIST_SHUFFLE",
+						`Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+
+				this.log(
+					"SUCCESS",
+					"PLAYLIST_SHUFFLE",
+					`Successfully updated private playlist "${playlistId}" for user "${session.userId}".`
+				);
+
+				return cb({
+					status: "success",
+					message: "Successfully shuffled playlist.",
+					data: { playlist }
+				});
+			}
+		);
+	}),
+
 	/**
 	 * Changes the order (position) of a song in a playlist
 	 *

+ 16 - 0
frontend/src/components/modals/EditPlaylist/index.vue

@@ -442,6 +442,22 @@ export default {
 			});
 			return this.utils.formatTimeLong(length);
 		},
+		shuffle() {
+			this.socket.dispatch(
+				"playlists.shuffle",
+				this.playlist._id,
+				res => {
+					new Toast(res.message);
+					if (res.status === "success") {
+						this.updatePlaylistSongs(
+							res.data.playlist.songs.sort(
+								(a, b) => a.position - b.position
+							)
+						);
+					}
+				}
+			);
+		},
 		removeSongFromPlaylist(id) {
 			if (this.playlist.type === "user-liked")
 				return this.socket.dispatch("songs.unlike", id, res => {