Ver código fonte

Improved importing YouTube playlists

Kristian Vos 4 anos atrás
pai
commit
1b74aa3005

+ 21 - 24
backend/logic/actions/playlists.js

@@ -599,8 +599,7 @@ export default {
 	addSetToPlaylist: isLoginRequired(function addSetToPlaylist(session, url, playlistId, musicOnly, cb) {
 		let videosInPlaylistTotal = 0;
 		let songsInPlaylistTotal = 0;
-		let songsSuccess = 0;
-		let songsFail = 0;
+		let addSongsStats = null;
 
 		const addedSongs = [];
 
@@ -626,41 +625,41 @@ export default {
 				},
 				(songIds, next) => {
 					let processed = 0;
+					let successful = 0;
+					let failed = 0;
+					let alreadyInPlaylist = 0;
 
-					/**
-					 *
-					 */
-					function checkDone() {
-						if (processed === songIds.length) next();
-					}
-					for (let s = 0; s < songIds.length; s += 1) {
+					if (songIds.length === 0) next();
+
+					songIds.forEach(songId => {
 						IOModule.runJob(
 							"RUN_ACTION2",
 							{
 								session,
 								namespace: "playlists",
 								action: "addSongToPlaylist",
-								args: [true, songIds[s], playlistId]
+								args: [true, songId, playlistId]
 							},
 							this
 						)
-							// eslint-disable-next-line no-loop-func
 							.then(res => {
 								if (res.status === "success") {
-									addedSongs.push(songIds[s]);
-									songsSuccess += 1;
-								} else songsFail += 1;
+									successful += 1;
+									addedSongs.push(songId);
+								} else failed += 1;
+								if (res.message === "That song is already in the playlist") alreadyInPlaylist += 1;
 							})
-							// eslint-disable-next-line no-loop-func
 							.catch(() => {
-								songsFail += 1;
+								failed += 1;
 							})
-							// eslint-disable-next-line no-loop-func
 							.finally(() => {
 								processed += 1;
-								checkDone();
+								if (processed === songIds.length) {
+									addSongsStats = { successful, failed, alreadyInPlaylist };
+									next(null);
+								}
 							});
-					}
+					});
 				},
 
 				next => {
@@ -694,17 +693,15 @@ export default {
 				this.log(
 					"SUCCESS",
 					"PLAYLIST_IMPORT",
-					`Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${session.userId}". Videos in playlist: ${videosInPlaylistTotal}, songs in playlist: ${songsInPlaylistTotal}, songs successfully added: ${songsSuccess}, songs failed: ${songsFail}.`
+					`Successfully imported a YouTube playlist to private playlist "${playlistId}" for user "${session.userId}". Videos in playlist: ${videosInPlaylistTotal}, songs in playlist: ${songsInPlaylistTotal}, songs successfully added: ${addSongsStats.successful}, songs failed: ${addSongsStats.failed}, already in playlist: ${addSongsStats.alreadyInPlaylist}.`
 				);
 				return cb({
 					status: "success",
-					message: "Playlist has been successfully imported.",
+					message: `Playlist has been imported. ${addSongsStats.successful} were addedd successfully, ${addSongsStats.failed} failed (${addSongsStats.alreadyInPlaylist} were already in the playlist)`,
 					data: playlist.songs,
 					stats: {
 						videosInPlaylistTotal,
-						songsInPlaylistTotal,
-						songsAddedSuccessfully: songsSuccess,
-						songsFailedToAdd: songsFail
+						songsInPlaylistTotal
 					}
 				});
 			}

+ 25 - 12
backend/logic/actions/queueSongs.js

@@ -351,12 +351,13 @@ export default {
 				},
 				(songIds, next) => {
 					let processed = 0;
-					/**
-					 *
-					 */
-					function checkDone() {
-						if (processed === songIds.length) next();
-					}
+					let successful = 0;
+					let failed = 0;
+					let alreadyInQueue = 0;
+					let alreadyAdded = 0;
+
+					if (songIds.length === 0) next();
+
 					songIds.forEach(songId => {
 						IOModule.runJob(
 							"RUN_ACTION2",
@@ -367,14 +368,26 @@ export default {
 								args: [songId]
 							},
 							this
-						).finally(() => {
-							processed += 1;
-							checkDone();
-						});
+						)
+							.then(res => {
+								if (res.status === "success") successful += 1;
+								else failed += 1;
+								if (res.message === "This song is already in the queue.") alreadyInQueue += 1;
+								if (res.message === "This song has already been added.") alreadyAdded += 1;
+							})
+							.catch(() => {
+								failed += 1;
+							})
+							.finally(() => {
+								processed += 1;
+								if (processed === songIds.length) {
+									next(null, { successful, failed, alreadyInQueue, alreadyAdded });
+								}
+							});
 					});
 				}
 			],
-			async err => {
+			async (err, response) => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log(
@@ -391,7 +404,7 @@ export default {
 				);
 				return cb({
 					status: "success",
-					message: "Playlist has been successfully imported."
+					message: `Playlist is done importing. ${response.successful} were added succesfully, ${response.failed} failed (${response.alreadyInQueue} were already in queue, ${response.alreadyAdded} were already added)`
 				});
 			}
 		);

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

@@ -310,16 +310,12 @@ export default {
 				this.playlist._id,
 				musicOnly,
 				res => {
-					new Toast({ content: res.message, timeout: 4000 });
+					new Toast({ content: res.message, timeout: 20000 });
 					if (res.status === "success") {
-						new Toast({
-							content: `Successfully added ${res.stats.songsAddedSuccessfully} songs. Failed to add ${res.stats.songsFailedToAdd} songs.`,
-							timeout: 4000
-						});
 						if (musicOnly) {
 							new Toast({
 								content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
-								timeout: 4000
+								timeout: 20000
 							});
 						}
 					}

+ 1 - 1
frontend/src/pages/Station/AddSongToQueue.vue

@@ -324,7 +324,7 @@ export default {
 				this.isImportingOnlyMusicOfPlaylist,
 				res => {
 					isImportingPlaylist = false;
-					return new Toast({ content: res.message, timeout: 4000 });
+					return new Toast({ content: res.message, timeout: 20000 });
 				}
 			);
 		},