Kaynağa Gözat

Moved total playlist duration calculation from backed to frontend.

KrisVos130 7 yıl önce
ebeveyn
işleme
c3d9ce0733

+ 1 - 63
backend/logic/actions/playlists.js

@@ -22,25 +22,6 @@ cache.sub('playlist.create', playlistId => {
 	});
 });
 
-cache.sub('playlist.updateTotalLength', res => {
-	playlists.getPlaylist(res.playlistId, (err, playlist) => {
-		if (!err) {
-			let totalLength = 0;
-			for (let i = 0; i < playlist.songs.length; i++) {
-				totalLength += playlist.songs[i].duration;
-			}
-			utils.socketsFromUser(res.userId, sockets => {
-				sockets.forEach(socket => {
-					socket.emit('event:playlist.updateTotalLength', {
-						status: 'success',
-						data: totalLength
-					});
-				});
-			});
-		}
-	});
-});
-
 cache.sub('playlist.delete', res => {
 	utils.socketsFromUser(res.userId, sockets => {
 		sockets.forEach(socket => {
@@ -218,46 +199,6 @@ let lib = {
 		});
 	}),
 
-	/**
-	 * Gets the total length of a playlist from id
-	 *
-	 * @param {Object} session - the session object automatically added by socket.io
-	 * @param {String} playlistId - the id of the playlist we are getting
-	 * @param {Function} cb - gets called with the result
-	 */
-	totalLength: hooks.loginRequired((session, playlistId, cb) => {
-		async.waterfall([
-			(next) => {
-				playlists.getPlaylist(playlistId, next);
-			},
-
-			(playlist, next) => {
-				if (!playlist) return next('Playlist not found');
-				next(null, playlist);
-			},
-
-			(playlist, next) => {
-				let totalLength = 0;
-				for (let i = 0; i < playlist.songs.length; i++) {
-					totalLength += playlist.songs[i].duration;
-				}
-				next(null, totalLength);
-			}
-		], (err, totalLength) => {
-			if (err) {
-				err = utils.getError(err);
-				logger.error("PLAYLIST_TOTAL_LENGTH", `Getting private playlist "${playlistId}" failed. "${err}"`);
-				return cb({ status: 'failure', message: err});
-			} else{
-				logger.success("PLAYLIST_TOTAL_LENGTH", `Successfully got private playlist "${playlistId}" length.`);
-				cb({
-					status: 'success',
-					data: totalLength
-				});
-			}
-		});
-	}),
-
 	//TODO Remove this
 	/**
 	 * Updates a private playlist
@@ -345,7 +286,6 @@ let lib = {
 			} else {
 				logger.success("PLAYLIST_ADD_SONG", `Successfully added song "${songId}" to private playlist "${playlistId}" for user "${userId}".`);
 				cache.pub('playlist.addSong', { playlistId: playlist._id, song: newSong, userId });
-				cache.pub('playlist.updateTotalLength', { playlistId: playlist._id, userId });
 				return cb({ status: 'success', message: 'Song has been successfully added to the playlist', data: playlist.songs });
 			}
 		});
@@ -393,7 +333,6 @@ let lib = {
 				logger.error("PLAYLIST_IMPORT", `Importing a YouTube playlist to private playlist "${playlistId}" failed for user "${userId}". "${err}"`);
 				return cb({ status: 'failure', message: err});
 			} else {
-				cache.pub('playlist.updateTotalLength', { playlistId: playlist._id, userId });
 				logger.success("PLAYLIST_IMPORT", `Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${userId}".`);
 				cb({ status: 'success', message: 'Playlist has been successfully imported.', data: playlist.songs });
 			}
@@ -436,8 +375,7 @@ let lib = {
 				return cb({ status: 'failure', message: err});
 			} else {
 				logger.success("PLAYLIST_REMOVE_SONG", `Successfully removed song "${songId}" from private playlist "${playlistId}" for user "${userId}".`);
-				cache.pub('playlist.removeSong', { playlistId: playlist._id, songId: songId, userId });
-				cache.pub('playlist.updateTotalLength', { playlistId: playlist._id, userId });
+				cache.pub('playlist.removeSong', { playlistId: playlist._id, songId: songId, userId });\
 				return cb({ status: 'success', message: 'Song has been successfully removed from playlist', data: playlist.songs });
 			}
 		});

+ 12 - 12
frontend/components/Modals/Playlists/Edit.vue

@@ -5,7 +5,7 @@
 				<div class="level-item has-text-centered">
 					<div>
 						<p class="heading">Total Length</p>
-						<p class="title">{{ formatTime(totalLength) }}</p>
+						<p class="title">{{ totalLength() }}</p>
 					</div>
 				</div>
 			</nav>
@@ -86,19 +86,25 @@
 		components: { Modal },
 		data() {
 			return {
-				playlist: {},
-				totalLength: 0,
+				playlist: {songs: []},
 				songQueryResults: [],
 				songQuery: '',
 				importQuery: ''
 			}
 		},
 		methods: {
-			formatTime: function () {
-				let duration = moment.duration(this.totalLength, 'seconds');
-				if (this.totalLength < 0) return '0 minutes';
+			formatTime: function (length) {
+				let duration = moment.duration(length, 'seconds');
+				if (length < 0) return '0 minutes';
 				else return ((duration.hours() > 0 ? (duration.hours > 1 ? (duration.hours() < 10 ? ('0' + duration.hours() + ' hours ') : (duration.hours() + ' hours ')) : ('0' + duration.hours() + ' hour ')) : '') + (duration.minutes() > 0 ? (duration.minutes() > 1 ? (duration.minutes() < 10 ? ('0' + duration.minutes() + ' minutes ') : (duration.minutes() + ' minutes ')) : ('0' + duration.minutes() + ' minute ')) : '') + (duration.seconds() > 0 ? (duration.seconds() > 1 ? (duration.seconds() < 10 ? ('0' + duration.seconds() + ' seconds ') : (duration.seconds() + ' seconds ')) : ('0' + duration.seconds() + ' second ')) : ''));
 			},
+			totalLength: function() {
+			    let length = 0;
+			    this.playlist.songs.forEach((song) => {
+			        length += song.duration;
+				});
+			    return this.formatTime(length);
+			},
 			searchForSongs: function () {
 				let _this = this;
 				let query = _this.songQuery;
@@ -185,12 +191,6 @@
 				_this.socket.emit('playlists.getPlaylist', _this.$parent.playlistBeingEdited, res => {
 					if (res.status === 'success') _this.playlist = res.data; _this.playlist.oldId = res.data._id;
 				});
-				_this.socket.emit('playlists.totalLength', _this.$parent.playlistBeingEdited, res => {
-					if (res.status === 'success') _this.totalLength = res.data;
-				});
-				_this.socket.on('event:playlist.updateTotalLength', res => {
-					if (res.status === 'success') _this.totalLength = res.data;
-				});
 				_this.socket.on('event:playlist.addSong', data => {
 					if (_this.playlist._id === data.playlistId) _this.playlist.songs.push(data.song);
 				});