From 26684b48d4ef97c51e0c5b55cabe255e07858fe6 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 2 Oct 2024 10:34:54 +0800 Subject: [PATCH 1/2] $ Use early return within `#forEach` --- .../components/data-settings/data-settings.js | 74 ++++++++++--------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/src/renderer/components/data-settings/data-settings.js b/src/renderer/components/data-settings/data-settings.js index 7c950732ebb5e..2bb5ab4c041a1 100644 --- a/src/renderer/components/data-settings/data-settings.js +++ b/src/renderer/components/data-settings/data-settings.js @@ -901,48 +901,50 @@ export default defineComponent({ const playlistObjectKeys = Object.keys(playlistObject) const playlistObjectHasAllRequiredKeys = requiredKeys.every((k) => playlistObjectKeys.includes(k)) - if (playlistObjectHasAllRequiredKeys) { - const existingPlaylist = this.allPlaylists.find((playlist) => { - return playlist.playlistName === playlistObject.playlistName - }) + if (!playlistObjectHasAllRequiredKeys) { + const message = this.$t('Settings.Data Settings.Playlist insufficient data', { playlist: playlistData.playlistName }) + showToast(message) + return + } - if (existingPlaylist !== undefined) { - playlistObject.videos.forEach((video) => { - let videoExists = false - if (video.playlistItemId != null) { - // Find by `playlistItemId` if present - videoExists = existingPlaylist.videos.some((x) => { - // Allow duplicate (by videoId) videos to be added - return x.videoId === video.videoId && x.playlistItemId === video.playlistItemId - }) - } else { - // Older playlist exports have no `playlistItemId` but have `timeAdded` - // Which might be duplicate for copied playlists with duplicate `videoId` - videoExists = existingPlaylist.videos.some((x) => { - // Allow duplicate (by videoId) videos to be added - return x.videoId === video.videoId && x.timeAdded === video.timeAdded - }) - } + const existingPlaylist = this.allPlaylists.find((playlist) => { + return playlist.playlistName === playlistObject.playlistName + }) - if (!videoExists) { - // Keep original `timeAdded` value - const payload = { - _id: existingPlaylist._id, - videoData: video, - } + if (existingPlaylist === undefined) { + this.addPlaylist(playlistObject) + return + } - this.addVideo(payload) - } + playlistObject.videos.forEach((video) => { + let videoExists = false + if (video.playlistItemId != null) { + // Find by `playlistItemId` if present + videoExists = existingPlaylist.videos.some((x) => { + // Allow duplicate (by videoId) videos to be added + return x.videoId === video.videoId && x.playlistItemId === video.playlistItemId }) - // Update playlist's `lastUpdatedAt` - this.updatePlaylist({ _id: existingPlaylist._id }) } else { - this.addPlaylist(playlistObject) + // Older playlist exports have no `playlistItemId` but have `timeAdded` + // Which might be duplicate for copied playlists with duplicate `videoId` + videoExists = existingPlaylist.videos.some((x) => { + // Allow duplicate (by videoId) videos to be added + return x.videoId === video.videoId && x.timeAdded === video.timeAdded + }) } - } else { - const message = this.$t('Settings.Data Settings.Playlist insufficient data', { playlist: playlistData.playlistName }) - showToast(message) - } + + if (!videoExists) { + // Keep original `timeAdded` value + const payload = { + _id: existingPlaylist._id, + videoData: video, + } + + this.addVideo(payload) + } + }) + // Update playlist's `lastUpdatedAt` + this.updatePlaylist({ _id: existingPlaylist._id }) }) showToast(this.$t('Settings.Data Settings.All playlists has been successfully imported')) From f3c62910f579ba3c9b0b49addcae3df5c03eb40f Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 2 Oct 2024 10:45:29 +0800 Subject: [PATCH 2/2] * Update playlist import to only add duplicate playlist items sometimes Either when existing playlist or incoming playlist has duplicate items --- .../components/data-settings/data-settings.js | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/renderer/components/data-settings/data-settings.js b/src/renderer/components/data-settings/data-settings.js index 2bb5ab4c041a1..d303a7c6c2a15 100644 --- a/src/renderer/components/data-settings/data-settings.js +++ b/src/renderer/components/data-settings/data-settings.js @@ -875,6 +875,7 @@ export default defineComponent({ // to the app, so we'll only grab the data we need here. const playlistObject = {} + const videoIdToBeAddedSet = new Set() Object.keys(playlistData).forEach((key) => { if ([requiredKeys, optionalKeys, ignoredKeys].every((ks) => !ks.includes(key))) { @@ -888,6 +889,7 @@ export default defineComponent({ if (videoObjectHasAllRequiredKeys) { videoArray.push(video) + videoIdToBeAddedSet.add(video.videoId) } }) @@ -916,20 +918,33 @@ export default defineComponent({ return } + const duplicateVideoPresentInToBeAdded = playlistObject.videos.length > videoIdToBeAddedSet.size + const existingVideoIdSet = existingPlaylist.videos.reduce((video) => videoIdToBeAddedSet.add(video.videoId), new Set()) + const duplicateVideoPresentInExistingPlaylist = existingPlaylist.videos.length > existingVideoIdSet.size + const shouldAddDuplicateVideos = duplicateVideoPresentInToBeAdded || duplicateVideoPresentInExistingPlaylist + playlistObject.videos.forEach((video) => { let videoExists = false - if (video.playlistItemId != null) { - // Find by `playlistItemId` if present - videoExists = existingPlaylist.videos.some((x) => { - // Allow duplicate (by videoId) videos to be added - return x.videoId === video.videoId && x.playlistItemId === video.playlistItemId - }) + if (shouldAddDuplicateVideos) { + if (video.playlistItemId != null) { + // Find by `playlistItemId` if present + videoExists = existingPlaylist.videos.some((x) => { + // Allow duplicate (by videoId) videos to be added + return x.videoId === video.videoId && x.playlistItemId === video.playlistItemId + }) + } else { + // Older playlist exports have no `playlistItemId` but have `timeAdded` + // Which might be duplicate for copied playlists with duplicate `videoId` + videoExists = existingPlaylist.videos.some((x) => { + // Allow duplicate (by videoId) videos to be added + return x.videoId === video.videoId && x.timeAdded === video.timeAdded + }) + } } else { - // Older playlist exports have no `playlistItemId` but have `timeAdded` - // Which might be duplicate for copied playlists with duplicate `videoId` + // Find by `playlistItemId` if present videoExists = existingPlaylist.videos.some((x) => { // Allow duplicate (by videoId) videos to be added - return x.videoId === video.videoId && x.timeAdded === video.timeAdded + return x.videoId === video.videoId }) }