浏览代码

Merge branch 'polishing' of github.com:Musare/MusareNode into polishing

Jonathan 4 年之前
父节点
当前提交
a1295e22ae

+ 1 - 0
backend/logic/actions/playlists.js

@@ -206,6 +206,7 @@ export default {
 
 				(playlist, next) => {
 					if (!playlist || playlist.createdBy !== session.userId) return next("Playlist not found.");
+					playlist.songs.sort((a, b) => a.position - b.position);
 					return next(null, playlist.songs[0]);
 				}
 			],

+ 9 - 3
backend/logic/actions/stations.js

@@ -1978,13 +1978,19 @@ export default {
 						return next(`The party mode was already ${newPartyMode ? "enabled." : "disabled."}`);
 					return stationModel.updateOne(
 						{ _id: stationId },
-						{ $set: { partyMode: newPartyMode } },
+						{ $set: { partyMode: newPartyMode, queue: [] } },
 						{ runValidators: true },
 						next
 					);
 				},
 
 				(res, next) => {
+					CacheModule.runJob("PUB", {
+						channel: "station.queueUpdate",
+						value: stationId
+					})
+						.then()
+						.catch();
 					StationsModule.runJob("UPDATE_STATION", { stationId }, this)
 						.then(station => {
 							next(null, station);
@@ -2514,7 +2520,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 +2757,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 +2964,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 }
 		}
 	],

+ 97 - 78
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,30 +508,37 @@ 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]);
+						const newPlaylist = [...currentSongs, ...songsToAdd].map(song => {
+							if (!song._id) song._id = null;
+							return song;
+						});
+						next(null, newPlaylist);
 					},
 
 					(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 +573,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]);
 						}
 					},
 
@@ -597,7 +604,15 @@ class _StationsModule extends CoreClass {
 
 									return next(null, song);
 								})
-								.catch(next);
+								.catch(err => {
+									if (err.message === "Song not found.") {
+										this.log(
+											"ERROR",
+											`In GET_NEXT_STATION_SONG, attempted to get song "${song._id}", but got the error "Song not found.". Ignoring it for now, but this is not normal.`
+										);
+										next(null, song);
+									} else next(err);
+								});
 					}
 				],
 				(err, song) => {
@@ -610,13 +625,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;
 
@@ -625,7 +640,7 @@ class _StationsModule extends CoreClass {
 					next => {
 						StationsModule.stationModel.updateOne(
 							{ _id: stationId },
-							{ $pop: { playlist: -1 } },
+							{ $pop: { queue: -1 } },
 							{ runValidators: true },
 							err => {
 								if (err) next(err);
@@ -705,49 +720,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);
 						}
@@ -812,31 +836,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);
 						}
@@ -883,13 +903,12 @@ class _StationsModule extends CoreClass {
 
 							return StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, 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);

+ 5 - 0
frontend/src/pages/Station/components/Sidebar/MyPlaylists.vue

@@ -174,6 +174,7 @@ export default {
 							content: res.message,
 							timeout: 8000
 						});
+					this.station.includedPlaylists.push(id);
 					return new Toast({ content: res.message, timeout: 4000 });
 				}
 			);
@@ -189,6 +190,10 @@ export default {
 							content: res.message,
 							timeout: 8000
 						});
+					this.station.includedPlaylists.splice(
+						this.station.includedPlaylists.indexOf(id),
+						1
+					);
 					return new Toast({ content: res.message, timeout: 4000 });
 				}
 			);

+ 15 - 10
frontend/src/pages/Station/components/Sidebar/Queue/index.vue

@@ -79,7 +79,7 @@
 </template>
 
 <script>
-import { mapActions, mapState } from "vuex";
+import { mapActions, mapState, mapGetters } from "vuex";
 import Toast from "toasters";
 
 import QueueItem from "./QueueItem.vue";
@@ -92,14 +92,19 @@ export default {
 			actionableButtonVisible: false
 		};
 	},
-	computed: mapState({
-		loggedIn: state => state.user.auth.loggedIn,
-		userId: state => state.user.auth.userId,
-		userRole: state => state.user.auth.role,
-		station: state => state.station.station,
-		songsList: state => state.station.songsList,
-		noSong: state => state.station.noSong
-	}),
+	computed: {
+		...mapState({
+			loggedIn: state => state.user.auth.loggedIn,
+			userId: state => state.user.auth.userId,
+			userRole: state => state.user.auth.role,
+			station: state => state.station.station,
+			songsList: state => state.station.songsList,
+			noSong: state => state.station.noSong
+		}),
+		...mapGetters({
+			socket: "websockets/getSocket"
+		})
+	},
 	updated() {
 		// check if actionable button is visible, if not: set max-height of queue items to 100%
 		if (
@@ -118,7 +123,7 @@ export default {
 			return this.loggedIn && this.userRole === "admin";
 		},
 		removeFromQueue(songId) {
-			window.socket.dispatch(
+			this.socket.dispatch(
 				"stations.removeFromQueue",
 				this.station._id,
 				songId,

+ 31 - 52
frontend/src/pages/Station/index.vue

@@ -688,27 +688,27 @@ 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 (
-				!isInQueue &&
-				this.privatePlaylistQueueSelected &&
-				(this.automaticallyRequestedSongId !==
-					this.currentSong.songId ||
-					!this.currentSong.songId)
-			) {
-				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 (
+			// 	!isInQueue &&
+			// 	this.privatePlaylistQueueSelected &&
+			// 	(this.automaticallyRequestedSongId !==
+			// 		this.currentSong.songId ||
+			// 		!this.currentSong.songId)
+			// ) {
+			// 	this.addFirstPrivatePlaylistSongToQueue();
+			// }
 		});
 
 		this.socket.on("event:stations.pause", data => {
@@ -774,7 +774,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", () => {
@@ -827,10 +827,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));
 
@@ -1546,32 +1546,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(