Browse Source

refactor: Unified song update socket events

Owen Diffey 3 years ago
parent
commit
967e4d25ed

+ 26 - 156
backend/logic/actions/songs.js

@@ -14,121 +14,22 @@ const YouTubeModule = moduleManager.modules.youtube;
 const PlaylistsModule = moduleManager.modules.playlists;
 
 CacheModule.runJob("SUB", {
-	channel: "song.newUnverifiedSong",
-	cb: async songId => {
+	channel: "song.updated",
+	cb: async data => {
 		const songModel = await DBModule.runJob("GET_MODEL", {
 			modelName: "song"
 		});
 
-		songModel.findOne({ _id: songId }, (err, song) =>
+		songModel.findOne({ _id: data.songId }, (err, song) => {
 			WSModule.runJob("EMIT_TO_ROOMS", {
-				rooms: ["admin.unverifiedSongs", `edit-song.${songId}`],
-				args: ["event:admin.unverifiedSong.created", { data: { song } }]
-			})
-		);
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.removedUnverifiedSong",
-	cb: songId => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: "admin.unverifiedSongs",
-			args: ["event:admin.unverifiedSong.deleted", { data: { songId } }]
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.updatedUnverifiedSong",
-	cb: async songId => {
-		const songModel = await DBModule.runJob("GET_MODEL", {
-			modelName: "song"
-		});
-
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.unverifiedSongs",
-				args: ["event:admin.unverifiedSong.updated", { data: { song } }]
-			});
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.newVerifiedSong",
-	cb: async songId => {
-		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-
-		songModel.findOne({ _id: songId }, (err, song) =>
-			WSModule.runJob("EMIT_TO_ROOMS", {
-				rooms: ["admin.songs", `edit-song.${songId}`],
-				args: ["event:admin.verifiedSong.created", { data: { song } }]
-			})
-		);
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.removedVerifiedSong",
-	cb: songId => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: "admin.songs",
-			args: ["event:admin.verifiedSong.deleted", { data: { songId } }]
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.updatedVerifiedSong",
-	cb: async songId => {
-		const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.songs",
-				args: ["event:admin.verifiedSong.updated", { data: { song } }]
-			});
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.newHiddenSong",
-	cb: async songId => {
-		const songModel = await DBModule.runJob("GET_MODEL", {
-			modelName: "song"
-		});
-
-		songModel.findOne({ _id: songId }, (err, song) =>
-			WSModule.runJob("EMIT_TO_ROOMS", {
-				rooms: ["admin.hiddenSongs", `edit-song.${songId}`],
-				args: ["event:admin.hiddenSong.created", { data: { song } }]
-			})
-		);
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.removedHiddenSong",
-	cb: songId => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: "admin.hiddenSongs",
-			args: ["event:admin.hiddenSong.deleted", { data: { songId } }]
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "song.updatedHiddenSong",
-	cb: async songId => {
-		const songModel = await DBModule.runJob("GET_MODEL", {
-			modelName: "song"
-		});
-
-		songModel.findOne({ _id: songId }, (err, song) => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.hiddenSongs",
-				args: ["event:admin.hiddenSong.updated", { data: { song } }]
+				rooms: [
+					"import-album",
+					"admin.songs",
+					"admin.unverifiedSongs",
+					"admin.hiddenSongs",
+					`edit-song.${data.songId}`
+				],
+				args: ["event:admin.song.updated", { data: { song, oldStatus: data.oldStatus } }]
 			});
 		});
 	}
@@ -421,23 +322,6 @@ export default {
 
 				this.log("SUCCESS", "SONGS_UPDATE", `Successfully updated song "${songId}".`);
 
-				if (song.status === "verified") {
-					CacheModule.runJob("PUB", {
-						channel: "song.updatedVerifiedSong",
-						value: song._id
-					});
-				} else if (song.status === "unverified") {
-					CacheModule.runJob("PUB", {
-						channel: "song.updatedUnverifiedSong",
-						value: song._id
-					});
-				} else if (song.status === "hidden") {
-					CacheModule.runJob("PUB", {
-						channel: "song.updatedHiddenSong",
-						value: song._id
-					});
-				}
-
 				return cb({
 					status: "success",
 					message: "Song has been successfully updated",
@@ -684,11 +568,17 @@ export default {
 							.catch(() => {});
 					});
 
-					SongsModule.runJob("UPDATE_SONG", { songId: song._id });
+					song.artists.forEach(artist => {
+						PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist })
+							.then(() => {})
+							.catch(() => {});
+					});
+
+					SongsModule.runJob("UPDATE_SONG", { songId: song._id, oldStatus });
 					next(null, song, oldStatus);
 				}
 			],
-			async (err, song, oldStatus) => {
+			async err => {
 				if (err) {
 					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
 					this.log("ERROR", "SONGS_VERIFY", `User "${session.userId}" failed to verify song. "${err}"`);
@@ -697,22 +587,6 @@ export default {
 
 				this.log("SUCCESS", "SONGS_VERIFY", `User "${session.userId}" successfully verified song "${songId}".`);
 
-				if (oldStatus === "hidden")
-					CacheModule.runJob("PUB", {
-						channel: "song.removedHiddenSong",
-						value: song._id
-					});
-
-				CacheModule.runJob("PUB", {
-					channel: "song.newVerifiedSong",
-					value: song._id
-				});
-
-				CacheModule.runJob("PUB", {
-					channel: "song.removedUnverifiedSong",
-					value: song._id
-				});
-
 				return cb({
 					status: "success",
 					message: "Song has been verified successfully."
@@ -756,7 +630,13 @@ export default {
 							.catch(() => {});
 					});
 
-					SongsModule.runJob("UPDATE_SONG", { songId });
+					song.artists.forEach(artist => {
+						PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist })
+							.then(() => {})
+							.catch(() => {});
+					});
+
+					SongsModule.runJob("UPDATE_SONG", { songId, oldStatus: "verified" });
 
 					next(null);
 				}
@@ -776,16 +656,6 @@ export default {
 					`User "${session.userId}" successfully unverified song "${songId}".`
 				);
 
-				CacheModule.runJob("PUB", {
-					channel: "song.newUnverifiedSong",
-					value: songId
-				});
-
-				CacheModule.runJob("PUB", {
-					channel: "song.removedVerifiedSong",
-					value: songId
-				});
-
 				return cb({
 					status: "success",
 					message: "Song has been unverified successfully."

+ 17 - 38
backend/logic/songs.js

@@ -297,6 +297,7 @@ class _SongsModule extends CoreClass {
 	 *
 	 * @param {object} payload - an object containing the payload
 	 * @param {string} payload.songId - the id of the song we are trying to update
+	 * @param {string} payload.oldStatus - old status of song being updated (optional)
 	 * @returns {Promise} - returns a promise (resolve, reject)
 	 */
 	UPDATE_SONG(payload) {
@@ -460,6 +461,14 @@ class _SongsModule extends CoreClass {
 				],
 				(err, song) => {
 					if (err && err !== true) return reject(new Error(err));
+
+					if (!payload.oldStatus) payload.oldStatus = null;
+
+					CacheModule.runJob("PUB", {
+						channel: "song.updated",
+						value: { songId: song._id, oldStatus: payload.oldStatus }
+					});
+
 					return resolve(song);
 				}
 			)
@@ -946,11 +955,6 @@ class _SongsModule extends CoreClass {
 
 					SongsModule.runJob("UPDATE_SONG", { songId: song._id });
 
-					CacheModule.runJob("PUB", {
-						channel: "song.newUnverifiedSong",
-						value: song._id
-					});
-
 					return resolve({ song: trimmedSong });
 				}
 			);
@@ -979,36 +983,22 @@ class _SongsModule extends CoreClass {
 						if (!song) return next("This song does not exist.");
 						if (song.status === "hidden") return next("This song is already hidden.");
 						// TODO Add err object as first param of callback
-						return next();
+						return next(null, song.status);
 					},
 
-					next => {
-						SongsModule.SongModel.updateOne({ _id: songId }, { status: "hidden" }, next);
+					(oldStatus, next) => {
+						SongsModule.SongModel.updateOne({ _id: songId }, { status: "hidden" }, res =>
+							next(null, res, oldStatus)
+						);
 					},
 
-					(res, next) => {
-						SongsModule.runJob("UPDATE_SONG", { songId });
+					(res, oldStatus, next) => {
+						SongsModule.runJob("UPDATE_SONG", { songId, oldStatus });
 						next();
 					}
 				],
 				async err => {
 					if (err) reject(err);
-
-					CacheModule.runJob("PUB", {
-						channel: "song.newHiddenSong",
-						value: songId
-					});
-
-					CacheModule.runJob("PUB", {
-						channel: "song.removedUnverifiedSong",
-						value: songId
-					});
-
-					CacheModule.runJob("PUB", {
-						channel: "song.removedVerifiedSong",
-						value: songId
-					});
-
 					resolve();
 				}
 			);
@@ -1045,23 +1035,12 @@ class _SongsModule extends CoreClass {
 					},
 
 					(res, next) => {
-						SongsModule.runJob("UPDATE_SONG", { songId });
+						SongsModule.runJob("UPDATE_SONG", { songId, oldStatus: "hidden" });
 						next();
 					}
 				],
 				async err => {
 					if (err) reject(err);
-
-					CacheModule.runJob("PUB", {
-						channel: "song.newUnverifiedSong",
-						value: songId
-					});
-
-					CacheModule.runJob("PUB", {
-						channel: "song.removedHiddenSong",
-						value: songId
-					});
-
 					resolve();
 				}
 			);

+ 1 - 19
frontend/src/components/modals/EditSong/index.vue

@@ -673,25 +673,7 @@ export default {
 		this.volumeSliderValue = volume * 100;
 
 		this.socket.on(
-			"event:admin.hiddenSong.created",
-			res => {
-				if (res.data.song._id === this.song._id)
-					this.song.status = res.data.song.status;
-			},
-			{ modal: "editSong" }
-		);
-
-		this.socket.on(
-			"event:admin.unverifiedSong.created",
-			res => {
-				if (res.data.song._id === this.song._id)
-					this.song.status = res.data.song.status;
-			},
-			{ modal: "editSong" }
-		);
-
-		this.socket.on(
-			"event:admin.verifiedSong.created",
+			"event:admin.song.updated",
 			res => {
 				if (res.data.song._id === this.song._id)
 					this.song.status = res.data.song.status;

+ 8 - 11
frontend/src/pages/Admin/tabs/HiddenSongs.vue

@@ -391,17 +391,14 @@ export default {
 	mounted() {
 		ws.onConnect(this.init);
 
-		this.socket.on("event:admin.hiddenSong.created", res => {
-			console.log("CREATED");
-			this.addSong(res.data.song);
-		});
-
-		this.socket.on("event:admin.hiddenSong.deleted", res => {
-			this.removeSong(res.data.songId);
-		});
-
-		this.socket.on("event:admin.hiddenSong.updated", res => {
-			this.updateSong(res.data.song);
+		this.socket.on("event:admin.song.updated", res => {
+			const { song } = res.data;
+			if (res.data.oldStatus && res.data.oldStatus === "hidden") {
+				this.removeSong(song._id);
+			} else {
+				this.addSong(song);
+				this.updateSong(song);
+			}
 		});
 	},
 	methods: {

+ 9 - 11
frontend/src/pages/Admin/tabs/UnverifiedSongs.vue

@@ -407,19 +407,17 @@ export default {
 		})
 	},
 	mounted() {
-		this.socket.on("event:admin.unverifiedSong.created", res => {
-			this.addSong(res.data.song);
-		});
-
-		this.socket.on("event:admin.unverifiedSong.deleted", res => {
-			this.removeSong(res.data.songId);
-		});
+		ws.onConnect(this.init);
 
-		this.socket.on("event:admin.unverifiedSong.updated", res => {
-			this.updateSong(res.data.song);
+		this.socket.on("event:admin.song.updated", res => {
+			const { song } = res.data;
+			if (res.data.oldStatus && res.data.oldStatus === "unverified") {
+				this.removeSong(song._id);
+			} else {
+				this.addSong(song);
+				this.updateSong(song);
+			}
 		});
-
-		ws.onConnect(this.init);
 	},
 	methods: {
 		edit(song) {

+ 10 - 12
frontend/src/pages/Admin/tabs/VerifiedSongs.vue

@@ -425,20 +425,18 @@ export default {
 		})
 	},
 	mounted() {
-		this.socket.on("event:admin.verifiedSong.created", res =>
-			this.addSong(res.data.song)
-		);
-
-		this.socket.on("event:admin.verifiedSong.deleted", res =>
-			this.removeSong(res.data.songId)
-		);
-
-		this.socket.on("event:admin.verifiedSong.updated", res =>
-			this.updateSong(res.data.song)
-		);
-
 		ws.onConnect(this.init);
 
+		this.socket.on("event:admin.song.updated", res => {
+			const { song } = res.data;
+			if (res.data.oldStatus && res.data.oldStatus === "verified") {
+				this.removeSong(song._id);
+			} else {
+				this.addSong(song);
+				this.updateSong(song);
+			}
+		});
+
 		if (this.$route.query.songId) {
 			this.socket.dispatch(
 				"songs.getSongFromSongId",

+ 9 - 6
frontend/src/store/modules/admin.js

@@ -27,7 +27,8 @@ const modules = {
 				state.songs = [];
 			},
 			addSong(state, song) {
-				state.songs.push(song);
+				if (!state.songs.find(s => s._id === song._id))
+					state.songs.push(song);
 			},
 			removeSong(state, songId) {
 				state.songs = state.songs.filter(song => song._id !== songId);
@@ -35,7 +36,7 @@ const modules = {
 			updateSong(state, updatedSong) {
 				state.songs.forEach((song, index) => {
 					if (song._id === updatedSong._id)
-						this.set(state.songs, index, updatedSong);
+						state.songs[index] = updatedSong;
 				});
 			}
 		}
@@ -58,7 +59,8 @@ const modules = {
 				state.songs = [];
 			},
 			addSong(state, song) {
-				state.songs.push(song);
+				if (!state.songs.find(s => s._id === song._id))
+					state.songs.push(song);
 			},
 			removeSong(state, songId) {
 				state.songs = state.songs.filter(song => song._id !== songId);
@@ -66,7 +68,7 @@ const modules = {
 			updateSong(state, updatedSong) {
 				state.songs.forEach((song, index) => {
 					if (song._id === updatedSong._id)
-						this.set(state.songs, index, updatedSong);
+						state.songs[index] = updatedSong;
 				});
 			}
 		}
@@ -89,7 +91,8 @@ const modules = {
 				state.songs = [];
 			},
 			addSong(state, song) {
-				state.songs.push(song);
+				if (!state.songs.find(s => s._id === song._id))
+					state.songs.push(song);
 			},
 			removeSong(state, songId) {
 				state.songs = state.songs.filter(song => song._id !== songId);
@@ -97,7 +100,7 @@ const modules = {
 			updateSong(state, updatedSong) {
 				state.songs.forEach((song, index) => {
 					if (song._id === updatedSong._id)
-						this.set(state.songs, index, updatedSong);
+						state.songs[index] = updatedSong;
 				});
 			}
 		}