Browse Source

feat: added a way to shuffle a playlist

Kristian Vos 5 years ago
parent
commit
1ca29427f2
2 changed files with 51 additions and 0 deletions
  1. 42 0
      backend/logic/actions/playlists.js
  2. 9 0
      frontend/components/Modals/Playlists/Edit.vue

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

@@ -231,6 +231,48 @@ let lib = {
 		});
 	}),
 
+	/**
+	 * Updates a private playlist
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {String} playlistId - the id of the playlist we are updating
+	 * @param {Function} cb - gets called with the result
+	 */
+	shuffle: hooks.loginRequired((session, playlistId, cb) => {
+		async.waterfall([
+			(next) => {
+				if (!playlistId) return next("No playlist id.");
+				db.models.playlist.findById(playlistId, next);
+			},
+
+			(playlist, next) => {
+				utils.shuffle(playlist.songs).then(songs => {
+					next(null, songs);
+				}).catch(next);
+			},
+
+			(songs, next) => {
+				db.models.playlist.updateOne({ _id: playlistId }, { $set: { songs } }, { runValidators: true }, next);
+			},
+
+			(res, next) => {
+				playlists.updatePlaylist(playlistId, next)
+			}
+		], async (err, playlist) => {
+			if (err) {
+				err = await utils.getError(err);
+				logger.error("PLAYLIST_SHUFFLE", `Updating private playlist "${playlistId}" failed for user "${session.userId}". "${err}"`);
+				return cb({ status: 'failure', message: err});
+			}
+			logger.success("PLAYLIST_SHUFFLE", `Successfully updated private playlist "${playlistId}" for user "${session.userId}".`);
+			cb({
+				status: 'success',
+				message: "Successfully shuffled playlist.",
+				data: playlist
+			});
+		});
+	}),
+
 	/**
 	 * Adds a song to a private playlist
 	 *

+ 9 - 0
frontend/components/Modals/Playlists/Edit.vue

@@ -136,6 +136,7 @@
 					>
 				</p>
 			</div>
+			<button class="button is-info" @click="shuffle()">Shuffle</button>
 			<h5>Edit playlist details:</h5>
 			<div class="control is-grouped">
 				<p class="control is-expanded">
@@ -331,6 +332,14 @@ export default {
 			this.addSongToPlaylist(id);
 		},
 		/* eslint-enable prefer-destructuring */
+		shuffle() {
+			this.socket.emit("playlists.shuffle", this.playlist._id, res => {
+				new Toast({ content: res.message, timeout: 4000 });
+				if (res.status === "success") {
+					this.playlist = res.data;
+				}
+			});
+		},
 		importPlaylist(musicOnly) {
 			new Toast({
 				content: