Browse Source

fix: excluded media sources wasn't up-to-date in station

Kristian Vos 1 year ago
parent
commit
673b9026be

+ 8 - 52
frontend/src/components/PlaylistTabBase.vue

@@ -39,8 +39,6 @@ const { socket } = useWebsocketsStore();
 const stationStore = useStationStore();
 const userPreferencesStore = useUserPreferencesStore();
 
-const { autoSkipDisliked } = storeToRefs(userPreferencesStore);
-
 const tab = ref("current");
 const search = reactive({
 	query: "",
@@ -65,8 +63,10 @@ const {
 } = useSortablePlaylists();
 
 const { experimental } = storeToRefs(configStore);
+const { autoSkipDisliked } = storeToRefs(userPreferencesStore);
 
-const { autoRequest, history, songsList } = storeToRefs(stationStore);
+const { autoRequest, autorequestExcludedMediaSources } =
+	storeToRefs(stationStore);
 
 const manageStationStore = useManageStationStore({
 	modalUuid: props.modalUuid
@@ -98,60 +98,12 @@ const blacklist = computed({
 	}
 });
 
-const dislikedPlaylist = computed(() =>
-	playlists.value.find(playlist => playlist.type === "user-disliked")
-);
-
 const resultsLeftCount = computed(() => search.count - search.results.length);
 
 const nextPageResultsCount = computed(() =>
 	Math.min(search.pageSize, resultsLeftCount.value)
 );
 
-// List of media sources that will not be allowed to be autorequested
-const excludedMediaSources = computed(() => {
-	const mediaSources = new Set();
-
-	// Exclude the current song
-	if (station.value.currentSong)
-		mediaSources.add(station.value.currentSong.mediaSource);
-
-	// Exclude songs in the queue
-	if (songsList.value) {
-		songsList.value.forEach(song => {
-			mediaSources.add(song.mediaSource);
-		});
-	}
-
-	// If auto skip disliked preference is enabled, exclude all songs in the disliked playlist
-	if (autoSkipDisliked.value && dislikedPlaylist.value) {
-		dislikedPlaylist.value.songs.forEach(song => {
-			mediaSources.add(song.mediaSource);
-		});
-	}
-
-	// If no history exists, just stop here
-	if (!history.value) Array.from(mediaSources);
-
-	const {
-		autorequestDisallowRecentlyPlayedEnabled,
-		autorequestDisallowRecentlyPlayedNumber
-	} = station.value.requests;
-
-	// If the station is set to disallow recently played songs, and station history is enabled, exclude the last X history songs
-	if (
-		autorequestDisallowRecentlyPlayedEnabled &&
-		experimental.value.station_history
-	) {
-		history.value.forEach((historyItem, index) => {
-			if (index < autorequestDisallowRecentlyPlayedNumber)
-				mediaSources.add(historyItem.payload.song.mediaSource);
-		});
-	}
-
-	return Array.from(mediaSources);
-});
-
 const totalUniqueAutorequestableMediaSources = computed<string[]>(() => {
 	if (!autoRequest.value) return [];
 
@@ -167,7 +119,7 @@ const totalUniqueAutorequestableMediaSources = computed<string[]>(() => {
 });
 
 const actuallyAutorequestingMediaSources = computed(() => {
-	const excluded = excludedMediaSources.value;
+	const excluded = autorequestExcludedMediaSources.value;
 	const remaining = totalUniqueAutorequestableMediaSources.value.filter(
 		mediaSource => {
 			if (excluded.indexOf(mediaSource) !== -1) return false;
@@ -880,6 +832,10 @@ onMounted(() => {
 					autorequested. Spotify
 					<span v-if="!experimental.soundcloud">and SoundCloud</span>
 					songs will also not be autorequested.
+					<span v-if="autoSkipDisliked"
+						>Disliked songs will also not be autorequested due to
+						your preferences.</span
+					>
 
 					<br />
 					<br />

+ 9 - 39
frontend/src/pages/Station/index.vue

@@ -142,7 +142,7 @@ const {
 	noSong,
 	autoRequest,
 	autoRequestLock,
-	history
+	autorequestExcludedMediaSources
 } = storeToRefs(stationStore);
 
 const youtubePlayerState = ref<
@@ -261,16 +261,6 @@ const {
 // const stopVideo = payload =>
 // 	store.dispatch("modals/editSong/stopVideo", payload);
 
-const recentlyPlayedYoutubeIds = (max: number) => {
-	const mediaSources = new Set();
-
-	history.value.forEach((historyItem, index) => {
-		if (index < max) mediaSources.add(historyItem.payload.song.mediaSource);
-	});
-
-	return Array.from(mediaSources);
-};
-
 const updateMediaSessionData = song => {
 	if (song) {
 		ms.setMediaSessionData(
@@ -285,13 +275,8 @@ const updateMediaSessionData = song => {
 	} else ms.removeMediaSessionData(0);
 };
 const autoRequestSong = () => {
-	const {
-		limit,
-		allowAutorequest,
-		autorequestLimit,
-		autorequestDisallowRecentlyPlayedEnabled,
-		autorequestDisallowRecentlyPlayedNumber
-	} = station.value.requests;
+	const { limit, allowAutorequest, autorequestLimit } =
+		station.value.requests;
 
 	if (autoRequestLock.value) return;
 	if (!allowAutorequest) return;
@@ -303,31 +288,16 @@ const autoRequestSong = () => {
 	if (currentUserQueueSongs.value >= autorequestLimit) return;
 	if (songsList.value.length >= 50) return;
 
-	let excludedYoutubeIds = [];
-	if (
-		autorequestDisallowRecentlyPlayedEnabled &&
-		experimental.value.station_history
-	) {
-		excludedYoutubeIds = recentlyPlayedYoutubeIds(
-			autorequestDisallowRecentlyPlayedNumber
-		);
-	}
-
-	if (songsList.value) {
-		songsList.value.forEach(song => {
-			excludedYoutubeIds.push(song.mediaSource);
-		});
-	}
-
-	if (!noSong.value) {
-		excludedYoutubeIds.push(currentSong.value.mediaSource);
-	}
-
 	const uniqueMediaSources = new Set();
 
 	autoRequest.value.forEach(playlist => {
 		playlist.songs.forEach(song => {
-			if (excludedYoutubeIds.indexOf(song.mediaSource) !== -1) return;
+			if (
+				autorequestExcludedMediaSources.value.indexOf(
+					song.mediaSource
+				) !== -1
+			)
+				return;
 			if (song.mediaSource.startsWith("spotify:")) return;
 			if (
 				!experimental.value.soundcloud &&

+ 62 - 1
frontend/src/stores/station.ts

@@ -1,10 +1,21 @@
-import { defineStore } from "pinia";
+import { defineStore, storeToRefs } from "pinia";
 import { Playlist } from "@/types/playlist";
 import { Song, CurrentSong } from "@/types/song";
 import { Station } from "@/types/station";
 import { User } from "@/types/user";
 import { StationHistory } from "@/types/stationHistory";
 import { useWebsocketsStore } from "@/stores/websockets";
+import { useUserPreferencesStore } from "@/stores/userPreferences";
+import { useConfigStore } from "@/stores/config";
+import { useSortablePlaylists } from "@/composables/useSortablePlaylists";
+
+const userPreferencesStore = useUserPreferencesStore();
+const configStore = useConfigStore();
+
+const { autoSkipDisliked } = storeToRefs(userPreferencesStore);
+const { experimental } = storeToRefs(configStore);
+
+const { playlists } = useSortablePlaylists();
 
 export const useStationStore = defineStore("station", {
 	state: (): {
@@ -48,6 +59,56 @@ export const useStationStore = defineStore("station", {
 		permissions: {},
 		history: []
 	}),
+	getters: {
+		dislikedPlaylist() {
+			return playlists.value.find(
+				playlist => playlist.type === "user-disliked"
+			);
+		},
+		// List of media sources that will not be allowed to be autorequested
+		autorequestExcludedMediaSources() {
+			const mediaSources = new Set();
+
+			// Exclude the current song
+			if (this.currentSong && this.currentSong.mediaSource)
+				mediaSources.add(this.currentSong.mediaSource);
+
+			// Exclude songs in the queue
+			if (this.songsList) {
+				this.songsList.forEach(song => {
+					mediaSources.add(song.mediaSource);
+				});
+			}
+
+			// If auto skip disliked preference is enabled, exclude all songs in the disliked playlist
+			if (autoSkipDisliked.value && this.dislikedPlaylist) {
+				this.dislikedPlaylist.songs.forEach(song => {
+					mediaSources.add(song.mediaSource);
+				});
+			}
+
+			// If no history exists, just stop here
+			if (!this.history) Array.from(mediaSources);
+
+			const {
+				autorequestDisallowRecentlyPlayedEnabled,
+				autorequestDisallowRecentlyPlayedNumber
+			} = this.station.requests;
+
+			// If the station is set to disallow recently played songs, and station history is enabled, exclude the last X history songs
+			if (
+				autorequestDisallowRecentlyPlayedEnabled &&
+				experimental.value.station_history
+			) {
+				this.history.forEach((historyItem, index) => {
+					if (index < autorequestDisallowRecentlyPlayedNumber)
+						mediaSources.add(historyItem.payload.song.mediaSource);
+				});
+			}
+
+			return Array.from(mediaSources);
+		}
+	},
 	actions: {
 		joinStation(station) {
 			this.station = { ...station };