Browse Source

refactor: Ratings and youtube module improvements

Owen Diffey 2 years ago
parent
commit
c56850740b

+ 28 - 36
backend/logic/actions/ratings.js

@@ -205,12 +205,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error" && res.message !== "Song wasn't in playlist.")
+						.then(() => next(null, song, user.likedSongsPlaylist))
+						.catch(res => {
+							if (!(res.message && res.message === "That song is not currently in the playlist."))
 								return next("Unable to remove song from the 'Disliked Songs' playlist.");
 							return next(null, song, user.likedSongsPlaylist);
-						})
-						.catch(err => next(err));
+						});
 				},
 
 				(song, likedSongsPlaylist, next) =>
@@ -225,16 +225,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error") {
-								if (res.message === "That song is already in the playlist")
-									return next("You have already liked this song.");
-								return next("Unable to add song to the 'Liked Songs' playlist.");
-							}
-
-							return next(null, song);
-						})
-						.catch(err => next(err)),
+						.then(() => next(null, song))
+						.catch(res => {
+							if (res.message && res.message === "That song is already in the playlist")
+								return next("You have already liked this song.");
+							return next("Unable to add song to the 'Liked Songs' playlist.");
+						}),
 
 				(song, next) => {
 					RatingsModule.runJob("RECALCULATE_RATINGS", { youtubeId })
@@ -337,12 +333,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error" && res.message !== "Song wasn't in playlist.")
+						.then(() => next(null, song, user.dislikedSongsPlaylist))
+						.catch(res => {
+							if (!(res.message && res.message === "That song is not currently in the playlist."))
 								return next("Unable to remove song from the 'Liked Songs' playlist.");
 							return next(null, song, user.dislikedSongsPlaylist);
-						})
-						.catch(err => next(err));
+						});
 				},
 
 				(song, dislikedSongsPlaylist, next) =>
@@ -357,16 +353,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error") {
-								if (res.message === "That song is already in the playlist")
-									return next("You have already disliked this song.");
-								return next("Unable to add song to the 'Disliked Songs' playlist.");
-							}
-
-							return next(null, song);
-						})
-						.catch(err => next(err)),
+						.then(() => next(null, song))
+						.catch(res => {
+							if (res.message && res.message === "That song is already in the playlist")
+								return next("You have already disliked this song.");
+							return next("Unable to add song to the 'Disliked Songs' playlist.");
+						}),
 
 				(song, next) => {
 					RatingsModule.runJob("RECALCULATE_RATINGS", { youtubeId })
@@ -489,12 +481,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error" && res.message !== "Song wasn't in playlist.")
+						.then(() => next(null, song))
+						.catch(res => {
+							if (!(res.message && res.message === "That song is not currently in the playlist."))
 								return next("Unable to remove song from the 'Liked Songs' playlist.");
 							return next(null, song);
-						})
-						.catch(err => next(err));
+						});
 				},
 
 				(song, next) => {
@@ -600,12 +592,12 @@ export default {
 							},
 							this
 						)
