diff options
| -rw-r--r-- | CONTRIBUTORS.md | 2 | ||||
| -rw-r--r-- | Emby.Server.Implementations/Playlists/PlaylistManager.cs | 44 |
2 files changed, 33 insertions, 13 deletions
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4f3624965..d0e69369a 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -80,6 +80,8 @@ - [pjeanjean](https://github.com/pjeanjean) - [ploughpuff](https://github.com/ploughpuff) - [pR0Ps](https://github.com/pR0Ps) + - [artiume](https://github.com/Artiume) + - [SegiH](https://github.com/SegiH) - [PrplHaz4](https://github.com/PrplHaz4) - [RazeLighter777](https://github.com/RazeLighter777) - [redSpoutnik](https://github.com/redSpoutnik) diff --git a/Emby.Server.Implementations/Playlists/PlaylistManager.cs b/Emby.Server.Implementations/Playlists/PlaylistManager.cs index ad7a5005e..0864169ce 100644 --- a/Emby.Server.Implementations/Playlists/PlaylistManager.cs +++ b/Emby.Server.Implementations/Playlists/PlaylistManager.cs @@ -189,35 +189,53 @@ namespace Emby.Server.Implementations.Playlists 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) .Where(i => i.SupportsAddingToPlaylist) .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()) + .Select(i => LinkedChild.Create(i)) + .ToList(); + + // Log duplicates that have been ignored, if any + int numDuplicates = items.Count - uniqueItems.Count; + if (numDuplicates > 0) { - list.Add(LinkedChild.Create(item)); + _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(); + // Do nothing else if there are no items to add to the playlist + if (uniqueItems.Count == 0) + { + return; + } + + // Create a new array with the updated playlist items + var newLinkedChildren = new LinkedChild[playlist.LinkedChildren.Length + uniqueItems.Count]; + playlist.LinkedChildren.CopyTo(newLinkedChildren, 0); + uniqueItems.CopyTo(newLinkedChildren, playlist.LinkedChildren.Length); + // Update the playlist in the repository + playlist.LinkedChildren = newLinkedChildren; playlist.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None); + // Update the playlist on disk if (playlist.IsFile) { SavePlaylistFile(playlist); } + // Refresh playlist metadata _providerManager.QueueRefresh( playlist.Id, new MetadataRefreshOptions(new DirectoryService(_fileSystem)) |
