Browse Source

Improvements to station local skipping

Kristian Vos 3 years ago
parent
commit
e55c429484

+ 11 - 113
backend/logic/actions/stations.js

@@ -1013,85 +1013,6 @@ export default {
 		);
 	},
 
-	getNextSongInfo(session, stationId, songId, cb) {
-		async.waterfall(
-			[
-				next => {
-					StationsModule.runJob("GET_STATION", { stationId }, this)
-						.then(station => next(null, station))
-						.catch(err => {
-							next(err);
-						});
-				},
-
-				(station, next) => {
-					if (!station) return next("Station not found.");
-					return StationsModule.runJob("CAN_USER_VIEW_STATION", { station, userId: session.userId }, this)
-						.then(canView => {
-							if (!canView) next("Not allowed to get info about station.");
-							else next(null, station);
-						})
-						.catch(err => {
-							console.log(stationId, station, songId);
-							next(err);
-						});
-				},
-
-				(station, next) => {
-					if (
-						station.currentSong._id === songId ||
-						(station.queue.length > 0 && station.queue[0]._id === songId) ||
-						(station.queue.length > 1 && station.queue[1]._id === songId)
-					) {
-						next(null);
-					} else next("That song is not next.");
-				},
-
-				next => {
-					SongsModule.runJob("GET_SONG", { songId }, this)
-						.then(response => {
-							const { song } = response;
-							const nextSong = {
-								_id: song._id,
-								youtubeId: song.youtubeId,
-								title: song.title,
-								artists: song.artists,
-								duration: song.duration,
-								likes: song.likes,
-								dislikes: song.dislikes,
-								skipDuration: song.skipDuration,
-								thumbnail: song.thumbnail,
-								requestedAt: song.requestedAt,
-								requestedBy: song.requestedBy,
-								status: song.status
-							};
-							next(null, { nextSong });
-						})
-						.catch(err => {
-							next(err);
-						});
-				}
-			],
-			async (err, data) => {
-				if (err) {
-					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
-					this.log(
-						"ERROR",
-						"STATIONS_GET_NEXT_SONG_INFO",
-						`Getting next song info for station "${stationId}" failed. "${err}"`
-					);
-					return cb({ status: "error", message: err });
-				}
-
-				this.log(
-					"SUCCESS",
-					"STATIONS_GET_NEXT_SONG_INFO",
-					`Got next song info for station "${stationId}" successfully.`
-				);
-				return cb({ status: "success", data });
-			}
-		);
-	},
 
 	/**
 	 * Gets a station by id
@@ -1426,14 +1347,14 @@ export default {
 				(station, next) => {
 					skipVotes = station.currentSong.skipVotes.length;
 					WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: `station.${stationId}` }, this)
-						.then(sockets => next(null, station, sockets))
+						.then(sockets => next(null, sockets))
 						.catch(next);
 				},
 
-				(station, sockets, next) => {
+				(sockets, next) => {
 					if (sockets.length <= skipVotes) {
 						shouldSkip = true;
-						return next(null, station);
+						return next();
 					}
 
 					const users = [];
@@ -1454,12 +1375,12 @@ export default {
 							if (err) return next(err);
 
 							if (users.length <= skipVotes) shouldSkip = true;
-							return next(null, station);
+							return next();
 						}
 					);
 				}
 			],
-			async (err, station) => {
+			async err => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log("ERROR", "STATIONS_VOTE_SKIP", `Vote skipping station "${stationId}" failed. "${err}"`);
@@ -1473,19 +1394,7 @@ export default {
 				});
 
 				if (shouldSkip) {
-					console.log(111, station);
-					WSModule.runJob("EMIT_TO_ROOM", {
-						room: `station.${station._id}`,
-						args: [
-							"event:songs.skip",
-							{
-								data: {
-									skippedSong: station.currentSong
-								}
-							}
-						]
-					});
-					StationsModule.runJob("SKIP_STATION", { stationId });
+					StationsModule.runJob("SKIP_STATION", { stationId, natural: false });
 				}
 
 				return cb({
@@ -1516,27 +1425,16 @@ export default {
 
 				(station, next) => {
 					if (!station) return next("Station not found.");
-					return next(null, station);
+					return next();
 				}
 			],
-			async (err, station) => {
+			async err => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log("ERROR", "STATIONS_FORCE_SKIP", `Force skipping station "${stationId}" failed. "${err}"`);
 					return cb({ status: "error", message: err });
 				}
-				WSModule.runJob("EMIT_TO_ROOM", {
-					room: `station.${station._id}`,
-					args: [
-						"event:songs.skip",
-						{
-							data: {
-								skippedSong: station.currentSong
-							}
-						}
-					]
-				});
-				StationsModule.runJob("SKIP_STATION", { stationId });
+				StationsModule.runJob("SKIP_STATION", { stationId, natural: false });
 				this.log("SUCCESS", "STATIONS_FORCE_SKIP", `Force skipped station "${stationId}" successfully.`);
 				return cb({
 					status: "success",
@@ -2312,7 +2210,7 @@ export default {
 					}
 				});
 
-				StationsModule.runJob("SKIP_STATION", { stationId });
+				StationsModule.runJob("SKIP_STATION", { stationId, natural: false });
 
 				return cb({
 					status: "success",
@@ -2390,7 +2288,7 @@ export default {
 						playMode: newPlayMode
 					}
 				});
-				StationsModule.runJob("SKIP_STATION", { stationId });
+				StationsModule.runJob("SKIP_STATION", { stationId, natural: false });
 				return cb({
 					status: "success",
 					message: "Successfully updated the play mode."

+ 1 - 1
backend/logic/playlists.js

@@ -812,7 +812,7 @@ class _PlaylistsModule extends CoreClass {
 
 					(includedSongs, next) => {
 						if (originalPlaylist.songs.length === 0 && includedSongs.length > 0)
-							StationsModule.runJob("SKIP_STATION", { stationId: payload.stationId });
+							StationsModule.runJob("SKIP_STATION", { stationId: payload.stationId, natural: false });
 						next();
 					}
 				],

+ 9 - 4
backend/logic/stations.js

@@ -242,7 +242,8 @@ class _StationsModule extends CoreClass {
 									name: `stations.nextSong?id=${station._id}`,
 									cb: () =>
 										StationsModule.runJob("SKIP_STATION", {
-											stationId: station._id
+											stationId: station._id,
+											natural: true
 										}),
 									unique: true,
 									station
@@ -260,7 +261,8 @@ class _StationsModule extends CoreClass {
 							return StationsModule.runJob(
 								"SKIP_STATION",
 								{
-									stationId: station._id
+									stationId: station._id,
+									natural: false
 								},
 								this
 							)
@@ -280,7 +282,8 @@ class _StationsModule extends CoreClass {
 							return StationsModule.runJob(
 								"SKIP_STATION",
 								{
-									stationId: station._id
+									stationId: station._id,
+									natural: false
 								},
 								this
 							)
@@ -695,6 +698,7 @@ class _StationsModule extends CoreClass {
 	 *
 	 * @param {object} payload - object that contains the payload
 	 * @param {string} payload.stationId - the id of the station to skip
+	 * @param {string} payload.natural - whether to skip naturally or forcefully
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	SKIP_STATION(payload) {
@@ -953,7 +957,8 @@ class _StationsModule extends CoreClass {
 										currentSong: station.currentSong,
 										startedAt: station.startedAt,
 										paused: station.paused,
-										timePaused: 0
+										timePaused: 0,
+										natural: payload.natural
 									}
 								}
 							]

+ 40 - 71
frontend/src/pages/Station/index.vue

@@ -691,7 +691,8 @@ export default {
 			activityWatchVideoDataInterval: null,
 			activityWatchVideoLastStatus: "",
 			activityWatchVideoLastYouTubeId: "",
-			activityWatchVideoLastStartDuration: ""
+			activityWatchVideoLastStartDuration: "",
+			nextCurrentSong: null
 		};
 	},
 	computed: {
@@ -760,16 +761,15 @@ export default {
 			}
 		);
 
-		this.socket.on("event:songs.skip", res => {
-			const { skippedSong } = res.data;
-			if (this.currentSong._id === skippedSong._id) {
-				this.skipSong();
-			}
-		});
-
 		this.socket.on("event:songs.next", res => {
-			const { currentSong, startedAt, paused, timePaused } = res.data;
-			if (this.noSong) {
+			const {
+				currentSong,
+				startedAt,
+				paused,
+				timePaused,
+				naturel
+			} = res.data;
+			if (this.noSong || !naturel) {
 				this.setCurrentSong({
 					currentSong,
 					startedAt,
@@ -777,12 +777,13 @@ export default {
 					timePaused,
 					pausedAt: 0
 				});
-			} else if (
-				this.nextSong &&
-				currentSong &&
-				this.nextSong._id === currentSong._id
-			) {
-				this.setNextSong(currentSong);
+			} else {
+				this.setNextCurrentSong({
+					currentSong,
+					startedAt,
+					paused,
+					timePaused
+				});
 			}
 		});
 
@@ -865,7 +866,7 @@ export default {
 					? this.songsList[0]
 					: null;
 
-			this.setNextSong(nextSong);
+			this.updateNextSong(nextSong);
 
 			this.addPartyPlaylistSongToQueue();
 		});
@@ -879,7 +880,7 @@ export default {
 					? this.songsList[0]
 					: null;
 
-			this.setNextSong(nextSong);
+			this.updateNextSong(nextSong);
 		});
 
 		this.socket.on("event:song.voteSkipSong", () => {
@@ -1021,54 +1022,16 @@ export default {
 				}
 			);
 		},
-		setNextSong(nextSong) {
-			if (
-				nextSong &&
-				(!this.nextSong || this.nextSong._id !== nextSong._id) &&
-				(!this.noSong && nextSong._id !== this.currentSong._id)
-			) {
-				this.updateNextSong(nextSong);
-
-				this.socket.dispatch(
-					"stations.getNextSongInfo",
-					this.station._id,
-					nextSong._id,
-					data => {
-						if (data.status === "success") {
-							if (
-								this.nextSong &&
-								this.nextSong._id === nextSong._id
-							) {
-								this.updateNextSong(data.data.nextSong);
-							}
-						}
-					}
-				);
-			}
-			if (!nextSong) {
-				this.updateNextSong(null);
+		setNextCurrentSong(nextCurrentSong) {
+			this.nextCurrentSong = nextCurrentSong;
+			if (this.getTimeRemaining() <= 0) {
+				this.skipSong();
 			}
 		},
 		skipSong() {
-			if (this.nextSong) {
-				this.setCurrentSong({
-					currentSong: {
-						...this.nextSong,
-						skipVotes: 0
-					},
-					startedAt: Date.now(),
-					paused: this.stationPaused,
-					timePaused: 0,
-					pausedAt: 0
-				});
-			} else {
-				this.setCurrentSong({
-					currentSong: null,
-					startedAt: 0,
-					paused: this.stationPaused,
-					timePaused: 0,
-					pausedAt: 0
-				});
+			console.log("SKIP_SONG_FN", this.nextCurrentSong);
+			if (this.nextCurrentSong && this.nextCurrentSong.currentSong) {
+				this.setCurrentSong(this.nextCurrentSong);
 			}
 		},
 		setCurrentSong(data) {
@@ -1082,13 +1045,19 @@ export default {
 
 			this.updateCurrentSong(currentSong || {});
 
-			if (
-				currentSong &&
-				this.nextSong &&
-				currentSong._id === this.nextSong._id
-			) {
-				this.setNextSong(null);
-			}
+			let nextSong = null;
+			if (this.songsList[0])
+				nextSong = this.songsList[0].youtubeId
+					? this.songsList[0]
+					: null;
+			this.updateNextSong(nextSong);
+			this.nextCurrentSong = {
+				currentSong: null,
+				startedAt: 0,
+				paused,
+				timePaused: 0,
+				pausedAt: 0
+			};
 
 			clearTimeout(window.stationNextSongTimeout);
 
@@ -1700,7 +1669,7 @@ export default {
 										? this.songsList[0]
 										: null;
 								}
-								this.setNextSong(nextSong);
+								this.updateNextSong(nextSong);
 							}
 						});