Преглед на файлове

Made more progress on the station queue

Kristian Vos преди 4 години
родител
ревизия
ac0b3bec1f
променени са 4 файла, в които са добавени 107 реда и са изтрити 122 реда
  1. 2 2
      backend/logic/actions/stations.js
  2. 1 1
      backend/logic/db/schemas/station.js
  3. 83 77
      backend/logic/stations.js
  4. 21 42
      frontend/src/pages/Station/index.vue

+ 2 - 2
backend/logic/actions/stations.js

@@ -2514,7 +2514,6 @@ export default {
 					if (type === "community") {
 						if (blacklist.indexOf(name) !== -1)
 							return next("That name is blacklisted. Please use a different name.");
-						console.log(12121212, stationId);
 						return playlistModel.create(
 							{
 								isUserModifiable: false,
@@ -2752,6 +2751,7 @@ export default {
 				(song, station, next) => {
 					song.requestedBy = session.userId;
 					song.requestedAt = Date.now();
+					song._id = null;
 
 					let totalDuration = 0;
 					station.queue.forEach(song => {
@@ -2958,7 +2958,7 @@ export default {
 				},
 
 				(station, next) => {
-					if (station.type === "official") next(null, station.playlist);
+					if (station.type === "official") next(null, station.queue);
 					else next(null, station.queue);
 				}
 			],

+ 1 - 1
backend/logic/db/schemas/station.js

@@ -39,7 +39,7 @@ export default {
 			thumbnail: { type: String },
 			likes: { type: Number, default: -1 },
 			dislikes: { type: Number, default: -1 },
-			requestedBy: { type: String, required: true },
+			requestedBy: { type: String },
 			requestedAt: { type: Date }
 		}
 	],

+ 83 - 77
backend/logic/stations.js

@@ -465,10 +465,9 @@ class _StationsModule extends CoreClass {
 	 * @param {string} payload.stationId - the id of the station
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
-	FILL_UP_OFFICIAL_STATION_PLAYLIST_QUEUE(payload) {
+	FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST(payload) {
 		return new Promise((resolve, reject) => {
 			const { stationId } = payload;
-
 			async.waterfall(
 				[
 					next => {
@@ -496,10 +495,11 @@ class _StationsModule extends CoreClass {
 					},
 
 					(playlistSongs, station, next) => {
-						const songsStillNeeded = 50 - station.playlist.length;
-						const currentSongs = station.playlist;
-						const currentSongIds = station.playlist.map(song => song.songId);
+						const songsStillNeeded = 50 - station.queue.length;
+						const currentSongs = station.queue;
+						const currentSongIds = station.queue.map(song => song.songId);
 						const songsToAdd = [];
+
 						playlistSongs
 							.map(song => song._doc)
 							.every(song => {
@@ -508,8 +508,9 @@ class _StationsModule extends CoreClass {
 									currentSongIds.indexOf(song.songId) === -1
 								) {
 									songsToAdd.push(song);
-									return false;
+									return true;
 								}
+								if (songsToAdd.length >= songsStillNeeded) return false;
 								return true;
 							});
 						next(null, [...currentSongs, ...songsToAdd]);
@@ -518,20 +519,22 @@ class _StationsModule extends CoreClass {
 					(newPlaylist, next) => {
 						StationsModule.stationModel.updateOne(
 							{ _id: stationId },
-							{ $set: { playlist: newPlaylist } },
+							{ $set: { queue: newPlaylist } },
 							{ runValidators: true },
-							() => {
-								StationsModule.runJob(
-									"UPDATE_STATION",
-									{
-										stationId
-									},
-									this
-								)
-									.then(() => {
-										next(null);
-									})
-									.catch(next);
+							err => {
+								if (err) next(err);
+								else
+									StationsModule.runJob(
+										"UPDATE_STATION",
+										{
+											stationId
+										},
+										this
+									)
+										.then(() => {
+											next(null);
+										})
+										.catch(next);
 							}
 						);
 					}
@@ -566,9 +569,9 @@ class _StationsModule extends CoreClass {
 					},
 
 					(station, next) => {
-						if (station.playlist.length === 0) next("No songs available.");
+						if (station.queue.length === 0) next("No songs available.");
 						else {
-							next(null, station.playlist[0]);
+							next(null, station.queue[0]);
 						}
 					},
 
@@ -606,13 +609,13 @@ class _StationsModule extends CoreClass {
 	}
 
 	/**
-	 * Removes first official playlist queue song
+	 * Removes first station queue song
 	 *
 	 * @param {object} payload - object that contains the payload
 	 * @param {string} payload.stationId - the id of the station
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
-	REMOVE_FIRST_OFFICIAL_PLAYLIST_QUEUE_SONG(payload) {
+	REMOVE_FIRST_QUEUE_SONG(payload) {
 		return new Promise((resolve, reject) => {
 			const { stationId } = payload;
 
@@ -621,7 +624,7 @@ class _StationsModule extends CoreClass {
 					next => {
 						StationsModule.stationModel.updateOne(
 							{ _id: stationId },
-							{ $pop: { playlist: -1 } },
+							{ $pop: { queue: -1 } },
 							{ runValidators: true },
 							err => {
 								if (err) next(err);
@@ -701,49 +704,58 @@ class _StationsModule extends CoreClass {
 							// Community station with party mode enabled and songs in the queue
 							if (station.paused) return next(null, null, -19, station);
 
-							return StationsModule.stationModel.updateOne(
-								{ _id: payload.stationId },
-								{
-									$pull: {
-										queue: {
-											_id: station.queue[0]._id
-										}
-									}
-								},
-								err => {
-									if (err) return next(err);
-									return next(null, station.queue[0], -12, station);
-								}
-							);
+							StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
+								.then(response => {
+									StationsModule.runJob(
+										"REMOVE_FIRST_QUEUE_SONG",
+										{ stationId: station._id },
+										this
+									).then(() => {
+										next(null, response.song, 0, station);
+									});
+								})
+								.catch(err => {
+									if (err === "No songs available.") next(null, null, 0, station);
+									else next(err);
+								});
+
+							// return StationsModule.stationModel.updateOne(
+							// 	{ _id: payload.stationId },
+							// 	{
+							// 		$pull: {
+							// 			queue: {
+							// 				_id: station.queue[0]._id
+							// 			}
+							// 		}
+							// 	},
+							// 	err => {
+							// 		if (err) return next(err);
+							// 		return next(null, station.queue[0], -12, station);
+							// 	}
+							// );
 						}
 
 						if (station.type === "community" && !station.partyMode) {
 							StationsModule.runJob(
-								"REMOVE_FIRST_OFFICIAL_PLAYLIST_QUEUE_SONG",
+								"FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
 								{ stationId: station._id },
 								this
 							)
 								.then(() => {
-									StationsModule.runJob(
-										"FILL_UP_OFFICIAL_STATION_PLAYLIST_QUEUE",
-										{ stationId: station._id },
-										this
-									)
-										.then(() => {
+									StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
+										.then(response => {
 											StationsModule.runJob(
-												"GET_NEXT_STATION_SONG",
+												"REMOVE_FIRST_QUEUE_SONG",
 												{ stationId: station._id },
 												this
-											)
-												.then(response => {
-													next(null, response.song, 0, station);
-												})
-												.catch(err => {
-													if (err === "No songs available.") next(null, null, 0, station);
-													else next(err);
-												});
+											).then(() => {
+												next(null, response.song, 0, station);
+											});
 										})
-										.catch(next);
+										.catch(err => {
+											if (err === "No songs available.") next(null, null, 0, station);
+											else next(err);
+										});
 								})
 								.catch(next);
 						}
@@ -808,31 +820,27 @@ class _StationsModule extends CoreClass {
 
 						if (station.type === "official") {
 							StationsModule.runJob(
-								"REMOVE_FIRST_OFFICIAL_PLAYLIST_QUEUE_SONG",
+								"FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
 								{ stationId: station._id },
 								this
 							)
 								.then(() => {
-									StationsModule.runJob(
-										"FILL_UP_OFFICIAL_STATION_PLAYLIST_QUEUE",
-										{ stationId: station._id },
-										this
-									)
-										.then(() => {
+									StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
+										.then(response => {
 											StationsModule.runJob(
-												"GET_NEXT_STATION_SONG",
+												"REMOVE_FIRST_QUEUE_SONG",
 												{ stationId: station._id },
 												this
 											)
-												.then(response => {
+												.then(() => {
 													next(null, response.song, 0, station);
 												})
-												.catch(err => {
-													if (err === "No songs available.") next(null, null, 0, station);
-													else next(err);
-												});
+												.catch(next);
 										})
-										.catch(next);
+										.catch(err => {
+											if (err === "No songs available.") next(null, null, 0, station);
+											else next(err);
+										});
 								})
 								.catch(next);
 						}
@@ -884,13 +892,12 @@ class _StationsModule extends CoreClass {
 								this
 							)
 								.then(station => {
-									if (station.type === "community" && station.partyMode === true)
-										CacheModule.runJob("PUB", {
-											channel: "station.queueUpdate",
-											value: payload.stationId
-										})
-											.then()
-											.catch();
+									CacheModule.runJob("PUB", {
+										channel: "station.queueUpdate",
+										value: payload.stationId
+									})
+										.then()
+										.catch();
 									next(null, station);
 								})
 								.catch(next);
@@ -899,7 +906,6 @@ class _StationsModule extends CoreClass {
 				],
 				async (err, station) => {
 					if (err) {
-						console.log(123, err);
 						err = await UtilsModule.runJob(
 							"GET_ERROR",
 							{

+ 21 - 42
frontend/src/pages/Station/index.vue

@@ -693,17 +693,17 @@ export default {
 				this.addFirstPrivatePlaylistSongToQueue();
 			}
 
-			if (this.station.type === "official") {
-				this.socket.dispatch(
-					"stations.getQueue",
-					this.station._id,
-					res => {
-						if (res.status === "success") {
-							this.updateSongsList(res.queue);
-						}
-					}
-				);
-			}
+			// if (this.station.type === "official") {
+			// 	this.socket.dispatch(
+			// 		"stations.getQueue",
+			// 		this.station._id,
+			// 		res => {
+			// 			if (res.status === "success") {
+			// 				this.updateSongsList(res.queue);
+			// 			}
+			// 		}
+			// 	);
+			// }
 
 			if (
 				!isInQueue &&
@@ -779,7 +779,7 @@ export default {
 		});
 
 		this.socket.on("event:queue.update", queue => {
-			if (this.station.type === "community") this.updateSongsList(queue);
+			this.updateSongsList(queue);
 		});
 
 		this.socket.on("event:song.voteSkipSong", () => {
@@ -832,10 +832,10 @@ export default {
 			this.station.description = res.description;
 		});
 
-		this.socket.on("event:newOfficialPlaylist", playlist => {
-			if (this.station.type === "official")
-				this.updateSongsList(playlist);
-		});
+		// this.socket.on("event:newOfficialPlaylist", playlist => {
+		// 	if (this.station.type === "official")
+		// 		this.updateSongsList(playlist);
+		// });
 
 		this.socket.on("event:users.updated", users => this.updateUsers(users));
 
@@ -1556,32 +1556,11 @@ export default {
 							this.updateNoSong(true);
 						}
 
-						if (type === "community" && partyMode === true) {
-							this.socket.dispatch(
-								"stations.getQueue",
-								_id,
-								res => {
-									if (res.status === "success") {
-										this.updateSongsList(res.queue);
-									}
-								}
-							);
-						}
-
-						if (
-							(type === "community" && partyMode === true) ||
-							type === "official"
-						) {
-							this.socket.dispatch(
-								"stations.getQueue",
-								_id,
-								res => {
-									if (res.status === "success") {
-										this.updateSongsList(res.queue);
-									}
-								}
-							);
-						}
+						this.socket.dispatch("stations.getQueue", _id, res => {
+							if (res.status === "success") {
+								this.updateSongsList(res.queue);
+							}
+						});
 
 						if (this.isOwnerOrAdmin()) {
 							keyboardShortcuts.registerShortcut(