use map to de-duplicate playlist tracks

This commit is contained in:
Drew Weymouth
2024-05-23 17:29:32 -07:00
parent e2a9ea49e0
commit 7a12f58658
+6 -9
View File
@@ -11,7 +11,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"slices"
"time" "time"
"github.com/dweymouth/supersonic/backend" "github.com/dweymouth/supersonic/backend"
@@ -340,22 +339,20 @@ func (m *Controller) DoAddTracksToPlaylistWorkflow(trackIDs []string) {
} else { } else {
m.App.Config.Application.DefaultPlaylistID = id m.App.Config.Application.DefaultPlaylistID = id
if sp.SkipDuplicates { if sp.SkipDuplicates {
var filterTrackIDs []string
go func() { go func() {
currentTrackIDs := make(map[string]struct{})
if selectedPlaylist, err := m.App.ServerManager.Server.GetPlaylist(id); err != nil { if selectedPlaylist, err := m.App.ServerManager.Server.GetPlaylist(id); err != nil {
log.Printf("error getting playlist: %s", err.Error()) log.Printf("error getting playlist: %s", err.Error())
} else { } else {
var trackIDsInPaylist []string
for _, track := range selectedPlaylist.Tracks { for _, track := range selectedPlaylist.Tracks {
trackIDsInPaylist = append(trackIDsInPaylist, track.ID) currentTrackIDs[track.ID] = struct{}{}
} }
filterTrackIDs = sharedutil.FilterSlice(trackIDs, func(trackID string) bool { filterTrackIDs := sharedutil.FilterSlice(trackIDs, func(trackID string) bool {
return !slices.Contains(trackIDsInPaylist, trackID) _, ok := currentTrackIDs[trackID]
return !ok
}) })
}
m.App.ServerManager.Server.AddPlaylistTracks(id, filterTrackIDs) m.App.ServerManager.Server.AddPlaylistTracks(id, filterTrackIDs)
}
}() }()
} else { } else {
go m.App.ServerManager.Server.AddPlaylistTracks(id, trackIDs) go m.App.ServerManager.Server.AddPlaylistTracks(id, trackIDs)