Browse Source

refactor: Replace deleted song with youtube video and prevent youtube videos with songs being deleted

Owen Diffey 2 years ago
parent
commit
e175563879
2 changed files with 113 additions and 99 deletions
  1. 110 98
      backend/logic/actions/songs.js
  2. 3 1
      backend/logic/youtube.js

+ 110 - 98
backend/logic/actions/songs.js

@@ -431,118 +431,130 @@ export default {
 				},
 
 				(song, next) => {
+					YouTubeModule.runJob("GET_VIDEO", { identifier: song.youtubeId, createMissing: true }, this)
+						.then(video => next(null, video))
+						.catch(next);
+				},
+
+				(youtubeVideo, next) => {
 					PlaylistsModule.runJob("GET_PLAYLISTS_WITH_SONG", { songId }, this)
-						.then(res => {
-							async.eachLimit(
-								res.playlists,
-								1,
-								(playlist, next) => {
-									WSModule.runJob(
-										"RUN_ACTION2",
-										{
-											session,
-											namespace: "playlists",
-											action: "removeSongFromPlaylist",
-											args: [song.youtubeId, playlist._id]
-										},
-										this
-									)
-										.then(res => {
-											if (res.status === "error") next(res.message);
-											else next();
-										})
-										.catch(err => {
-											next(err);
-										});
-								},
-								err => {
-									if (err) next(err);
-									else next(null, song);
-								}
-							);
-						})
-						.catch(err => next(err));
+						.then(res =>
+							next(
+								null,
+								youtubeVideo,
+								res.playlists.map(playlist => playlist._id)
+							)
+						)
+						.catch(next);
 				},
 
-				(song, next) => {
-					stationModel.find({ "queue._id": songId }, (err, stations) => {
-						if (err) next(err);
-						else {
-							async.eachLimit(
-								stations,
-								1,
-								(station, next) => {
-									StationsModule.runJob(
-										"REMOVE_FROM_QUEUE",
-										{ stationId: station._id, youtubeId: song.youtubeId },
-										this
-									)
-										.then(() => next())
-										.catch(err => {
-											if (
-												err === "Station not found" ||
-												err === "Song is not currently in the queue."
-											)
-												next();
-											else next(err);
-										});
-								},
-								err => {
-									if (err) next(err);
-									else next(null, song);
-								}
-							);
+				(youtubeVideo, playlistIds, next) => {
+					PlaylistsModule.playlistModel.updateMany(
+						{ "songs._id": songId },
+						{
+							$set: {
+								"songs.$._id": null,
+								"songs.$.title": youtubeVideo.title,
+								"songs.$.artists": [youtubeVideo.author],
+								"songs.$.duration": youtubeVideo.duration,
+								"songs.$.skipDuration": 0,
+								"songs.$.thumbnail": youtubeVideo.thumbnail,
+								"songs.$.verified": false
+							}
+						},
+						err => {
+							if (err) next(err);
+							next(null, youtubeVideo, playlistIds);
 						}
-					});
+					);
 				},
 
-				(song, next) => {
-					stationModel.find({ "currentSong._id": songId }, (err, stations) => {
-						if (err) next(err);
-						else {
-							async.eachLimit(
-								stations,
-								1,
-								(station, next) => {
-									StationsModule.runJob(
-										"SKIP_STATION",
-										{ stationId: station._id, natural: false },
-										this
-									)
-										.then(() => {
-											next();
-										})
-										.catch(err => {
-											if (err.message === "Station not found.") next();
-											else next(err);
-										});
-								},
-								err => {
-									if (err) next(err);
-									else next(null, song);
-								}
+				(youtubeVideo, playlistIds, next) => {
+					async.eachLimit(
+						playlistIds,
+						1,
+						(playlistId, next) => {
+							PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
+								.then(() => next())
+								.catch(next);
+						},
+						err => {
+							if (err) next(err);
+							else next(null, youtubeVideo);
+						}
+					);
+				},
+
+				(youtubeVideo, next) => {
+					stationModel.find(
+						{ $or: [{ "queue._id": songId }, { "currentSong._id": songId }] },
+						(err, stations) => {
+							if (err) next(err);
+							next(
+								null,
+								youtubeVideo,
+								stations.map(station => station._id)
 							);
 						}
-					});
+					);
 				},
 
-				(song, next) => {
-					RatingsModule.runJob("REMOVE_RATINGS", { youtubeIds: song.youtubeId }, this)
-						.then(() => next(null, song.youtubeId))
-						.catch(next);
+				(youtubeVideo, stationIds, next) => {
+					stationModel.updateMany(
+						{ "queue._id": songId },
+						{
+							$set: {
+								"queue.$._id": null,
+								"queue.$.title": youtubeVideo.title,
+								"queue.$.artists": [youtubeVideo.author],
+								"queue.$.duration": youtubeVideo.duration,
+								"queue.$.skipDuration": 0,
+								"queue.$.thumbnail": youtubeVideo.thumbnail,
+								"queue.$.verified": false
+							}
+						},
+						err => {
+							if (err) next(err);
+							next(null, youtubeVideo, stationIds);
+						}
+					);
 				},
 
-				(youtubeId, next) => {
-					YouTubeModule.youtubeVideoModel.findOne({ youtubeId }, (err, video) => {
-						if (err) next(err);
-						else next(null, video._id);
-					});
+				(youtubeVideo, stationIds, next) => {
+					stationModel.updateMany(
+						{ "currentSong._id": songId },
+						{
+							$set: {
+								"currentSong._id": null,
+								"currentSong.title": youtubeVideo.title,
+								"currentSong.artists": [youtubeVideo.author],
+								// "currentSong.duration": youtubeVideo.duration,
+								// "currentSong.skipDuration": 0,
+								"currentSong.thumbnail": youtubeVideo.thumbnail,
+								"currentSong.verified": false
+							}
+						},
+						err => {
+							if (err) next(err);
+							next(null, stationIds);
+						}
+					);
 				},
 
-				(videoIds, next) => {
-					YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
-						.then(() => next())
-						.catch(next);
+				(stationIds, next) => {
+					async.eachLimit(
+						stationIds,
+						1,
+						(stationId, next) => {
+							StationsModule.runJob("UPDATE_STATION", { stationId }, this)
+								.then(() => next())
+								.catch(next);
+						},
+						err => {
+							if (err) next(err);
+							else next();
+						}
+					);
 				},
 
 				next => {

+ 3 - 1
backend/logic/youtube.js

@@ -1216,7 +1216,9 @@ class _YouTubeModule extends CoreClass {
 						SongsModule.SongModel.find({ youtubeId: { $in: youtubeIds } }, (err, songs) => {
 							if (err) next(err);
 							else {
-								next(null, youtubeIds.filter(youtubeId => !songs.find(song => song.youtubeId === youtubeId)));
+								const filteredIds = youtubeIds.filter(youtubeId => !songs.find(song => song.youtubeId === youtubeId));
+								if (filteredIds.length < youtubeIds.length) next("One or more videos are attached to songs.");
+								else next(null, filteredIds);
 							}
 						});
 					},