Browse Source

fix(Station): skip votes and own song ratings would sometimes not load correctly

Kristian Vos 3 years ago
parent
commit
afafe95d73

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

@@ -3950,5 +3950,55 @@ export default {
 				return cb({ status: "success", message: "Successfully cleared and refilled station queue." });
 				return cb({ status: "success", message: "Successfully cleared and refilled station queue." });
 			}
 			}
 		);
 		);
+	}),
+
+	/**
+	 * Gets skip votes for a station
+	 *
+	 * @param session
+	 * @param stationId - the station id
+	 * @param cb
+	 */
+
+	getSkipVotes: isLoginRequired(async function getSkipVotes(session, stationId, cb) {
+		async.waterfall(
+			[
+				next => {
+					StationsModule.runJob("GET_STATION", { stationId }, this)
+						.then(res => next(null, res.currentSong))
+						.catch(console.log);
+				},
+
+				(currentSong, next) => {
+					if (currentSong)
+						next(null, {
+							skipVotes: currentSong.skipVotes.length,
+							songId: currentSong._id
+						});
+					else next("There is no song currently playing.");
+				}
+			],
+			async (err, data) => {
+				if (err) {
+					err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
+					this.log(
+						"ERROR",
+						"STATIONS_GET_SKIP_VOTES",
+						`User "${session.userId}" failed to get skip votes for ${stationId}. "${err}"`
+					);
+					return cb({ status: "error", message: err });
+				}
+
+				const { skipVotes, songId } = data;
+
+				return cb({
+					status: "success",
+					data: {
+						skipVotes,
+						songId
+					}
+				});
+			}
+		);
 	})
 	})
 };
 };

+ 80 - 34
frontend/src/pages/Station/index.vue

@@ -345,7 +345,7 @@
 									<button
 									<button
 										v-else
 										v-else
 										class="button is-primary disabled"
 										class="button is-primary disabled"
-										content="Login to vote to skip songs"
+										content="Log in to vote to skip songs"
 										v-tippy="{ theme: 'info' }"
 										v-tippy="{ theme: 'info' }"
 									>
 									>
 										<i
 										<i
@@ -403,10 +403,10 @@
 									<!-- Ratings (Like/Dislike) Buttons -->
 									<!-- Ratings (Like/Dislike) Buttons -->
 									<div
 									<div
 										id="ratings"
 										id="ratings"
-										v-if="ratingsLoaded"
+										v-if="ratingsLoaded && ownRatingsLoaded"
 										:class="{
 										:class="{
-											liked: liked,
-											disliked: disliked
+											liked: currentSong.liked,
+											disliked: currentSong.disliked
 										}"
 										}"
 									>
 									>
 										<!-- Like Song Button -->
 										<!-- Like Song Button -->
@@ -419,7 +419,9 @@
 										>
 										>
 											<i
 											<i
 												class="material-icons icon-with-button"
 												class="material-icons icon-with-button"
-												:class="{ liked: liked }"
+												:class="{
+													liked: currentSong.liked
+												}"
 												>thumb_up_alt</i
 												>thumb_up_alt</i
 											>{{ currentSong.likes }}
 											>{{ currentSong.likes }}
 										</button>
 										</button>
@@ -435,7 +437,8 @@
 											<i
 											<i
 												class="material-icons icon-with-button"
 												class="material-icons icon-with-button"
 												:class="{
 												:class="{
-													disliked: disliked
+													disliked:
+														currentSong.disliked
 												}"
 												}"
 												>thumb_down_alt</i
 												>thumb_down_alt</i
 											>{{ currentSong.dislikes }}
 											>{{ currentSong.dislikes }}
@@ -516,7 +519,7 @@
 										<button
 										<button
 											class="button is-success disabled"
 											class="button is-success disabled"
 											id="like-song"
 											id="like-song"
-											content="Login to like songs"
+											content="Log in to like songs"
 											v-tippy="{ theme: 'info' }"
 											v-tippy="{ theme: 'info' }"
 										>
 										>
 											<i
 											<i
@@ -529,7 +532,7 @@
 										<button
 										<button
 											class="button is-danger disabled"
 											class="button is-danger disabled"
 											id="dislike-song"
 											id="dislike-song"
-											content="Login to dislike songs"
+											content="Log in to dislike songs"
 											v-tippy="{ theme: 'info' }"
 											v-tippy="{ theme: 'info' }"
 										>
 										>
 											<i
 											<i
