Browse Source

Added button to delete orphaned genre playlists

Kristian Vos 4 years ago
parent
commit
997f5e20a0

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

@@ -1541,5 +1541,40 @@ export default {
 				return cb({ status: "success", message: "Success" });
 			}
 		);
+	}),
+
+	/**
+	 * Deletes all orphaned genre playlists
+	 *
+	 * @param {object} session - the session object automatically added by socket.io
+	 * @param {Function} cb - gets called with the result
+	 */
+	deleteOrphanedGenrePlaylists: isAdminRequired(async function index(session, cb) {
+		async.waterfall(
+			[
+				next => {
+					PlaylistsModule.runJob("DELETE_ORPHANED_GENRE_PLAYLISTS", {}, this)
+						.then(() => next())
+						.catch(next);
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
+						`Deleting orphaned genre playlists failed. "${err}"`
+					);
+					return cb({ status: "failure", message: err });
+				}
+				this.log(
+					"SUCCESS",
+					"PLAYLISTS_DELETE_ORPHANED_GENRE_PLAYLISTS",
+					"Deleting orphaned genre playlists successfull."
+				);
+				return cb({ status: "success", message: "Success" });
+			}
+		);
 	})
 };

+ 76 - 0
backend/logic/playlists.js

@@ -502,6 +502,82 @@ class _PlaylistsModule extends CoreClass {
 		});
 	}
 
+	/**
+	 * Gets orphaned genre playlists
+	 *
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	GET_ORPHANED_GENRE_PLAYLISTS() {
+		return new Promise((resolve, reject) => {
+			PlaylistsModule.playlistModel.find({ type: "genre" }, { songs: false }, (err, playlists) => {
+				if (err) reject(new Error(err));
+				else {
+					const orphanedPlaylists = [];
+					async.eachLimit(
+						playlists,
+						1,
+						(playlist, next) => {
+							SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: playlist.createdFor }, this)
+								.then(response => {
+									if (response.songs.length === 0) {
+										StationsModule.runJob(
+											"GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST",
+											{ playlistId: playlist._id },
+											this
+										)
+											.then(response => {
+												if (response.stationIds.length === 0) orphanedPlaylists.push(playlist);
+												next();
+											})
+											.catch(next);
+									} else next();
+								})
+								.catch(next);
+						},
+						err => {
+							if (err) reject(new Error(err));
+							else resolve({ playlists: orphanedPlaylists });
+						}
+					);
+				}
+			});
+		});
+	}
+
+	/**
+	 * Deletes all orphaned genre playlists
+	 *
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	DELETE_ORPHANED_GENRE_PLAYLISTS() {
+		return new Promise((resolve, reject) => {
+			PlaylistsModule.runJob("GET_ORPHANED_GENRE_PLAYLISTS", {}, this)
+				.then(response => {
+					async.eachLimit(
+						response.playlists,
+						1,
+						(playlist, next) => {
+							PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
+								.then(() => {
+									this.log("INFO", "Deleting orphaned genre playlist");
+									next();
+								})
+								.catch(err => {
+									next(err);
+								});
+						},
+						err => {
+							if (err) reject(new Error(err));
+							else resolve({});
+						}
+					);
+				})
+				.catch(err => {
+					reject(new Error(err));
+				});
+		});
+	}
+
 	/**
 	 * Gets a orphaned station playlists
 	 *

+ 3 - 1
backend/logic/stations.js

@@ -1579,7 +1579,9 @@ class _StationsModule extends CoreClass {
 				this
 			).then(stationModel => {
 				stationModel.find(
-					{ $or: [{ includedPlaylists: payload.playlistId }, { excludedPlaylists: payload.playlistId }] },
+					{
+						$or: [{ includedPlaylists: payload.playlistId }, { excludedPlaylists: payload.playlistId }]
+					},
 					(err, stations) => {
 						if (err) reject(err);
 						else resolve({ stationIds: stations.map(station => station._id) });

+ 24 - 0
frontend/src/pages/Admin/tabs/Playlists.vue

@@ -8,6 +8,12 @@
 			>
 				Delete orphaned station playlists
 			</button>
+			<button
+				class="button is-primary"
+				@click="deleteOrphanedGenrePlaylists()"
+			>
+				Delete orphaned genre playlists
+			</button>
 			<br />
 			<br />
 			<table class="table is-striped">
@@ -157,6 +163,24 @@ export default {
 				}
 			);
 		},
+		deleteOrphanedGenrePlaylists() {
+			this.socket.dispatch(
+				"playlists.deleteOrphanedGenrePlaylists",
+				res => {
+					if (res.status === "success") {
+						new Toast({
+							content: `${res.message}`,
+							timeout: 4000
+						});
+					} else {
+						new Toast({
+							content: `Error: ${res.message}`,
+							timeout: 8000
+						});
+					}
+				}
+			);
+		},
 		...mapActions("modalVisibility", ["openModal"]),
 		...mapActions("user/playlists", ["editPlaylist"])
 	}