diff --git a/src/renderer/components/data-settings/data-settings.js b/src/renderer/components/data-settings/data-settings.js index 7c950732ebb5e..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) } }) @@ -901,48 +903,63 @@ 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) - } - }) - // Update playlist's `lastUpdatedAt` - this.updatePlaylist({ _id: existingPlaylist._id }) + 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 (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 { - this.addPlaylist(playlistObject) + // Find by `playlistItemId` if present + videoExists = existingPlaylist.videos.some((x) => { + // Allow duplicate (by videoId) videos to be added + return x.videoId === video.videoId + }) } - } 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'))