Browse Source

Re-added remove song functionality

Owen Diffey 3 years ago
parent
commit
c32f68c516

+ 68 - 61
backend/logic/actions/songs.js

@@ -545,73 +545,80 @@ export default {
 		);
 	}),
 
-	// /**
-	//  * Removes a song
-	//  *
-	//  * @param session
-	//  * @param songId - the song id
-	//  * @param cb
-	//  */
-	// remove: isAdminRequired(async function remove(session, songId, cb) {
-	// 	const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
-	// 	let song = null;
-	// 	async.waterfall(
-	// 		[
-	// 			next => {
-	// 				songModel.findOne({ _id: songId }, next);
-	// 			},
+	/**
+	 * Removes a song
+	 *
+	 * @param session
+	 * @param songId - the song id
+	 * @param cb
+	 */
+	remove: isAdminRequired(async function remove(session, songId, cb) {
+		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
+		let song = null;
+		async.waterfall(
+			[
+				next => {
+					songModel.findOne({ _id: songId }, next);
+				},
 
-	// 			(_song, next) => {
-	// 				song = _song;
-	// 				songModel.deleteOne({ _id: songId }, next);
-	// 			},
+				(_song, next) => {
+					song = _song;
+					songModel.deleteOne({ _id: songId }, next);
+				},
 
-	// 			(res, next) => {
-	// 				// TODO Check if res gets returned from above
-	// 				CacheModule.runJob("HDEL", { table: "songs", key: songId }, this)
-	// 					.then(() => {
-	// 						next();
-	// 					})
-	// 					.catch(next)
-	// 					.finally(() => {
-	// 						song.genres.forEach(genre => {
-	// 							PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre })
-	// 								.then(() => {})
-	// 								.catch(() => {});
-	// 						});
-	// 					});
-	// 			}
-	// 		],
-	// 		async err => {
-	// 			if (err) {
-	// 				err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+				(res, next) => {
+					// TODO Check if res gets returned from above
+					CacheModule.runJob("HDEL", { table: "songs", key: songId }, this)
+						.then(() => {
+							next();
+						})
+						.catch(next)
+						.finally(() => {
+							song.genres.forEach(genre => {
+								PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre })
+									.then(() => {})
+									.catch(() => {});
+							});
+						});
+				}
+			],
+			async err => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 
-	// 				this.log("ERROR", "SONGS_REMOVE", `Failed to remove song "${songId}". "${err}"`);
+					this.log("ERROR", "SONGS_REMOVE", `Failed to remove song "${songId}". "${err}"`);
 
-	// 				return cb({ status: "error", message: err });
-	// 			}
+					return cb({ status: "error", message: err });
+				}
 
-	// 			this.log("SUCCESS", "SONGS_REMOVE", `Successfully remove song "${songId}".`);
-
-	// 			if (song.verified) {
-	// 				CacheModule.runJob("PUB", {
-	// 					channel: "song.removedVerifiedSong",
-	// 					value: songId
-	// 				});
-	// 			} else {
-	// 				CacheModule.runJob("PUB", {
-	// 					channel: "song.removedUnverifiedSong",
-	// 					value: songId
-	// 				});
-	// 			}
+				this.log("SUCCESS", "SONGS_REMOVE", `Successfully removed song "${songId}".`);
 
-	// 			return cb({
-	// 				status: "success",
-	// 				message: "Song has been successfully removed"
-	// 			});
-	// 		}
-	// 	);
-	// }),
+				if (song.status === "verified") {
+					CacheModule.runJob("PUB", {
+						channel: "song.removedVerifiedSong",
+						value: songId
+					});
+				}
+				if (song.status === "unverified") {
+					CacheModule.runJob("PUB", {
+						channel: "song.removedUnverifiedSong",
+						value: songId
+					});
+				}
+				if (song.status === "hidden") {
+					CacheModule.runJob("PUB", {
+						channel: "song.removedHiddenSong",
+						value: songId
+					});
+				}
+
+				return cb({
+					status: "success",
+					message: "Song has been successfully removed"
+				});
+			}
+		);
+	}),
 
 	/**
 	 * Searches through official songs

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

@@ -1013,7 +1013,6 @@ export default {
 		);
 	},
 
-
 	/**
 	 * Gets a station by id
 	 *

+ 29 - 0
frontend/src/components/modals/EditSong.vue

@@ -523,6 +523,15 @@
 					>
 						<i class="material-icons">visibility</i>
 					</button>
+					<confirm placement="left" @confirm="remove(song._id)">
+						<button
+							class="button is-danger"
+							content="Remove Song"
+							v-tippy
+						>
+							<i class="material-icons">delete</i>
+						</button>
+					</confirm>
 				</div>
 			</div>
 		</modal>
@@ -811,6 +820,21 @@ export default {
 			this.song.status = res.data.song.status;
 		});
 
+		this.socket.on("event:admin.hiddenSong.removed", () => {
+			new Toast("The song you were editing was removed");
+			this.closeModal("editSong");
+		});
+
+		this.socket.on("event:admin.unverifiedSong.removed", () => {
+			new Toast("The song you were editing was removed");
+			this.closeModal("editSong");
+		});
+
+		this.socket.on("event:admin.verifiedSong.removed", () => {
+			new Toast("The song you were editing was removed");
+			this.closeModal("editSong");
+		});
+
 		keyboardShortcuts.registerShortcut("editSong.pauseResumeVideo", {
 			keyCode: 101,
 			preventDefault: true,
@@ -1495,6 +1519,11 @@ export default {
 				new Toast(res.message);
 			});
 		},
+		remove(id) {
+			this.socket.dispatch("songs.remove", id, res => {
+				new Toast(res.message);
+			});
+		},
 		...mapActions("modals/editSong", [
 			"stopVideo",
 			"loadVideoById",