-						.then(res => {
-							if (res.status === "error" && res.message !== "Song wasn't in playlist.")
+						.then(() => next(null, song, user.likedSongsPlaylist))
+						.catch(res => {
+							if (!(res.message && res.message === "That song is not currently in the playlist."))
 								return next("Unable to remove song from the 'Disliked Songs' playlist.");
 							return next(null, song, user.likedSongsPlaylist);
-						})
-						.catch(err => next(err));
+						});
 				},
 
 				(song, likedSongsPlaylist, next) => {

+ 14 - 0
backend/logic/actions/songs.js

@@ -13,6 +13,7 @@ const SongsModule = moduleManager.modules.songs;
 const PlaylistsModule = moduleManager.modules.playlists;
 const StationsModule = moduleManager.modules.stations;
 const RatingsModule = moduleManager.modules.ratings;
+const YouTubeModule = moduleManager.modules.youtube;
 
 CacheModule.runJob("SUB", {
 	channel: "song.updated",
@@ -527,6 +528,19 @@ export default {
 
 				(song, next) => {
 					RatingsModule.runJob("REMOVE_RATINGS", { youtubeIds: song.youtubeId }, this)
+						.then(() => next(null, song.youtubeId))
+						.catch(next);
+				},
+
+				(youtubeId, next) => {
+					YouTubeModule.youtubeVideoModel.findOne({ youtubeId }, (err, video) => {
+						if (err) next(err);
+						else next(null, video._id);
+					});
+				},
+
+				(videoIds, next) => {
+					YouTubeModule.runJob("REMOVE_VIDEOS", { videoIds }, this)
 						.then(() => next())
 						.catch(next);
 				},

+ 12 - 97
backend/logic/actions/youtube.js

@@ -9,41 +9,6 @@ import moduleManager from "../../index";
 const DBModule = moduleManager.modules.db;
 const UtilsModule = moduleManager.modules.utils;
 const YouTubeModule = moduleManager.modules.youtube;
-const WSModule = moduleManager.modules.ws;
-const CacheModule = moduleManager.modules.cache;
-
-CacheModule.runJob("SUB", {
-	channel: "youtube.removeYoutubeApiRequest",
-	cb: requestId => {
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: `view-api-request.${requestId}`,
-			args: ["event:youtubeApiRequest.removed"]
-		});
-
-		WSModule.runJob("EMIT_TO_ROOM", {
-			room: "admin.youtube",
-			args: ["event:admin.youtubeApiRequest.removed", { data: { requestId } }]
-		});
-	}
-});
-
-CacheModule.runJob("SUB", {
-	channel: "youtube.removeVideos",
-	cb: videoIds => {
-		const videos = Array.isArray(videoIds) ? videoIds : [videoIds];
-		videos.forEach(videoId => {
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: `view-youtube-video.${videoId}`,
-				args: ["event:youtubeVideo.removed"]
-			});
-
-			WSModule.runJob("EMIT_TO_ROOM", {
-				room: "admin.youtubeVideos",
-				args: ["event:admin.youtubeVideo.removed", { data: { videoId } }]
-			});
-		});
-	}
-});
 
 export default {
 	/**
@@ -164,64 +129,24 @@ export default {
 	 * @returns {{status: string, data: object}}
 	 */
 	resetStoredApiRequests: isAdminRequired(async function resetStoredApiRequests(session, cb) {
-		async.waterfall(
-			[
-				next => {
-					YouTubeModule.youtubeApiRequestModel.find({}, next);
-				},
-
-				(apiRequests, next) => {
-					YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
-						.then(() => next(null, apiRequests))
-						.catch(err => next(err));
-				},
-
-				(apiRequests, next) => {
-					async.eachLimit(
-						apiRequests.map(apiRequest => apiRequest._id),
-						1,
-						(requestId, next) => {
-							CacheModule.runJob(
-								"PUB",
-								{
-									channel: "youtube.removeYoutubeApiRequest",
-									value: requestId
-								},
-								this
-							)
-								.then(() => {
-									next();
-								})
-								.catch(err => {
-									next(err);
-								});
-						},
-						err => {
-							if (err) next(err);
-							else next();
-						}
-					);
-				}
-			],
-			async err => {
-				if (err) {
-					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
-					this.log(
-						"ERROR",
-						"YOUTUBE_RESET_STORED_API_REQUESTS",
-						`Resetting stored API requests failed. "${err}"`
-					);
-					return cb({ status: "error", message: err });
-				}
-
+		YouTubeModule.runJob("RESET_STORED_API_REQUESTS", {}, this)
+			.then(() => {
 				this.log(
 					"SUCCESS",
 					"YOUTUBE_RESET_STORED_API_REQUESTS",
 					`Resetting stored API requests was successful.`
 				);
 				return cb({ status: "success", message: "Successfully reset stored YouTube API requests" });
-			}
-		);
+			})
+			.catch(async err => {
+				err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+				this.log(
+					"ERROR",
+					"YOUTUBE_RESET_STORED_API_REQUESTS",
+					`Resetting stored API requests failed. "${err}"`
+				);
+				return cb({ status: "error", message: err });
+			});
 	}),
 
 	/**
@@ -238,11 +163,6 @@ export default {
 					`Removing stored API request "${requestId}" was successful.`
 				);
 
-				CacheModule.runJob("PUB", {
-					channel: "youtube.removeYoutubeApiRequest",
-					value: requestId
-				});
-
 				return cb({ status: "success", message: "Successfully removed stored YouTube API request" });
 			})
 			.catch(async err => {
@@ -350,11 +270,6 @@ export default {
 			.then(() => {
 				this.log("SUCCESS", "YOUTUBE_REMOVE_VIDEOS", `Removing videos was successful.`);
 
-				CacheModule.runJob("PUB", {
-					channel: "youtube.removeVideos",
-					value: videoIds
-				});
-
 				return cb({ status: "success", message: "Successfully removed YouTube videos" });
 			})
 			.catch(async err => {

+ 17 - 3
backend/logic/ratings.js

@@ -139,7 +139,7 @@ class _RatingsModule extends CoreClass {
 					},
 
 					({ likes, dislikes }, next) => {
-						RatingsModule.RatingsModel.updateOne(
+						RatingsModule.RatingsModel.findOneAndUpdate(
 							{ youtubeId: payload.youtubeId },
 							{
 								$set: {
@@ -147,9 +147,23 @@ class _RatingsModule extends CoreClass {
 									dislikes
 								}
 							},
-							{ upsert: true },
-							err => next(err, { likes, dislikes })
+							{ new: true, upsert: true },
+							next
 						);
+					},
+
+					(ratings, next) => {
+						CacheModule.runJob(
+							"HSET",
+							{
+								table: "ratings",
+								key: payload.youtubeId,
+								value: ratings
+							},
+							this
+						)
+							.then(ratings => next(null, ratings))
+							.catch(next);
 					}
 				],
 				(err, { likes, dislikes }) => {

+ 83 - 5
backend/logic/youtube.js

@@ -114,6 +114,39 @@ class _YouTubeModule extends CoreClass {
 			PlaylistsModule = this.moduleManager.modules.playlists;
 			WSModule = this.moduleManager.modules.ws;
 
+			CacheModule.runJob("SUB", {
+				channel: "youtube.removeYoutubeApiRequest",
+				cb: requestId => {
+					WSModule.runJob("EMIT_TO_ROOM", {
+						room: `view-api-request.${requestId}`,
+						args: ["event:youtubeApiRequest.removed"]
+					});
+			
+					WSModule.runJob("EMIT_TO_ROOM", {
+						room: "admin.youtube",
+						args: ["event:admin.youtubeApiRequest.removed", { data: { requestId } }]
+					});
+				}
+			});
+
+			CacheModule.runJob("SUB", {
+				channel: "youtube.removeVideos",
+				cb: videoIds => {
+					const videos = Array.isArray(videoIds) ? videoIds : [videoIds];
+					videos.forEach(videoId => {
+						WSModule.runJob("EMIT_TO_ROOM", {
+							room: `view-youtube-video.${videoId}`,
+							args: ["event:youtubeVideo.removed"]
+						});
+			
+						WSModule.runJob("EMIT_TO_ROOM", {
+							room: "admin.youtubeVideos",
+							args: ["event:admin.youtubeVideo.removed", { data: { videoId } }]
+						});
+					});
+				}
+			});
+
 			this.youtubeApiRequestModel = this.YoutubeApiRequestModel = await DBModule.runJob("GET_MODEL", {
 				modelName: "youtubeApiRequest"
 			});
@@ -903,28 +936,59 @@ class _YouTubeModule extends CoreClass {
 			async.waterfall(
 				[
 					next => {
+						YouTubeModule.youtubeApiRequestModel.find({}, next);
+					},
+
+					(apiRequests, next) => {
 						YouTubeModule.youtubeApiRequestModel.deleteMany({}, err => {
 							if (err) next("Couldn't reset stored YouTube API requests.");
 							else {
-								next();
+								next(null, apiRequests);
 							}
 						});
 					},
 
-					next => {
+					(apiRequests, next) => {
 						CacheModule.runJob(
 							"DEL",
 							{key: "youtubeApiRequestParams"},
 							this
-						).then(next).catch(err => next(err));
+						).then(() => next(null, apiRequests)).catch(err => next(err));
 					},
 
-					next => {
+					(apiRequests, next) => {
 						CacheModule.runJob(
 							"DEL",
 							{key: "youtubeApiRequestResults"},
 							this
-						).then(next).catch(err => next(err));
+						).then(() => next(null, apiRequests)).catch(err => next(err));
+					},
+
+					(apiRequests, next) => {
+						async.eachLimit(
+							apiRequests.map(apiRequest => apiRequest._id),
+							1,
+							(requestId, next) => {
+								CacheModule.runJob(
+									"PUB",
+									{
+										channel: "youtube.removeYoutubeApiRequest",
+										value: requestId
+									},
+									this
+								)
+									.then(() => {
+										next();
+									})
+									.catch(err => {
+										next(err);
+									});
+							},
+							err => {
+								if (err) next(err);
+								else next();
+							}
+						);
 					}
 				],
 				err => {
@@ -969,6 +1033,13 @@ class _YouTubeModule extends CoreClass {
 							},
 							this
 						).then(next).catch(err => next(err));
+					},
+
+					next => {
+						CacheModule.runJob("PUB", {
+							channel: "youtube.removeYoutubeApiRequest",
+							value: requestId
+						}).then(next).catch(err => next(err));;
 					}
 				],
 				err => {
@@ -1242,6 +1313,13 @@ class _YouTubeModule extends CoreClass {
 
 					next => {
 						YouTubeModule.youtubeVideoModel.deleteMany({_id: { $in: videoIds }}, next);
+					},
+
+					(res, next) => {
+						CacheModule.runJob("PUB", {
+							channel: "youtube.removeVideos",
+							value: videoIds
+						}).then(next).catch(err => next(err));
 					}
 				],
 				err => {

+ 1 - 1
frontend/src/components/SongItem.vue

@@ -85,7 +85,7 @@
 									openModal({
 										modal: 'viewYoutubeVideo',
 										data: {
-											videoId: song.youtubeId
+											youtubeId: song.youtubeId
 										}
 									})
 								"

+ 2 - 1
frontend/src/components/modals/ViewYoutubeVideo.vue

@@ -254,6 +254,7 @@ export default {
 	computed: {
 		...mapModalState("modals/viewYoutubeVideo/MODAL_UUID", {
 			videoId: state => state.videoId,
+			youtubeId: state => state.youtubeId,
 			video: state => state.video,
 			player: state => state.player
 		}),
@@ -293,7 +294,7 @@ export default {
 			this.loaded = false;
 			this.socket.dispatch(
 				"youtube.getVideo",
-				this.videoId,
+				this.videoId || this.youtubeId,
 				true,
 				res => {
 					if (res.status === "success") {

+ 5 - 1
frontend/src/store/modules/modals/viewYoutubeVideo.js

@@ -4,6 +4,7 @@ export default {
 	namespaced: true,
 	state: {
 		videoId: null,
+		youtubeId: null,
 		video: {},
 		player: {
 			error: false,
@@ -33,10 +34,13 @@ export default {
 		setPlaybackRate: ({ commit }, rate) => commit("setPlaybackRate", rate)
 	},
 	mutations: {
-		init(state, { videoId }) {
+		init(state, { videoId, youtubeId }) {
 			state.videoId = videoId;
+			state.youtubeId = youtubeId;
 		},
 		viewYoutubeVideo(state, video) {
+			state.videoId = state.videoId || video._id;
+			state.youtubeId = video.youtubeId || video.youtubeId;
 			state.video = video;
 		},
 		updatePlayer(state, player) {