@@ -875,6 +875,7 @@ export default defineComponent({
875
875
// to the app, so we'll only grab the data we need here.
876
876
877
877
const playlistObject = { }
878
+ const videoIdToBeAddedSet = new Set ( )
878
879
879
880
Object . keys ( playlistData ) . forEach ( ( key ) => {
880
881
if ( [ requiredKeys , optionalKeys , ignoredKeys ] . every ( ( ks ) => ! ks . includes ( key ) ) ) {
@@ -888,6 +889,7 @@ export default defineComponent({
888
889
889
890
if ( videoObjectHasAllRequiredKeys ) {
890
891
videoArray . push ( video )
892
+ videoIdToBeAddedSet . add ( video . videoId )
891
893
}
892
894
} )
893
895
@@ -901,48 +903,63 @@ export default defineComponent({
901
903
const playlistObjectKeys = Object . keys ( playlistObject )
902
904
const playlistObjectHasAllRequiredKeys = requiredKeys . every ( ( k ) => playlistObjectKeys . includes ( k ) )
903
905
904
- if ( playlistObjectHasAllRequiredKeys ) {
905
- const existingPlaylist = this . allPlaylists . find ( ( playlist ) => {
906
- return playlist . playlistName === playlistObject . playlistName
907
- } )
906
+ if ( ! playlistObjectHasAllRequiredKeys ) {
907
+ const message = this . $t ( 'Settings.Data Settings.Playlist insufficient data' , { playlist : playlistData . playlistName } )
908
+ showToast ( message )
909
+ return
910
+ }
908
911
909
- if ( existingPlaylist !== undefined ) {
910
- playlistObject . videos . forEach ( ( video ) => {
911
- let videoExists = false
912
- if ( video . playlistItemId != null ) {
913
- // Find by `playlistItemId` if present
914
- videoExists = existingPlaylist . videos . some ( ( x ) => {
915
- // Allow duplicate (by videoId) videos to be added
916
- return x . videoId === video . videoId && x . playlistItemId === video . playlistItemId
917
- } )
918
- } else {
919
- // Older playlist exports have no `playlistItemId` but have `timeAdded`
920
- // Which might be duplicate for copied playlists with duplicate `videoId`
921
- videoExists = existingPlaylist . videos . some ( ( x ) => {
922
- // Allow duplicate (by videoId) videos to be added
923
- return x . videoId === video . videoId && x . timeAdded === video . timeAdded
924
- } )
925
- }
912
+ const existingPlaylist = this . allPlaylists . find ( ( playlist ) => {
913
+ return playlist . playlistName === playlistObject . playlistName
914
+ } )
926
915
927
- if ( ! videoExists ) {
928
- // Keep original `timeAdded` value
929
- const payload = {
930
- _id : existingPlaylist . _id ,
931
- videoData : video ,
932
- }
916
+ if ( existingPlaylist === undefined ) {
917
+ this . addPlaylist ( playlistObject )
918
+ return
919
+ }
933
920
934
- this . addVideo ( payload )
935
- }
936
- } )
937
- // Update playlist's `lastUpdatedAt`
938
- this . updatePlaylist ( { _id : existingPlaylist . _id } )
921
+ const duplicateVideoPresentInToBeAdded = playlistObject . videos . length > videoIdToBeAddedSet . size
922
+ const existingVideoIdSet = existingPlaylist . videos . reduce ( ( video ) => videoIdToBeAddedSet . add ( video . videoId ) , new Set ( ) )
923
+ const duplicateVideoPresentInExistingPlaylist = existingPlaylist . videos . length > existingVideoIdSet . size
924
+ const shouldAddDuplicateVideos = duplicateVideoPresentInToBeAdded || duplicateVideoPresentInExistingPlaylist
925
+
926
+ playlistObject . videos . forEach ( ( video ) => {
927
+ let videoExists = false
928
+ if ( shouldAddDuplicateVideos ) {
929
+ if ( video . playlistItemId != null ) {
930
+ // Find by `playlistItemId` if present
931
+ videoExists = existingPlaylist . videos . some ( ( x ) => {
932
+ // Allow duplicate (by videoId) videos to be added
933
+ return x . videoId === video . videoId && x . playlistItemId === video . playlistItemId
934
+ } )
935
+ } else {
936
+ // Older playlist exports have no `playlistItemId` but have `timeAdded`
937
+ // Which might be duplicate for copied playlists with duplicate `videoId`
938
+ videoExists = existingPlaylist . videos . some ( ( x ) => {
939
+ // Allow duplicate (by videoId) videos to be added
940
+ return x . videoId === video . videoId && x . timeAdded === video . timeAdded
941
+ } )
942
+ }
939
943
} else {
940
- this . addPlaylist ( playlistObject )
944
+ // Find by `playlistItemId` if present
945
+ videoExists = existingPlaylist . videos . some ( ( x ) => {
946
+ // Allow duplicate (by videoId) videos to be added
947
+ return x . videoId === video . videoId
948
+ } )
941
949
}
942
- } else {
943
- const message = this . $t ( 'Settings.Data Settings.Playlist insufficient data' , { playlist : playlistData . playlistName } )
944
- showToast ( message )
945
- }
950
+
951
+ if ( ! videoExists ) {
952
+ // Keep original `timeAdded` value
953
+ const payload = {
954
+ _id : existingPlaylist . _id ,
955
+ videoData : video ,
956
+ }
957
+
958
+ this . addVideo ( payload )
959
+ }
960
+ } )
961
+ // Update playlist's `lastUpdatedAt`
962
+ this . updatePlaylist ( { _id : existingPlaylist . _id } )
946
963
} )
947
964
948
965
showToast ( this . $t ( 'Settings.Data Settings.All playlists has been successfully imported' ) )
0 commit comments