Skip to content

Commit

Permalink
Merge branch 'feature/playlist-smart-merge' into custom-builds/playli…
Browse files Browse the repository at this point in the history
…st-smart-merge

* feature/playlist-smart-merge:
  * Update playlist import to only add duplicate playlist items sometimes
  $ Use early return within `#forEach`
  • Loading branch information
PikachuEXE committed Oct 5, 2024
2 parents 0dc3e39 + f3c6291 commit 0b00550
Showing 1 changed file with 54 additions and 37 deletions.
91 changes: 54 additions & 37 deletions src/renderer/components/data-settings/data-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -888,6 +889,7 @@ export default defineComponent({

if (videoObjectHasAllRequiredKeys) {
videoArray.push(video)
videoIdToBeAddedSet.add(video.videoId)
}
})

Expand All @@ -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'))
Expand Down

0 comments on commit 0b00550

Please sign in to comment.