| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Server.Migrations.Stages; |
| | | 7 | | using Jellyfin.Server.ServerSetupApp; |
| | | 8 | | using MediaBrowser.Controller.Entities; |
| | | 9 | | using MediaBrowser.Controller.Library; |
| | | 10 | | using MediaBrowser.Controller.Providers; |
| | | 11 | | using MediaBrowser.Model.Configuration; |
| | | 12 | | using MediaBrowser.Model.Entities; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Enables the local similarity providers on libraries that predate the similar items settings. |
| | | 19 | | /// </summary> |
| | | 20 | | [JellyfinMigration("2026-08-31T10:00:00", nameof(EnableLocalSimilarityProviders), Stage = JellyfinMigrationStageTypes.Ap |
| | | 21 | | internal class EnableLocalSimilarityProviders : IAsyncMigrationRoutine |
| | | 22 | | { |
| | | 23 | | private readonly ILibraryManager _libraryManager; |
| | | 24 | | private readonly IProviderManager _providerManager; |
| | | 25 | | private readonly ILogger _logger; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="EnableLocalSimilarityProviders"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="libraryManager">The library manager.</param> |
| | | 31 | | /// <param name="providerManager">The provider manager.</param> |
| | | 32 | | /// <param name="startupLogger">The startup logger for Startup UI integration.</param> |
| | | 33 | | /// <param name="logger">The logger.</param> |
| | | 34 | | public EnableLocalSimilarityProviders( |
| | | 35 | | ILibraryManager libraryManager, |
| | | 36 | | IProviderManager providerManager, |
| | | 37 | | IStartupLogger<EnableLocalSimilarityProviders> startupLogger, |
| | | 38 | | ILogger<EnableLocalSimilarityProviders> logger) |
| | | 39 | | { |
| | 0 | 40 | | _libraryManager = libraryManager; |
| | 0 | 41 | | _providerManager = providerManager; |
| | 0 | 42 | | _logger = startupLogger.With(logger); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task PerformAsync(CancellationToken cancellationToken) |
| | | 47 | | { |
| | | 48 | | // Libraries created before similar items became configurable have an empty provider list, |
| | | 49 | | // which the library editor renders as "everything unchecked" instead of falling back to the |
| | | 50 | | // defaults it uses for new libraries. Seed the local providers so they stay enabled. |
| | 0 | 51 | | var localProvidersByType = GetLocalProvidersByItemType(); |
| | 0 | 52 | | if (localProvidersByType.Count == 0) |
| | | 53 | | { |
| | 0 | 54 | | return Task.CompletedTask; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | foreach (var virtualFolder in _libraryManager.GetVirtualFolders(false)) |
| | | 58 | | { |
| | 0 | 59 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 60 | | |
| | 0 | 61 | | EnableLocalProviders(virtualFolder, localProvidersByType); |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | return Task.CompletedTask; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private void EnableLocalProviders(VirtualFolderInfo virtualFolder, Dictionary<string, string[]> localProvidersByType |
| | | 68 | | { |
| | 0 | 69 | | var options = virtualFolder.LibraryOptions; |
| | 0 | 70 | | if (options?.TypeOptions is null || options.TypeOptions.Length == 0) |
| | | 71 | | { |
| | 0 | 72 | | return; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Some virtual folders don't have a proper item id. |
| | 0 | 76 | | if (!Guid.TryParse(virtualFolder.ItemId, out var folderId)) |
| | | 77 | | { |
| | 0 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | var collectionFolder = _libraryManager.GetItemById<CollectionFolder>(folderId); |
| | 0 | 82 | | if (collectionFolder is null) |
| | | 83 | | { |
| | 0 | 84 | | _logger.LogWarning("Could not find collection folder for virtual folder '{LibraryName}' with id '{FolderId}' |
| | 0 | 85 | | return; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | var changed = false; |
| | 0 | 89 | | foreach (var typeOptions in options.TypeOptions) |
| | | 90 | | { |
| | 0 | 91 | | changed |= EnableLocalProviders(typeOptions, localProvidersByType, virtualFolder.Name); |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (changed) |
| | | 95 | | { |
| | 0 | 96 | | collectionFolder.UpdateLibraryOptions(options); |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | private bool EnableLocalProviders(TypeOptions typeOptions, Dictionary<string, string[]> localProvidersByType, string |
| | | 101 | | { |
| | 0 | 102 | | if (typeOptions.Type is null || !localProvidersByType.TryGetValue(typeOptions.Type, out var localProviders)) |
| | | 103 | | { |
| | 0 | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | var enabled = typeOptions.SimilarItemProviders ?? []; |
| | 0 | 108 | | var missing = localProviders.Where(name => !enabled.Contains(name, StringComparer.OrdinalIgnoreCase)).ToArray(); |
| | 0 | 109 | | if (missing.Length == 0) |
| | | 110 | | { |
| | 0 | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | // Local providers rank ahead of remote ones, and the enabled list doubles as the |
| | | 115 | | // priority order when no explicit order was saved. |
| | 0 | 116 | | typeOptions.SimilarItemProviders = [.. missing, .. enabled]; |
| | 0 | 117 | | if (typeOptions.SimilarItemProviderOrder is { Length: > 0 } order) |
| | | 118 | | { |
| | 0 | 119 | | typeOptions.SimilarItemProviderOrder = [.. missing, .. order]; |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | _logger.LogInformation("Enabled local similarity providers {Providers} for '{ItemType}' in library '{LibraryName |
| | 0 | 123 | | return true; |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private Dictionary<string, string[]> GetLocalProvidersByItemType() |
| | | 127 | | { |
| | 0 | 128 | | var result = new Dictionary<string, string[]>(StringComparer.OrdinalIgnoreCase); |
| | | 129 | | |
| | 0 | 130 | | foreach (var summary in _providerManager.GetAllMetadataPlugins()) |
| | | 131 | | { |
| | 0 | 132 | | var names = summary.Plugins |
| | 0 | 133 | | .Where(p => p.Type == MetadataPluginType.LocalSimilarityProvider) |
| | 0 | 134 | | .Select(p => p.Name) |
| | 0 | 135 | | .Distinct(StringComparer.OrdinalIgnoreCase) |
| | 0 | 136 | | .ToArray(); |
| | | 137 | | |
| | 0 | 138 | | if (names.Length > 0) |
| | | 139 | | { |
| | 0 | 140 | | result[summary.ItemType] = names; |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |
| | 0 | 144 | | return result; |
| | | 145 | | } |
| | | 146 | | } |