Browse Source

Removed some old properties and added playMode property for station objects in migration3. Also commented out some now unneeded code

Kristian Vos 4 years ago
parent
commit
4e74c3a930

+ 45 - 45
backend/logic/actions/playlists.js

@@ -1339,52 +1339,52 @@ export default {
 					PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId }, this)
 						.then(() => next(null, playlist))
 						.catch(next);
-				},
-
-				(playlist, next) => {
-					stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
-						next(err, playlist, res);
-					});
-				},
-
-				(playlist, stations, next) => {
-					async.each(
-						stations,
-						(station, next) => {
-							async.waterfall(
-								[
-									next => {
-										stationModel.updateOne(
-											{ _id: station._id },
-											{ $set: { privatePlaylist: null } },
-											{ runValidators: true },
-											next
-										);
-									},
-
-									(res, next) => {
-										if (!station.partyMode) {
-											moduleManager.modules.stations
-												.runJob("UPDATE_STATION", { stationId: station._id }, this)
-												.then(station => next(null, station))
-												.catch(next);
-											CacheModule.runJob("PUB", {
-												channel: "privatePlaylist.selected",
-												value: {
-													playlistId: null,
-													stationId: station._id
-												}
-											});
-										} else next();
-									}
-								],
-
-								() => next()
-							);
-						},
-						() => next(null, playlist)
-					);
 				}
