2 Commits f13fa2575f ... 753959c685

Author SHA1 Message Date
  Owen Diffey 753959c685 refactor: Set station currentSong default to null in schema 11 months ago
  Kristian Vos ff6b95abd1 fix: stations are created with currentSong as an empty object instead of null 11 months ago
3 changed files with 31 additions and 13 deletions
  1. 15 0
      backend/logic/actions/stations.js
  2. 15 12
      backend/logic/db/schemas/station.js
  3. 1 1
      backend/logic/stations.js

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

@@ -1860,6 +1860,21 @@ export default {
 							next
 						);
 					}
+				},
+
+				// This extra step is needed because Mongoose decides to create an object with empty arrays for currentSong for some reason
+				(station, next) => {
+					stationModel.updateOne(
+						{ _id: station._id },
+						{
+							$set: {
+								currentSong: null
+							}
+						},
+						err => {
+							next(err, station);
+						}
+					);
 				}
 			],
 			async (err, station) => {

+ 15 - 12
backend/logic/db/schemas/station.js

@@ -7,18 +7,21 @@ export default {
 	description: { type: String, minlength: 2, maxlength: 128, trim: true, required: true },
 	paused: { type: Boolean, default: false, required: true },
 	currentSong: {
-		_id: { type: mongoose.Schema.Types.ObjectId },
-		mediaSource: { type: String },
-		title: { type: String },
-		artists: [{ type: String }],
-		duration: { type: Number },
-		skipDuration: { type: Number },
-		thumbnail: { type: String },
-		skipVotes: [{ type: String }],
-		requestedBy: { type: String },
-		requestedAt: { type: Date },
-		requestedType: { type: String, enum: ["manual", "autorequest", "autofill"] },
-		verified: { type: Boolean }
+		type: {
+			_id: { type: mongoose.Schema.Types.ObjectId },
+			mediaSource: { type: String },
+			title: { type: String },
+			artists: [{ type: String }],
+			duration: { type: Number },
+			skipDuration: { type: Number },
+			thumbnail: { type: String },
+			skipVotes: [{ type: String }],
+			requestedBy: { type: String },
+			requestedAt: { type: Date },
+			requestedType: { type: String, enum: ["manual", "autorequest", "autofill"] },
+			verified: { type: Boolean }
+		},
+		default: null
 	},
 	currentSongIndex: { type: Number, default: 0, required: true },
 	timePaused: { type: Number, default: 0, required: true },

+ 1 - 1
backend/logic/stations.js

@@ -309,7 +309,7 @@ class _StationsModule extends CoreClass {
 					(station, next) => {
 						// A current song is invalid if it isn't allowed to be played. Spotify songs can never be played, and SoundCloud songs can't be played if SoundCloud isn't enabled
 						let currentSongIsInvalid = false;
-						if (station.currentSong) {
+						if (station.currentSong && station.currentSong.mediaSource) {
 							if (station.currentSong.mediaSource.startsWith("spotify:")) currentSongIsInvalid = true;
 							if (
 								station.currentSong.mediaSource.startsWith("soundcloud:") &&