Kaynağa Gözat

Check for duplicates when adding items to a playlist

Mark Monteiro 5 yıl önce
ebeveyn
işleme
dde0277050

+ 22 - 18
Emby.Server.Implementations/Playlists/PlaylistManager.cs

@@ -189,41 +189,45 @@ namespace Emby.Server.Implementations.Playlists
 
 
         private void AddToPlaylistInternal(string playlistId, IEnumerable<Guid> itemIds, User user, DtoOptions options)
         private void AddToPlaylistInternal(string playlistId, IEnumerable<Guid> itemIds, User user, DtoOptions options)
         {
         {
-            var playlist = _libraryManager.GetItemById(playlistId) as Playlist;
-
-            if (playlist == null)
-            {
-                throw new ArgumentException("No Playlist exists with the supplied Id");
-            }
-
-            var list = new List<LinkedChild>();
+            // Retrieve the existing playlist
+            var playlist = _libraryManager.GetItemById(playlistId) as Playlist
+                ?? throw new ArgumentException("No Playlist exists with Id " + playlistId);
 
 
+            // Retrieve all the items to be added to the playlist
             var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options)
             var items = GetPlaylistItems(itemIds, playlist.MediaType, user, options)
                 .Where(i => i.SupportsAddingToPlaylist)
                 .Where(i => i.SupportsAddingToPlaylist)
                 .ToList();
                 .ToList();
 
 
-            foreach (var item in items)
+            // Remove duplicates from the new items to be added
+            var existingIds = playlist.LinkedChildren.Select(c => c.ItemId).ToHashSet();
+            var uniqueItems = items
+                .Where(i => !existingIds.Contains(i.Id))
+                .GroupBy(i => i.Id)
+                .Select(group => group.First())
+                .ToList();
+
+            // Log duplicates that have been ignored, if any
+            if (uniqueItems.Count != items.Count)
             {
             {
-                list.Add(LinkedChild.Create(item));
+                var numDuplicates = items.Count - uniqueItems.Count;
+                _logger.LogWarning("Ignored adding {DuplicateCount} duplicate items to playlist {PlaylistName}.", numDuplicates, playlist.Name);
             }
             }
 
 
-            var newList = playlist.LinkedChildren.ToList();
-            newList.AddRange(list);
-            playlist.LinkedChildren = newList.ToArray();
-
+            // Update the playlist in the repository
+            var linkedItems = uniqueItems.Select(i => LinkedChild.Create(i));
+            playlist.LinkedChildren = playlist.LinkedChildren.Concat(linkedItems).ToArray();
             playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
             playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
 
 
+            // Update the playlist on disk
             if (playlist.IsFile)
             if (playlist.IsFile)
             {
             {
                 SavePlaylistFile(playlist);
                 SavePlaylistFile(playlist);
             }
             }
 
 
+            // Refresh playlist metadata
             _providerManager.QueueRefresh(
             _providerManager.QueueRefresh(
                 playlist.Id,
                 playlist.Id,
-                new MetadataRefreshOptions(new DirectoryService(_fileSystem))
-                {
-                    ForceSave = true
-                },
+                new MetadataRefreshOptions(new DirectoryService(_fileSystem)) { ForceSave = true },
                 RefreshPriority.High);
                 RefreshPriority.High);
         }
         }