+
+				// (playlist, next) => {
+				// 	stationModel.find({ privatePlaylist: playlistId }, (err, res) => {
+				// 		next(err, playlist, res);
+				// 	});
+				// },
+
+				// (playlist, stations, next) => {
+				// 	async.each(
+				// 		stations,
+				// 		(station, next) => {
+				// 			async.waterfall(
+				// 				[
+				// 					next => {
+				// 						stationModel.updateOne(
+				// 							{ _id: station._id },
+				// 							{ $set: { privatePlaylist: null } },
+				// 							{ runValidators: true },
+				// 							next
+				// 						);
+				// 					},
+
+				// 					(res, next) => {
+				// 						if (!station.partyMode) {
+				// 							moduleManager.modules.stations
+				// 								.runJob("UPDATE_STATION", { stationId: station._id }, this)
+				// 								.then(station => next(null, station))
+				// 								.catch(next);
+				// 							CacheModule.runJob("PUB", {
+				// 								channel: "privatePlaylist.selected",
+				// 								value: {
+				// 									playlistId: null,
+				// 									stationId: station._id
+				// 								}
+				// 							});
+				// 						} else next();
+				// 					}
+				// 				],
+
+				// 				() => next()
+				// 			);
+				// 		},
+				// 		() => next(null, playlist)
+				// 	);
+				// }
 			],
 			async (err, playlist) => {
 				if (err) {

+ 32 - 33
backend/logic/actions/stations.js

@@ -109,25 +109,25 @@ CacheModule.runJob("SUB", {
 	}
 });
 
-CacheModule.runJob("SUB", {
-	channel: "privatePlaylist.selected",
-	cb: data => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${data.stationId}`,
-			args: ["event:privatePlaylist.selected", data.playlistId]
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "privatePlaylist.deselected",
-	cb: data => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `station.${data.stationId}`,
-			args: ["event:privatePlaylist.deselected"]
-		});
-	}
-});
+// CacheModule.runJob("SUB", {
+// 	channel: "privatePlaylist.selected",
+// 	cb: data => {
+// 		WSModule.runJob("EMIT_TO_ROOM", {
+// 			room: `station.${data.stationId}`,
+// 			args: ["event:privatePlaylist.selected", data.playlistId]
+// 		});
+// 	}
+// });
+
+// CacheModule.runJob("SUB", {
+// 	channel: "privatePlaylist.deselected",
+// 	cb: data => {
+// 		WSModule.runJob("EMIT_TO_ROOM", {
+// 			room: `station.${data.stationId}`,
+// 			args: ["event:privatePlaylist.deselected"]
+// 		});
+// 	}
+// });
 
 CacheModule.runJob("SUB", {
 	channel: "station.pause",
@@ -753,11 +753,11 @@ export default {
 						locked: station.locked,
 						partyMode: station.partyMode,
 						owner: station.owner,
-						privatePlaylist: station.privatePlaylist,
+						// privatePlaylist: station.privatePlaylist,
 						includedPlaylists: station.includedPlaylists,
 						excludedPlaylists: station.excludedPlaylists,
-						genres: station.genres,
-						blacklistedGenres: station.blacklistedGenres,
+						// genres: station.genres,
+						// blacklistedGenres: station.blacklistedGenres,
 						theme: station.theme
 					};
 
@@ -879,9 +879,9 @@ export default {
 						locked: station.locked,
 						partyMode: station.partyMode,
 						owner: station.owner,
-						privatePlaylist: station.privatePlaylist,
-						genres: station.genres,
-						blacklistedGenres: station.blacklistedGenres,
+						// privatePlaylist: station.privatePlaylist,
+						// genres: station.genres,
+						// blacklistedGenres: station.blacklistedGenres,
 						theme: station.theme
 					};
 
@@ -1399,7 +1399,7 @@ export default {
 
 				(station, next) => {
 					playlistModel.updateOne(
-						{ _id: station.playlist2 },
+						{ _id: station.playlist },
 						{ $set: { displayName: `Station - ${station.displayName}` } },
 						err => {
 							next(err, station);
@@ -2267,8 +2267,8 @@ export default {
 				},
 
 				(station, next) => {
-					if (station.playlist2)
-						PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: station.playlist2 })
+					if (station.playlist)
+						PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: station.playlist })
 							.then(() => {})
 							.catch(next);
 					next(null, station);
@@ -2486,7 +2486,7 @@ export default {
 											type: "station"
 										},
 
-										(err, playlist2) => {
+										(err, playlist) => {
 											if (err) next(err);
 											else {
 												stationModel.create(
@@ -2497,8 +2497,7 @@ export default {
 														description,
 														type,
 														privacy: "private",
-														playlist2: playlist2._id,
-														playlist,
+														playlist: playlist._id,
 														currentSong: StationsModule.defaultSong
 													},
 													(err, station) => {
@@ -2531,7 +2530,7 @@ export default {
 								type: "station"
 							},
 
-							(err, playlist2) => {
+							(err, playlist) => {
 								if (err) next(err);
 								else {
 									stationModel.create(
@@ -2540,7 +2539,7 @@ export default {
 											name,
 											displayName,
 											description,
-											playlist2: playlist2._id,
+											playlist: playlist._id,
 											type,
 											privacy: "private",
 											owner: session.userId,

+ 1 - 1
backend/logic/db/index.js

@@ -13,7 +13,7 @@ const REQUIRED_DOCUMENT_VERSIONS = {
 	queueSong: 1,
 	report: 1,
 	song: 1,
-	station: 2,
+	station: 3,
 	user: 1
 };
 

+ 3 - 6
backend/logic/db/schemas/station.js

@@ -23,10 +23,7 @@ export default {
 	timePaused: { type: Number, default: 0, required: true },
 	pausedAt: { type: Number, default: 0, required: true },
 	startedAt: { type: Number, default: 0, required: true },
-	playlist: { type: Array },
-	playlist2: { type: mongoose.Schema.Types.ObjectId, required: true },
-	genres: [{ type: String }],
-	blacklistedGenres: [{ type: String }],
+	playlist: { type: mongoose.Schema.Types.ObjectId, required: true },
 	privacy: { type: String, enum: ["public", "unlisted", "private"], default: "private" },
 	locked: { type: Boolean, default: false },
 	queue: [
@@ -44,10 +41,10 @@ export default {
 		}
 	],
 	owner: { type: String },
-	privatePlaylist: { type: mongoose.Schema.Types.ObjectId },
 	partyMode: { type: Boolean },
+	playMode: { type: String, enum: ["random", "sequential"] },
 	theme: { type: String, enum: ["blue", "purple", "teal", "orange"], default: "blue" },
 	includedPlaylists: [{ type: String }],
 	excludedPlaylists: [{ type: String }],
-	documentVersion: { type: Number, default: 2, required: true }
+	documentVersion: { type: Number, default: 3, required: true }
 };

+ 76 - 0
backend/logic/migration/migrations/migration3.js

@@ -0,0 +1,76 @@
+import async from "async";
+
+/**
+ * Migration 3
+ *
+ * Clean up station object from playlist2 property (replacing old playlist property with playlist2 property), adding a playMode property and removing genres/blacklisted genres
+ *
+ * @param {object} MigrationModule - the MigrationModule
+ * @returns {Promise} - returns promise
+ */
+export default async function migrate(MigrationModule) {
+	const stationModel = await MigrationModule.runJob("GET_MODEL", { modelName: "station" }, this);
+
+	return new Promise((resolve, reject) => {
+		async.waterfall(
+			[
+				next => {
+					this.log("INFO", `Migration 3. Finding stations with document version 2.`);
+					stationModel.find({ documentVersion: 2 }, (err, stations) => {
+						this.log("INFO", `Migration 3. Found ${stations.length} stations with document version 2.`);
+
+						next(
+							null,
+							stations.map(station => station._doc)
+						);
+					});
+				},
+
+				(stations, next) => {
+					async.eachLimit(
+						stations,
+						1,
+						(station, next) => {
+							this.log("INFO", `Migration 3. Updating station ${station._id}.`);
+							stationModel.updateOne(
+								{ _id: station._id },
+								{
+									$set: {
+										playlist: station.playlist2,
+										playMode: "random",
+										documentVersion: 3
+									},
+									$unset: {
+										genres: "",
+										blacklistedGenres: "",
+										playlist2: "",
+										privatePlaylist: ""
+									}
+								},
+								(err, res) => {
+									if (err) next(err);
+									else {
+										this.log(
+											"INFO",
+											`Migration 3. Updating station ${station._id} done. Matched: ${res.n}, modified: ${res.nModified}, ok: ${res.ok}.`
+										);
+										next();
+									}
+								}
+							);
+							// next();
+						},
+						next
+					);
+				}
+			],
+			(err, response) => {
+				if (err) {
+					reject(new Error(err));
+				} else {
+					resolve(response);
+				}
+			}
+		);
+	});
+}

+ 3 - 3
backend/logic/playlists.js

@@ -519,7 +519,7 @@ class _PlaylistsModule extends CoreClass {
 						(playlist, next) => {
 							StationsModule.runJob("GET_STATION", { stationId: playlist.createdFor }, this)
 								.then(station => {
-									if (station.playlist2 !== playlist._id.toString()) {
+									if (station.playlist !== playlist._id.toString()) {
 										orphanedPlaylists.push(playlist);
 									}
 									next();
@@ -601,7 +601,7 @@ class _PlaylistsModule extends CoreClass {
 					},
 
 					(station, next) => {
-						PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist2 }, this)
+						PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist }, this)
 							.then(playlist => {
 								originalPlaylist = playlist;
 								next(null, station);
@@ -671,7 +671,7 @@ class _PlaylistsModule extends CoreClass {
 
 					(station, includedSongs, next) => {
 						PlaylistsModule.playlistModel.updateOne(
-							{ _id: station.playlist2 },
+							{ _id: station.playlist },
 							{ $set: { songs: includedSongs } },
 							err => {
 								next(err, includedSongs);

+ 2 - 2
backend/logic/stations.js

@@ -1250,7 +1250,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					(station, next) => {
-						if (station.playlist2 === payload.playlistId) next("You cannot include the station playlist");
+						if (station.playlist === payload.playlistId) next("You cannot include the station playlist");
 						else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)
 							next("This playlist is already included");
 						else if (station.excludedPlaylists.indexOf(payload.playlistId) !== -1)
@@ -1408,7 +1408,7 @@ class _StationsModule extends CoreClass {
 					},
 
 					(station, next) => {
-						if (station.playlist2 === payload.playlistId) next("You cannot exclude the station playlist");
+						if (station.playlist === payload.playlistId) next("You cannot exclude the station playlist");
 						else if (station.excludedPlaylists.indexOf(payload.playlistId) !== -1)
 							next("This playlist is already excluded");
 						else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)