From 8d158df67856bbb9de696f5caa0ec475b172ced2 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 10 Mar 2023 19:16:57 +0100 Subject: [PATCH] Add migration to properly set playlist owner --- .../Playlists/PlaylistManager.cs | 14 ++++ Jellyfin.Server/Migrations/MigrationRunner.cs | 3 +- .../Migrations/Routines/FixPlaylistOwner.cs | 67 +++++++++++++++++++ .../Playlists/IPlaylistManager.cs | 7 ++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 Jellyfin.Server/Migrations/Routines/FixPlaylistOwner.cs diff --git a/Emby.Server.Implementations/Playlists/PlaylistManager.cs b/Emby.Server.Implementations/Playlists/PlaylistManager.cs index 8cba652e7b..c15c2c4008 100644 --- a/Emby.Server.Implementations/Playlists/PlaylistManager.cs +++ b/Emby.Server.Implementations/Playlists/PlaylistManager.cs @@ -563,5 +563,19 @@ namespace Emby.Server.Implementations.Playlists } } } + + public async Task UpdatePlaylist(Playlist playlist) + { + var currentPlaylist = (Playlist)_libraryManager.GetItemById(playlist.Id); + currentPlaylist.OwnerUserId = playlist.OwnerUserId; + currentPlaylist.Shares = playlist.Shares; + + await playlist.UpdateToRepositoryAsync(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); + + if (playlist.IsFile) + { + SavePlaylistFile(currentPlaylist); + } + } } } diff --git a/Jellyfin.Server/Migrations/MigrationRunner.cs b/Jellyfin.Server/Migrations/MigrationRunner.cs index d4bf81f10b..866262d22f 100644 --- a/Jellyfin.Server/Migrations/MigrationRunner.cs +++ b/Jellyfin.Server/Migrations/MigrationRunner.cs @@ -40,7 +40,8 @@ namespace Jellyfin.Server.Migrations typeof(Routines.ReaddDefaultPluginRepository), typeof(Routines.MigrateDisplayPreferencesDb), typeof(Routines.RemoveDownloadImagesInAdvance), - typeof(Routines.MigrateAuthenticationDb) + typeof(Routines.MigrateAuthenticationDb), + typeof(Routines.FixPlaylistOwner) }; /// diff --git a/Jellyfin.Server/Migrations/Routines/FixPlaylistOwner.cs b/Jellyfin.Server/Migrations/Routines/FixPlaylistOwner.cs new file mode 100644 index 0000000000..d5736fd3c3 --- /dev/null +++ b/Jellyfin.Server/Migrations/Routines/FixPlaylistOwner.cs @@ -0,0 +1,67 @@ +using System; +using System.Linq; + +using Jellyfin.Data.Enums; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Playlists; +using Microsoft.Extensions.Logging; + +namespace Jellyfin.Server.Migrations.Routines; + +/// +/// Properly set playlist owner. +/// +internal class FixPlaylistOwner : IMigrationRoutine +{ + private readonly ILogger _logger; + private readonly ILibraryManager _libraryManager; + private readonly IPlaylistManager _playlistManager; + + public FixPlaylistOwner( + ILogger logger, + ILibraryManager libraryManager, + IPlaylistManager playlistManager) + { + _logger = logger; + _libraryManager = libraryManager; + _playlistManager = playlistManager; + } + + /// + public Guid Id => Guid.Parse("{615DFA9E-2497-4DBB-A472-61938B752C5B}"); + + /// + public string Name => "FixPlaylistOwner"; + + /// + public bool PerformOnNewInstall => false; + + /// + public void Perform() + { + var playlists = _libraryManager.GetItemList(new InternalItemsQuery + { + IncludeItemTypes = new[] { BaseItemKind.Playlist } + }) + .Cast() + .Where(x => x.OwnerUserId.Equals(Guid.Empty)) + .ToArray(); + + if (playlists.Length > 0) + { + foreach (var playlist in playlists) + { + var shares = playlist.Shares; + var firstEditShare = shares.First(x => x.CanEdit); + if (firstEditShare is not null && Guid.TryParse(firstEditShare.UserId, out var guid)) + { + playlist.OwnerUserId = guid; + playlist.Shares = shares.Where(x => x != firstEditShare).ToArray(); + + _playlistManager.UpdatePlaylist(playlist).GetAwaiter().GetResult(); + } + } + } + } +} diff --git a/MediaBrowser.Controller/Playlists/IPlaylistManager.cs b/MediaBrowser.Controller/Playlists/IPlaylistManager.cs index 201dadb3d2..22f974420f 100644 --- a/MediaBrowser.Controller/Playlists/IPlaylistManager.cs +++ b/MediaBrowser.Controller/Playlists/IPlaylistManager.cs @@ -64,5 +64,12 @@ namespace MediaBrowser.Controller.Playlists /// The user id. /// Task. Task RemovePlaylists(Guid userId); + + /// + /// Updates a playlist. + /// + /// The updated playlist. + /// Task. + Task UpdatePlaylist(Playlist playlist); } }