@@ -570,7 +573,7 @@
 										<div class="control has-addons">
 										<div class="control has-addons">
 											<button
 											<button
 												class="button is-primary disabled"
 												class="button is-primary disabled"
-												content="Login to add songs to playlist"
+												content="Log in to add songs to playlist"
 												v-tippy="{ theme: 'info' }"
 												v-tippy="{ theme: 'info' }"
 											>
 											>
 												<i class="material-icons"
 												<i class="material-icons"
@@ -674,6 +677,29 @@
 				<span
 				<span
 					><b>Party playlists selected</b>: {{ partyPlaylists }}</span
 					><b>Party playlists selected</b>: {{ partyPlaylists }}</span
 				>
 				>
+				<span><b>Skip votes loaded</b>: {{ skipVotesLoaded }}</span>
+				<span
+					><b>Skip votes</b>:
+					{{ skipVotesLoaded ? currentSong.skipVotes : "N/A" }}</span
+				>
+				<span><b>Ratings loaded</b>: {{ ratingsLoaded }}</span>
+				<span
+					><b>Ratings</b>:
+					{{
+						ratingsLoaded
+							? `${currentSong.likes} / ${currentSong.dislikes}`
+							: "N/A"
+					}}</span
+				>
+				<span><b>Own ratings loaded</b>: {{ ownRatingsLoaded }}</span>
+				<span
+					><b>Own ratings</b>:
+					{{
+						ownRatingsLoaded
+							? `${currentSong.liked} / ${currentSong.disliked}`
+							: "N/A"
+					}}</span
+				>
 			</template>
 			</template>
 		</floating-box>
 		</floating-box>
 
 
@@ -744,8 +770,6 @@ export default {
 			timePaused: 0,
 			timePaused: 0,
 			muted: false,
 			muted: false,
 			timeElapsed: "0:00",
 			timeElapsed: "0:00",
-			liked: false,
-			disliked: false,
 			timeBeforePause: 0,
 			timeBeforePause: 0,
 			systemDifference: 0,
 			systemDifference: 0,
 			attemptsToPlayVideo: 0,
 			attemptsToPlayVideo: 0,
@@ -784,6 +808,13 @@ export default {
 				this.currentSong.dislikes >= 0
 				this.currentSong.dislikes >= 0
 			);
 			);
 		},
 		},
+		ownRatingsLoaded() {
+			return (
+				!this.noSong &&
+				typeof this.currentSong.liked === "boolean" &&
+				typeof this.currentSong.disliked === "boolean"
+			);
+		},
 		...mapState("modalVisibility", {
 		...mapState("modalVisibility", {
 			modals: state => state.modals
 			modals: state => state.modals
 		}),
 		}),
@@ -966,8 +997,7 @@ export default {
 		this.socket.on("event:song.ratings.updated", res => {
 		this.socket.on("event:song.ratings.updated", res => {
 			if (!this.noSong) {
 			if (!this.noSong) {
 				if (res.data.youtubeId === this.currentSong.youtubeId) {
 				if (res.data.youtubeId === this.currentSong.youtubeId) {
-					this.liked = res.data.liked;
-					this.disliked = res.data.disliked;
+					this.updateOwnCurrentSongRatings(res.data);
 				}
 				}
 			}
 			}
 		});
 		});
@@ -1269,6 +1299,18 @@ export default {
 					}, this.getTimeRemaining());
 					}, this.getTimeRemaining());
 				}
 				}
 
 
+				this.socket.dispatch(
+					"stations.getSkipVotes",
+					this.station._id,
+					res => {
+						if (res.status === "success") {
+							const { skipVotes, songId } = res.data;
+							if (!this.noSong && this.currentSong._id === songId)
+								this.updateCurrentSongSkipVotes(skipVotes);
+						}
+					}
+				);
+
 				this.socket.dispatch(
 				this.socket.dispatch(
 					"songs.getSongRatings",
 					"songs.getSongRatings",
 					currentSong._id,
 					currentSong._id,
@@ -1280,29 +1322,32 @@ export default {
 					}
 					}
 				);
 				);
 
 
