Ver código fonte

Added job to autofill a station playlist

Kristian Vos 4 anos atrás
pai
commit
32691c64ca
2 arquivos alterados com 114 adições e 6 exclusões
  1. 108 0
      backend/logic/playlists.js
  2. 6 6
      backend/logic/stations.js

+ 108 - 0
backend/logic/playlists.js

@@ -446,6 +446,114 @@ class _PlaylistsModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Fills a station playlist with songs
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.stationId - the station id
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	AUTOFILL_STATION_PLAYLIST(payload) {
+		return new Promise((resolve, reject) => {
+			async.waterfall(
+				[
+					next => {
+						if (!payload.stationId) next("Please specify a station id");
+						else next();
+					},
+
+					next => {
+						StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
+							.then(station => {
+								next(null, station);
+							})
+							.catch(next);
+					},
+
+					(station, next) => {
+						PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist2 }, this)
+							.then(() => {
+								next(null, station);
+							})
+							.catch(err => {
+								next(err);
+							});
+					},
+
+					(station, next) => {
+						const includedPlaylists = [];
+						async.eachLimit(
+							station.includedPlaylists,
+							1,
+							(playlistId, next) => {
+								PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
+									.then(playlist => {
+										includedPlaylists.push(playlist);
+										next();
+									})
+									.catch(next);
+							},
+							err => {
+								next(err, station, includedPlaylists);
+							}
+						);
+					},
+
+					(station, includedPlaylists, next) => {
+						const excludedPlaylists = [];
+						async.eachLimit(
+							station.excludedPlaylists,
+							1,
+							(playlistId, next) => {
+								PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
+									.then(playlist => {
+										excludedPlaylists.push(playlist);
+										next();
+									})
+									.catch(next);
+							},
+							err => {
+								next(err, station, includedPlaylists, excludedPlaylists);
+							}
+						);
+					},
+
+					(station, includedPlaylists, excludedPlaylists, next) => {
+						const excludedSongs = excludedPlaylists
+							.flatMap(excludedPlaylist => excludedPlaylist.songs)
+							.reduce(
+								(items, item) =>
+									items.find(x => x.songId === item.songId) ? [...items] : [...items, item],
+								[]
+							);
+						const includedSongs = includedPlaylists
+							.flatMap(includedPlaylist => includedPlaylist.songs)
+							.reduce(
+								(songs, song) =>
+									songs.find(x => x.songId === song.songId) ? [...songs] : [...songs, song],
+								[]
+							)
+							.filter(song => !excludedSongs.find(x => x.songId === song.songId));
+
+						next(null, station, includedSongs);
+					},
+
+					(station, includedSongs, next) => {
+						PlaylistsModule.playlistModel.updateOne(
+							{ _id: station.playlist2 },
+							{ $set: { songs: includedSongs } },
+							next
+						);
+					}
+				],
+				err => {
+					if (err && err !== true) return reject(new Error(err));
+					return resolve({});
+				}
+			);
+		});
+	}
+
 	/**
 	 * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
 	 *

+ 6 - 6
backend/logic/stations.js

@@ -1252,7 +1252,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					next => {
-						StationsModule.runJob("GET_STATION", { stationId: payload.stationId })
+						StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
 							.then(station => {
 								next(null, station);
 							})
@@ -1268,7 +1268,7 @@ class _StationsModule extends CoreClass {
 								"This playlist is currently excluded, please remove it from there before including it"
 							);
 						else
-							PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId })
+							PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
 								.then(() => {
 									next(null);
 								})
@@ -1336,7 +1336,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					next => {
-						StationsModule.runJob("GET_STATION", { stationId: payload.stationId })
+						StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
 							.then(station => {
 								next(null, station);
 							})
@@ -1410,7 +1410,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					next => {
-						StationsModule.runJob("GET_STATION", { stationId: payload.stationId })
+						StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
 							.then(station => {
 								next(null, station);
 							})
@@ -1426,7 +1426,7 @@ class _StationsModule extends CoreClass {
 								"This playlist is currently included, please remove it from there before excluding it"
 							);
 						else
-							PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId })
+							PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
 								.then(() => {
 									next(null);
 								})
@@ -1494,7 +1494,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					next => {
-						StationsModule.runJob("GET_STATION", { stationId: payload.stationId })
+						StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
 							.then(station => {
 								next(null, station);
 							})