Browse Source

Added button to clear and refill the station queue

Kristian Vos 3 years ago
parent
commit
231f4fbe41

+ 36 - 0
backend/logic/actions/stations.js

@@ -3361,5 +3361,41 @@ export default {
 				return cb({ status: "success", message: "Success" });
 			}
 		);
+	}),
+
+	/**
+	 * Clears and refills a station queue
+	 *
+	 * @param {object} session - the session object automatically added by socket.io
+	 * @param {string} stationId - the station id
+	 * @param {Function} cb - gets called with the result
+	 */
+	clearAndRefillStationQueue: isAdminRequired(async function clearAndRefillStationQueue(session, stationId, cb) {
+		async.waterfall(
+			[
+				next => {
+					StationsModule.runJob("CLEAR_AND_REFILL_STATION_QUEUE", { stationId }, this)
+						.then(() => next())
+						.catch(next);
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"CLEAR_AND_REFILL_STATION_QUEUE",
+						`Clearing and refilling station queue failed. "${err}"`
+					);
+					return cb({ status: "failure", message: err });
+				}
+				this.log(
+					"SUCCESS",
+					"CLEAR_AND_REFILL_STATION_QUEUE",
+					"Clearing and refilling station queue was successfull."
+				);
+				return cb({ status: "success", message: "Success" });
+			}
+		);
 	})
 };

+ 43 - 1
backend/logic/stations.js

@@ -464,11 +464,12 @@ class _StationsModule extends CoreClass {
 	 *
 	 * @param {object} payload - object that contains the payload
 	 * @param {string} payload.stationId - the id of the station
+	 * @param {string} payload.ignoreExistingQueue - ignore the existing queue songs, replacing the old queue with a completely fresh one
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST(payload) {
 		return new Promise((resolve, reject) => {
-			const { stationId } = payload;
+			const { stationId, ignoreExistingQueue } = payload;
 			async.waterfall(
 				[
 					next => {
@@ -482,6 +483,7 @@ class _StationsModule extends CoreClass {
 					(playlist, next) => {
 						StationsModule.runJob("GET_STATION", { stationId }, this)
 							.then(station => {
+								if (ignoreExistingQueue) station.queue = [];
 								next(null, playlist, station);
 							})
 							.catch(next);
@@ -510,6 +512,7 @@ class _StationsModule extends CoreClass {
 						const currentSongIds = station.queue.map(song => song.songId);
 						const songsToAdd = [];
 						let lastSongAdded = null;
+						console.log(123123, _playlistSongs, currentSongs, songsStillNeeded);
 
 						playlistSongs
 							// .map(song => song._doc)
@@ -1628,6 +1631,45 @@ class _StationsModule extends CoreClass {
 			);
 		});
 	}
+
+	/**
+	 * Clears and refills a station queue
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {string} payload.stationId - the station id
+	 * @returns {Promise} - returns a promise (resolve, reject)
+	 */
+	CLEAR_AND_REFILL_STATION_QUEUE(payload) {
+		return new Promise((resolve, reject) => {
+			async.waterfall(
+				[
+					next => {
+						StationsModule.runJob(
+							"FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
+							{ stationId: payload.stationId, ignoreExistingQueue: true },
+							this
+						)
+							.then(() => {
+								CacheModule.runJob("PUB", {
+									channel: "station.queueUpdate",
+									value: payload.stationId
+								})
+									.then()
+									.catch();
+								next();
+							})
+							.catch(err => {
+								next(err);
+							});
+					}
+				],
+				err => {
+					if (err) reject(err);
+					else resolve();
+				}
+			);
+		});
+	}
 }
 
 export default new _StationsModule();

+ 21 - 0
frontend/src/components/modals/EditStation.vue

@@ -480,6 +480,13 @@
 			>
 				Delete station
 			</button>
+
+			<button
+				class="button is-danger"
+				@click="clearAndRefillStationQueue()"
+			>
+				Clear and refill station queue
+			</button>
 		</template>
 	</modal>
 </template>
@@ -1071,6 +1078,20 @@ export default {
 			else if (type === "blacklist-genres")
 				this.station.blacklistedGenres.splice(index, 1);
 		},
+		clearAndRefillStationQueue() {
+			this.socket.dispatch(
+				"stations.clearAndRefillStationQueue",
+				this.station._id,
+				data => {
+					if (data.status !== "success")
+						new Toast({
+							content: `Error: ${data.message}`,
+							timeout: 8000
+						});
+					else new Toast({ content: data.message, timeout: 4000 });
+				}
+			);
+		},
 		...mapActions("modals/editStation", [
 			"editStation",
 			"setGenres",