-				this.socket.dispatch(
-					"songs.getOwnSongRatings",
-					currentSong.youtubeId,
-					res => {
-						if (
-							res.status === "success" &&
-							this.currentSong.youtubeId === res.data.youtubeId
-						) {
-							this.liked = res.data.liked;
-							this.disliked = res.data.disliked;
-
+				if (this.loggedIn) {
+					this.socket.dispatch(
+						"songs.getOwnSongRatings",
+						currentSong.youtubeId,
+						res => {
+							console.log("getOwnSongRatings", res);
 							if (
 							if (
-								this.autoSkipDisliked &&
-								res.data.disliked === true
+								res.status === "success" &&
+								this.currentSong.youtubeId ===
+									res.data.youtubeId
 							) {
 							) {
-								this.voteSkipStation();
-								new Toast(
-									"Automatically voted to skip disliked song."
-								);
+								this.updateOwnCurrentSongRatings(res.data);
+
+								if (
+									this.autoSkipDisliked &&
+									res.data.disliked === true
+								) {
+									this.voteSkipStation();
+									new Toast(
+										"Automatically voted to skip disliked song."
+									);
+								}
 							}
 							}
 						}
 						}
-					}
-				);
+					);
+				}
 			} else {
 			} else {
 				if (this.playerReady) this.player.pauseVideo();
 				if (this.playerReady) this.player.pauseVideo();
 				this.updateNoSong(true);
 				this.updateNoSong(true);
@@ -1711,7 +1756,7 @@ export default {
 			}
 			}
 		},
 		},
 		toggleLike() {
 		toggleLike() {
-			if (this.liked)
+			if (this.currentSong.liked)
 				this.socket.dispatch(
 				this.socket.dispatch(
 					"songs.unlike",
 					"songs.unlike",
 					this.currentSong.youtubeId,
 					this.currentSong.youtubeId,
@@ -1731,7 +1776,7 @@ export default {
 				);
 				);
 		},
 		},
 		toggleDislike() {
 		toggleDislike() {
-			if (this.disliked)
+			if (this.currentSong.disliked)
 				return this.socket.dispatch(
 				return this.socket.dispatch(
 					"songs.undislike",
 					"songs.undislike",
 					this.currentSong.youtubeId,
 					this.currentSong.youtubeId,
@@ -2117,6 +2162,7 @@ export default {
 			"setIncludedPlaylists",
 			"setIncludedPlaylists",
 			"setExcludedPlaylists",
 			"setExcludedPlaylists",
 			"updateCurrentSongRatings",
 			"updateCurrentSongRatings",
+			"updateOwnCurrentSongRatings",
 			"updateCurrentSongSkipVotes"
 			"updateCurrentSongSkipVotes"
 		]),
 		]),
 		...mapActions("modals/editSong", ["stopVideo"])
 		...mapActions("modals/editSong", ["stopVideo"])

+ 7 - 0
frontend/src/store/modules/station.js

@@ -73,6 +73,9 @@ const actions = {
 	updateCurrentSongRatings: ({ commit }, songRatings) => {
 	updateCurrentSongRatings: ({ commit }, songRatings) => {
 		commit("updateCurrentSongRatings", songRatings);
 		commit("updateCurrentSongRatings", songRatings);
 	},
 	},
+	updateOwnCurrentSongRatings: ({ commit }, ownSongRatings) => {
+		commit("updateOwnCurrentSongRatings", ownSongRatings);
+	},
 	updateCurrentSongSkipVotes: ({ commit }, skipVotes) => {
 	updateCurrentSongSkipVotes: ({ commit }, skipVotes) => {
 		commit("updateCurrentSongSkipVotes", skipVotes);
 		commit("updateCurrentSongSkipVotes", skipVotes);
 	}
 	}
@@ -160,6 +163,10 @@ const mutations = {
 		state.currentSong.likes = songRatings.likes;
 		state.currentSong.likes = songRatings.likes;
 		state.currentSong.dislikes = songRatings.dislikes;
 		state.currentSong.dislikes = songRatings.dislikes;
 	},
 	},
+	updateOwnCurrentSongRatings(state, ownSongRatings) {
+		state.currentSong.liked = ownSongRatings.liked;
+		state.currentSong.disliked = ownSongRatings.disliked;
+	},
 	updateCurrentSongSkipVotes(state, skipVotes) {
 	updateCurrentSongSkipVotes(state, skipVotes) {
 		state.currentSong.skipVotes = skipVotes;
 		state.currentSong.skipVotes = skipVotes;
 	}
 	}