| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Threading; |
| | 4 | |
|
| | 5 | | using Jellyfin.Data.Enums; |
| | 6 | | using MediaBrowser.Controller.Entities; |
| | 7 | | using MediaBrowser.Controller.Library; |
| | 8 | | using MediaBrowser.Controller.Playlists; |
| | 9 | | using Microsoft.Extensions.Logging; |
| | 10 | |
|
| | 11 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 12 | |
|
| | 13 | | /// <summary> |
| | 14 | | /// Properly set playlist owner. |
| | 15 | | /// </summary> |
| | 16 | | internal class FixPlaylistOwner : IMigrationRoutine |
| | 17 | | { |
| | 18 | | private readonly ILogger<FixPlaylistOwner> _logger; |
| | 19 | | private readonly ILibraryManager _libraryManager; |
| | 20 | | private readonly IPlaylistManager _playlistManager; |
| | 21 | |
|
| | 22 | | public FixPlaylistOwner( |
| | 23 | | ILogger<FixPlaylistOwner> logger, |
| | 24 | | ILibraryManager libraryManager, |
| | 25 | | IPlaylistManager playlistManager) |
| | 26 | | { |
| 0 | 27 | | _logger = logger; |
| 0 | 28 | | _libraryManager = libraryManager; |
| 0 | 29 | | _playlistManager = playlistManager; |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| 0 | 33 | | public Guid Id => Guid.Parse("{615DFA9E-2497-4DBB-A472-61938B752C5B}"); |
| | 34 | |
|
| | 35 | | /// <inheritdoc/> |
| 0 | 36 | | public string Name => "FixPlaylistOwner"; |
| | 37 | |
|
| | 38 | | /// <inheritdoc/> |
| 0 | 39 | | public bool PerformOnNewInstall => false; |
| | 40 | |
|
| | 41 | | /// <inheritdoc/> |
| | 42 | | public void Perform() |
| | 43 | | { |
| 0 | 44 | | var playlists = _libraryManager.GetItemList(new InternalItemsQuery |
| 0 | 45 | | { |
| 0 | 46 | | IncludeItemTypes = new[] { BaseItemKind.Playlist } |
| 0 | 47 | | }) |
| 0 | 48 | | .Cast<Playlist>() |
| 0 | 49 | | .Where(x => x.OwnerUserId.Equals(Guid.Empty)) |
| 0 | 50 | | .ToArray(); |
| | 51 | |
|
| 0 | 52 | | if (playlists.Length > 0) |
| | 53 | | { |
| 0 | 54 | | foreach (var playlist in playlists) |
| | 55 | | { |
| 0 | 56 | | var shares = playlist.Shares; |
| 0 | 57 | | if (shares.Count > 0) |
| | 58 | | { |
| 0 | 59 | | var firstEditShare = shares.First(x => x.CanEdit); |
| 0 | 60 | | if (firstEditShare is not null) |
| | 61 | | { |
| 0 | 62 | | playlist.OwnerUserId = firstEditShare.UserId; |
| 0 | 63 | | playlist.Shares = shares.Where(x => x != firstEditShare).ToArray(); |
| 0 | 64 | | playlist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).GetAwaiter |
| 0 | 65 | | _playlistManager.SavePlaylistFile(playlist); |
| | 66 | | } |
| | 67 | | } |
| | 68 | | else |
| | 69 | | { |
| 0 | 70 | | playlist.OpenAccess = true; |
| 0 | 71 | | playlist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).GetAwaiter().G |
| | 72 | | } |
| | 73 | | } |
| | 74 | | } |
| 0 | 75 | | } |
| | 76 | | } |