| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading; |
| | 4 | | using Jellyfin.Data.Enums; |
| | 5 | | using MediaBrowser.Controller.Entities; |
| | 6 | | using MediaBrowser.Controller.Library; |
| | 7 | | using MediaBrowser.Controller.Playlists; |
| | 8 | |
|
| | 9 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Remove duplicate playlist entries. |
| | 13 | | /// </summary> |
| | 14 | | [JellyfinMigration("2025-04-20T19:00:00", nameof(RemoveDuplicatePlaylistChildren), "96C156A2-7A13-4B3B-A8B8-FB80C94D20C0 |
| | 15 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 16 | | internal class RemoveDuplicatePlaylistChildren : IMigrationRoutine |
| | 17 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | 18 | | { |
| | 19 | | private readonly ILibraryManager _libraryManager; |
| | 20 | | private readonly IPlaylistManager _playlistManager; |
| | 21 | |
|
| | 22 | | public RemoveDuplicatePlaylistChildren( |
| | 23 | | ILibraryManager libraryManager, |
| | 24 | | IPlaylistManager playlistManager) |
| | 25 | | { |
| 0 | 26 | | _libraryManager = libraryManager; |
| 0 | 27 | | _playlistManager = playlistManager; |
| 0 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <inheritdoc/> |
| | 31 | | public void Perform() |
| | 32 | | { |
| 0 | 33 | | var playlists = _libraryManager.GetItemList(new InternalItemsQuery |
| 0 | 34 | | { |
| 0 | 35 | | IncludeItemTypes = [BaseItemKind.Playlist] |
| 0 | 36 | | }) |
| 0 | 37 | | .Cast<Playlist>() |
| 0 | 38 | | .Where(p => !p.OpenAccess || !p.OwnerUserId.Equals(Guid.Empty)) |
| 0 | 39 | | .ToArray(); |
| | 40 | |
|
| 0 | 41 | | if (playlists.Length > 0) |
| | 42 | | { |
| 0 | 43 | | foreach (var playlist in playlists) |
| | 44 | | { |
| 0 | 45 | | var linkedChildren = playlist.LinkedChildren; |
| 0 | 46 | | if (linkedChildren.Length > 0) |
| | 47 | | { |
| 0 | 48 | | var nullItemChildren = linkedChildren.Where(c => c.ItemId is null); |
| 0 | 49 | | var deduplicatedChildren = linkedChildren.DistinctBy(c => c.ItemId); |
| 0 | 50 | | var newLinkedChildren = nullItemChildren.Concat(deduplicatedChildren); |
| 0 | 51 | | playlist.LinkedChildren = linkedChildren; |
| 0 | 52 | | playlist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).GetAwaiter().G |
| 0 | 53 | | _playlistManager.SavePlaylistFile(playlist); |
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| 0 | 57 | | } |
| | 58 | | } |