mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 05:01:10 +00:00
Adds metadata and artwork provider configuration seeding
Seeds a disabled provider configuration for every loaded plugin supporting a library type, so that libraries list the metadata and artwork providers they can use. Configurations are seeded on startup, on library creation, and reconciled when the library type changes, and are removed only when a library or a plugin is deleted. Reads the library metadata providers through the new provider configuration store.
This commit is contained in:
1 parent
9ae8c0c394
commit
00dcf3d471
20 files changed
+1139
-27
No files matched your search
+26
@@ -39,4 +39,30 @@ public interface IArtworkProviderConfigurationRepository : IRepository<LibraryAr
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Updated>> UpsertAsync(LibraryArtworkProviderConfigurationEntity configuration, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the artwork provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the artwork provider configurations referencing the plugin identified by <paramref name="pluginId"/>.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The Id of the plugin whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByPluginIdAsync(Guid pluginId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the artwork provider configurations of the media library identified by <paramref name="libraryId"/> that reference
|
||||
/// one of the plugins identified by <paramref name="pluginIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="pluginIds">The Ids of the plugins whose configurations of the media library are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByLibraryIdAndPluginIdsAsync(Guid libraryId, IEnumerable<Guid> pluginIds, CancellationToken cancellationToken);
|
||||
}
|
||||
+26
@@ -39,4 +39,30 @@ public interface ILibraryMetadataProviderConfigurationRepository : IRepository<L
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Updated>> UpsertAsync(LibraryMetadataProviderConfigurationEntity configuration, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the metadata provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the metadata provider configurations referencing the plugin identified by <paramref name="pluginId"/>.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The Id of the plugin whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByPluginIdAsync(Guid pluginId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the metadata provider configurations of the media library identified by <paramref name="libraryId"/> that reference
|
||||
/// one of the plugins identified by <paramref name="pluginIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="pluginIds">The Ids of the plugins whose configurations of the media library are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> DeleteByLibraryIdAndPluginIdsAsync(Guid libraryId, IEnumerable<Guid> pluginIds, CancellationToken cancellationToken);
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Common.Infrastructure.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Store of the metadata and artwork provider configurations of the media libraries.
|
||||
/// </summary>
|
||||
public interface IMediaLibraryProviderConfigurationStore
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the metadata provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are retrieved.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> containing either the configurations, or an error.</returns>
|
||||
Task<Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>> GetConfigurationsAsync(Guid libraryId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a disabled provider configuration for every loaded plugin that supports the provided library type and has no
|
||||
/// configuration yet for the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose provider configurations are ensured.</param>
|
||||
/// <param name="libraryType">The type of the media library, used to determine the plugins whose providers apply.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Success>> EnsureProviderConfigurationsAsync(Guid libraryId, LibraryType libraryType, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the provider configurations of the media library identified by <paramref name="libraryId"/> so that they
|
||||
/// match the plugins supporting the provided library type.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose provider configurations are reconciled.</param>
|
||||
/// <param name="libraryType">The type of the media library, used to determine the plugins whose providers apply.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Success>> ReconcileProviderConfigurationsAsync(Guid libraryId, LibraryType libraryType, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> RemoveProviderConfigurationsForLibraryAsync(Guid libraryId, CancellationToken cancellationToken);
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the provider configurations referencing the plugin identified by <paramref name="pluginId"/>.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The Id of the plugin whose provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
Task<Result<Deleted>> RemoveProviderConfigurationsAsync(Guid pluginId, CancellationToken cancellationToken);
|
||||
}
|
||||
+12
-2
@@ -5,6 +5,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.DomainEvents;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.Management;
|
||||
@@ -37,6 +38,7 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
private readonly IDomainEventsQueue _domainEventsQueue;
|
||||
private readonly IEnvironmentContext _environmentContext;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IMediaLibraryProviderConfigurationStore _providerConfigurationStore;
|
||||
private readonly IValidator<AddLibraryCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
@@ -46,13 +48,15 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="domainEventsQueue">Injected service for the queue of domain events.</param>
|
||||
/// <param name="environmentContext">Injected facade service for environment contextual services.</param>
|
||||
/// <param name="providerConfigurationStore">Injected store of the provider configurations of the media libraries.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public AddLibraryCommandHandler(
|
||||
IAuthorizationService authorizationService,
|
||||
ICurrentUserService currentUserService,
|
||||
ICurrentUserService currentUserService,
|
||||
IDomainEventsQueue domainEventsQueue,
|
||||
IEnvironmentContext environmentContext,
|
||||
IMediaLibraryProviderConfigurationStore providerConfigurationStore,
|
||||
IUnitOfWork unitOfWork,
|
||||
IValidator<AddLibraryCommand> validator)
|
||||
{
|
||||
@@ -61,6 +65,7 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
_domainEventsQueue = domainEventsQueue;
|
||||
_environmentContext = environmentContext;
|
||||
_authorizationService = authorizationService;
|
||||
_providerConfigurationStore = providerConfigurationStore;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
@@ -123,10 +128,15 @@ public class AddLibraryCommandHandler : ICommandHandler<AddLibraryCommand, Resul
|
||||
|
||||
// convert the domain library entity to a repository library entity
|
||||
LibraryEntity persistenceLibrary = createLibraryResult.Value.ToRepositoryEntity();
|
||||
// insert the repository entity and save changes
|
||||
// insert the repository entity, seed the provider configurations of the new library so that it lists the plugins
|
||||
// providing metadata or artwork for its type, and save the changes
|
||||
Result<Created> insertLibraryResult = await _unitOfWork.LibraryRepository.InsertAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
|
||||
if (insertLibraryResult.IsFailure)
|
||||
return insertLibraryResult.Errors;
|
||||
Result<Success> ensureProviderConfigurationsResult = await _providerConfigurationStore.EnsureProviderConfigurationsAsync(
|
||||
createLibraryResult.Value.Id.Value, Enum.Parse<LibraryType>(command.LibraryType!), cancellationToken).ConfigureAwait(false);
|
||||
if (ensureProviderConfigurationsResult.IsFailure)
|
||||
return ensureProviderConfigurationsResult.Errors;
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// retrieve the newly saved media library from the persistence medium and return it
|
||||
|
||||
+9
-1
@@ -8,6 +8,7 @@ using Lumina.Application.Common.DomainEvents;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
|
||||
using Lumina.Domain.Common.Events;
|
||||
@@ -31,6 +32,7 @@ public class DeleteLibraryCommandHandler : ICommandHandler<DeleteLibraryCommand,
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IDomainEventsQueue _domainEventsQueue;
|
||||
private readonly IMediaLibraryProviderConfigurationStore _providerConfigurationStore;
|
||||
private readonly IValidator<DeleteLibraryCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
@@ -39,18 +41,21 @@ public class DeleteLibraryCommandHandler : ICommandHandler<DeleteLibraryCommand,
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="domainEventsQueue">Injected service for the queue of domain events.</param>
|
||||
/// <param name="providerConfigurationStore">Injected store of the provider configurations of the media libraries.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public DeleteLibraryCommandHandler(
|
||||
IAuthorizationService authorizationService,
|
||||
ICurrentUserService currentUserService,
|
||||
IDomainEventsQueue domainEventsQueue,
|
||||
IMediaLibraryProviderConfigurationStore providerConfigurationStore,
|
||||
IUnitOfWork unitOfWork,
|
||||
IValidator<DeleteLibraryCommand> validator)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_domainEventsQueue = domainEventsQueue;
|
||||
_providerConfigurationStore = providerConfigurationStore;
|
||||
_unitOfWork = unitOfWork;
|
||||
_validator = validator;
|
||||
}
|
||||
@@ -100,7 +105,10 @@ public class DeleteLibraryCommandHandler : ICommandHandler<DeleteLibraryCommand,
|
||||
foreach (IDomainEvent domainEvent in createLibraryResult.Value.GetDomainEvents())
|
||||
_domainEventsQueue.Enqueue(domainEvent);
|
||||
|
||||
// perform the deletion
|
||||
// remove the provider configurations of the library, so that they are not orphaned, and delete the library
|
||||
Result<Deleted> removeProviderConfigurationsResult = await _providerConfigurationStore.RemoveProviderConfigurationsForLibraryAsync(command.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (removeProviderConfigurationsResult.IsFailure)
|
||||
return removeProviderConfigurationsResult.Errors;
|
||||
Result<Deleted> deletePersistenceLibraryResult = await _unitOfWork.LibraryRepository.DeleteByIdAsync(command.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (deletePersistenceLibraryResult.IsFailure)
|
||||
return deletePersistenceLibraryResult.Errors;
|
||||
|
||||
+16
-3
@@ -6,6 +6,7 @@ using Lumina.Application.Common.DomainEvents;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
|
||||
using Lumina.Contracts.Responses.MediaLibrary.Management;
|
||||
@@ -41,6 +42,7 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IDomainEventsQueue _domainEventsQueue;
|
||||
private readonly IEnvironmentContext _environmentContext;
|
||||
private readonly IMediaLibraryProviderConfigurationStore _providerConfigurationStore;
|
||||
private readonly IValidator<UpdateLibraryCommand> _validator;
|
||||
|
||||
/// <summary>
|
||||
@@ -50,13 +52,15 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="domainEventsQueue">Injected service for the queue of domain events.</param>
|
||||
/// <param name="environmentContext">Injected facade service for environment contextual services.</param>
|
||||
/// <param name="providerConfigurationStore">Injected store of the provider configurations of the media libraries.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public UpdateLibraryCommandHandler(
|
||||
IAuthorizationService authorizationService,
|
||||
IAuthorizationService authorizationService,
|
||||
ICurrentUserService currentUserService,
|
||||
IDomainEventsQueue domainEventsQueue,
|
||||
IEnvironmentContext environmentContext,
|
||||
IMediaLibraryProviderConfigurationStore providerConfigurationStore,
|
||||
IUnitOfWork unitOfWork,
|
||||
IValidator<UpdateLibraryCommand> validator)
|
||||
{
|
||||
@@ -65,6 +69,7 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
_unitOfWork = unitOfWork;
|
||||
_domainEventsQueue = domainEventsQueue;
|
||||
_environmentContext = environmentContext;
|
||||
_providerConfigurationStore = providerConfigurationStore;
|
||||
_validator = validator;
|
||||
}
|
||||
|
||||
@@ -117,11 +122,12 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
// create a domain library object
|
||||
LibraryType newLibraryType = Enum.Parse<LibraryType>(command.LibraryType!);
|
||||
Result<Library> createLibraryResult = Library.Create(
|
||||
LibraryId.Create(command.Id),
|
||||
UserId.Create(command.OwnerId),
|
||||
command.Title!,
|
||||
Enum.Parse<LibraryType>(command.LibraryType!),
|
||||
newLibraryType,
|
||||
command.ContentLocations!,
|
||||
command.CoverImage,
|
||||
command.IsEnabled,
|
||||
@@ -136,10 +142,17 @@ public class UpdateLibraryCommandHandler : ICommandHandler<UpdateLibraryCommand,
|
||||
// convert the domain library entity to a repository library entity
|
||||
LibraryEntity persistenceLibrary = createLibraryResult.Value.ToRepositoryEntity();
|
||||
|
||||
// update the repository entity and save changes
|
||||
// update the repository entity, and when the library type changed, reconcile the provider configurations so that
|
||||
// they match the plugins supporting the new type, and save the changes
|
||||
Result<Updated> updateLibraryResult = await _unitOfWork.LibraryRepository.UpdateAsync(persistenceLibrary, cancellationToken).ConfigureAwait(false);
|
||||
if (updateLibraryResult.IsFailure)
|
||||
return updateLibraryResult.Errors;
|
||||
if (getLibraryResult.Value.LibraryType != newLibraryType)
|
||||
{
|
||||
Result<Success> reconcileResult = await _providerConfigurationStore.ReconcileProviderConfigurationsAsync(command.Id, newLibraryType, cancellationToken).ConfigureAwait(false);
|
||||
if (reconcileResult.IsFailure)
|
||||
return reconcileResult.Errors;
|
||||
}
|
||||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
// retrieve the updated media library from the persistence medium and return it
|
||||
|
||||
+6
-2
@@ -5,6 +5,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization;
|
||||
using Lumina.Application.Common.Infrastructure.Authorization.Policies.LibraryOwnership;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Application.Common.Infrastructure.Validation;
|
||||
using Lumina.Application.Common.Mapping.Plugins;
|
||||
using Lumina.Contracts.Responses.Plugins;
|
||||
@@ -27,6 +28,7 @@ public class GetLibraryMetadataProvidersQueryHandler : IQueryHandler<GetLibraryM
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
private readonly IMediaLibraryProviderConfigurationStore _providerConfigurationStore;
|
||||
private readonly IValidator<GetLibraryMetadataProvidersQuery> _validator;
|
||||
|
||||
/// <summary>
|
||||
@@ -34,12 +36,14 @@ public class GetLibraryMetadataProvidersQueryHandler : IQueryHandler<GetLibraryM
|
||||
/// </summary>
|
||||
/// <param name="authorizationService">Injected service for authorization related functionality.</param>
|
||||
/// <param name="currentUserService">Injected service to retrieve the current user information.</param>
|
||||
/// <param name="providerConfigurationStore">Injected store of the provider configurations of the media libraries.</param>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="validator">Injected validator for application validation rules.</param>
|
||||
public GetLibraryMetadataProvidersQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IUnitOfWork unitOfWork, IValidator<GetLibraryMetadataProvidersQuery> validator)
|
||||
public GetLibraryMetadataProvidersQueryHandler(IAuthorizationService authorizationService, ICurrentUserService currentUserService, IMediaLibraryProviderConfigurationStore providerConfigurationStore, IUnitOfWork unitOfWork, IValidator<GetLibraryMetadataProvidersQuery> validator)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_currentUserService = currentUserService;
|
||||
_providerConfigurationStore = providerConfigurationStore;
|
||||
_unitOfWork = unitOfWork;
|
||||
_validator = validator;
|
||||
}
|
||||
@@ -70,7 +74,7 @@ public class GetLibraryMetadataProvidersQueryHandler : IQueryHandler<GetLibraryM
|
||||
if (!canAccessLibrary)
|
||||
return ApplicationErrors.Authorization.NotAuthorized;
|
||||
|
||||
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _providerConfigurationStore.GetConfigurationsAsync(query.LibraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getConfigurationsResult.IsFailure)
|
||||
return getConfigurationsResult.Errors;
|
||||
|
||||
|
||||
+51
@@ -77,4 +77,55 @@ internal sealed class ArtworkProviderConfigurationRepository : IArtworkProviderC
|
||||
}
|
||||
return Result.Updated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the artwork provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
|
||||
{
|
||||
List<LibraryArtworkProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryArtworkProviderConfigurations
|
||||
.Where(configuration => configuration.LibraryId == libraryId)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryArtworkProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the artwork provider configurations referencing the plugin identified by <paramref name="pluginId"/>.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The Id of the plugin whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByPluginIdAsync(Guid pluginId, CancellationToken cancellationToken)
|
||||
{
|
||||
List<LibraryArtworkProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryArtworkProviderConfigurations
|
||||
.Where(configuration => configuration.PluginId == pluginId)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryArtworkProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the artwork provider configurations of the media library identified by <paramref name="libraryId"/> that reference
|
||||
/// one of the plugins identified by <paramref name="pluginIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are deleted.</param>
|
||||
/// <param name="pluginIds">The Ids of the plugins whose configurations of the media library are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByLibraryIdAndPluginIdsAsync(Guid libraryId, IEnumerable<Guid> pluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Guid> pluginIdList = [.. pluginIds];
|
||||
List<LibraryArtworkProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryArtworkProviderConfigurations
|
||||
.Where(configuration => configuration.LibraryId == libraryId && pluginIdList.Contains(configuration.PluginId))
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryArtworkProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
}
|
||||
+51
@@ -77,4 +77,55 @@ internal sealed class LibraryMetadataProviderConfigurationRepository : ILibraryM
|
||||
}
|
||||
return Result.Updated;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the metadata provider configurations of the media library identified by <paramref name="libraryId"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByLibraryIdAsync(Guid libraryId, CancellationToken cancellationToken)
|
||||
{
|
||||
List<LibraryMetadataProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryMetadataProviderConfigurations
|
||||
.Where(configuration => configuration.LibraryId == libraryId)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryMetadataProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes all the metadata provider configurations referencing the plugin identified by <paramref name="pluginId"/>.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The Id of the plugin whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByPluginIdAsync(Guid pluginId, CancellationToken cancellationToken)
|
||||
{
|
||||
List<LibraryMetadataProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryMetadataProviderConfigurations
|
||||
.Where(configuration => configuration.PluginId == pluginId)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryMetadataProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the metadata provider configurations of the media library identified by <paramref name="libraryId"/> that reference
|
||||
/// one of the plugins identified by <paramref name="pluginIds"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are deleted.</param>
|
||||
/// <param name="pluginIds">The Ids of the plugins whose configurations of the media library are deleted.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
public async Task<Result<Deleted>> DeleteByLibraryIdAndPluginIdsAsync(Guid libraryId, IEnumerable<Guid> pluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
List<Guid> pluginIdList = [.. pluginIds];
|
||||
List<LibraryMetadataProviderConfigurationEntity> configurations = await _luminaDbContext.LibraryMetadataProviderConfigurations
|
||||
.Where(configuration => configuration.LibraryId == libraryId && pluginIdList.Contains(configuration.PluginId))
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (configurations.Count > 0)
|
||||
_luminaDbContext.LibraryMetadataProviderConfigurations.RemoveRange(configurations);
|
||||
return Result.Deleted;
|
||||
}
|
||||
}
|
||||
@@ -115,6 +115,7 @@ public static class InfrastructureLayerServices
|
||||
services.AddSingleton<IPluginManager>(new PluginManager(pluginLoadResult.Plugins));
|
||||
services.AddScoped<IPluginSettingsStore, PluginSettingsStore>();
|
||||
services.AddScoped<IPluginInstaller, PluginInstaller>();
|
||||
services.AddScoped<IMediaLibraryProviderConfigurationStore, MediaLibraryProviderConfigurationStore>();
|
||||
services.AddSingleton(serviceProvider => new PluginDetectionSyncJob(
|
||||
serviceProvider.GetRequiredService<IServiceScopeFactory>(),
|
||||
serviceProvider.GetRequiredService<IPluginManager>(),
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Plugins;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using Lumina.Plugins.Contracts.Core.Metadata;
|
||||
using Lumina.Plugins.Contracts.Core.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Core.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Store of the metadata and artwork provider configurations of the media libraries.
|
||||
/// </summary>
|
||||
internal sealed class MediaLibraryProviderConfigurationStore : IMediaLibraryProviderConfigurationStore
|
||||
{
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IPluginManager _pluginManager;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MediaLibraryProviderConfigurationStore"/> class.
|
||||
/// </summary>
|
||||
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
|
||||
/// <param name="pluginManager">Injected manager of the plugins loaded by the host application.</param>
|
||||
/// <param name="serviceProvider">Injected provider used to resolve the providers registered by the plugins.</param>
|
||||
public MediaLibraryProviderConfigurationStore(IUnitOfWork unitOfWork, IPluginManager pluginManager, IServiceProvider serviceProvider)
|
||||
{
|
||||
_unitOfWork = unitOfWork;
|
||||
_pluginManager = pluginManager;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>>> GetConfigurationsAsync(Guid libraryId, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Result<Success>> EnsureProviderConfigurationsAsync(Guid libraryId, LibraryType libraryType, CancellationToken cancellationToken)
|
||||
{
|
||||
(IReadOnlyList<Guid> metadataProviderPluginIds, IReadOnlyList<Guid> artworkProviderPluginIds) = GetSupportedProviderPluginIds(libraryType);
|
||||
Result<Success> ensureMetadataResult = await EnsureMetadataConfigurationsAsync(libraryId, metadataProviderPluginIds, cancellationToken).ConfigureAwait(false);
|
||||
if (ensureMetadataResult.IsFailure)
|
||||
return ensureMetadataResult;
|
||||
return await EnsureArtworkConfigurationsAsync(libraryId, artworkProviderPluginIds, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Result<Success>> ReconcileProviderConfigurationsAsync(Guid libraryId, LibraryType libraryType, CancellationToken cancellationToken)
|
||||
{
|
||||
(IReadOnlyList<Guid> metadataProviderPluginIds, IReadOnlyList<Guid> artworkProviderPluginIds) = GetSupportedProviderPluginIds(libraryType);
|
||||
Result<Success> reconcileMetadataResult = await ReconcileMetadataConfigurationsAsync(libraryId, metadataProviderPluginIds, cancellationToken).ConfigureAwait(false);
|
||||
if (reconcileMetadataResult.IsFailure)
|
||||
return reconcileMetadataResult;
|
||||
return await ReconcileArtworkConfigurationsAsync(libraryId, artworkProviderPluginIds, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Result<Deleted>> RemoveProviderConfigurationsForLibraryAsync(Guid libraryId, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<Deleted> removeMetadataResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.DeleteByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (removeMetadataResult.IsFailure)
|
||||
return removeMetadataResult;
|
||||
return await _unitOfWork.ArtworkProviderConfigurationRepository.DeleteByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<Result<Deleted>> RemoveProviderConfigurationsAsync(Guid pluginId, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<Deleted> removeMetadataResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.DeleteByPluginIdAsync(pluginId, cancellationToken).ConfigureAwait(false);
|
||||
if (removeMetadataResult.IsFailure)
|
||||
return removeMetadataResult;
|
||||
return await _unitOfWork.ArtworkProviderConfigurationRepository.DeleteByPluginIdAsync(pluginId, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Ids of the loaded plugins that provide metadata or artwork for the provided library type,
|
||||
/// ordered alphabetically by plugin name so that the assigned ranks are stable.
|
||||
/// </summary>
|
||||
/// <param name="libraryType">The type of the media library the providers must support.</param>
|
||||
/// <returns>The Ids of the plugins providing metadata, and the Ids of the plugins providing artwork.</returns>
|
||||
private (IReadOnlyList<Guid> metadataProviderPluginIds, IReadOnlyList<Guid> artworkProviderPluginIds) GetSupportedProviderPluginIds(LibraryType libraryType)
|
||||
{
|
||||
List<Guid> metadataProviderPluginIds = [];
|
||||
List<Guid> artworkProviderPluginIds = [];
|
||||
foreach (IPlugin plugin in _pluginManager.GetPlugins().OrderBy(plugin => plugin.Name))
|
||||
{
|
||||
if (_serviceProvider.GetKeyedServices<IMetadataProvider>(plugin.Id).Any(provider => provider.SupportedLibraryTypes.Contains(libraryType)))
|
||||
metadataProviderPluginIds.Add(plugin.Id);
|
||||
if (_serviceProvider.GetKeyedServices<IArtworkProvider>(plugin.Id).Any(provider => provider.SupportedLibraryTypes.Contains(libraryType)))
|
||||
artworkProviderPluginIds.Add(plugin.Id);
|
||||
}
|
||||
return (metadataProviderPluginIds, artworkProviderPluginIds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the metadata provider configurations of the plugins that no longer support the library type, and adds a disabled
|
||||
/// configuration for every plugin that supports it and has none yet, keeping the configurations of the plugins that still apply.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are reconciled.</param>
|
||||
/// <param name="metadataProviderPluginIds">The Ids of the plugins providing metadata for the library type, in rank order.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
private async Task<Result<Success>> ReconcileMetadataConfigurationsAsync(Guid libraryId, IReadOnlyList<Guid> metadataProviderPluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getConfigurationsResult.IsFailure)
|
||||
return Result<Success>.Failure(getConfigurationsResult.Errors);
|
||||
HashSet<Guid> supportedPluginIds = metadataProviderPluginIds.ToHashSet();
|
||||
List<Guid> stalePluginIds = getConfigurationsResult.Value
|
||||
.Select(configuration => configuration.PluginId)
|
||||
.Where(pluginId => !supportedPluginIds.Contains(pluginId))
|
||||
.ToList();
|
||||
if (stalePluginIds.Count > 0)
|
||||
{
|
||||
Result<Deleted> removeResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, stalePluginIds, cancellationToken).ConfigureAwait(false);
|
||||
if (removeResult.IsFailure)
|
||||
return Result<Success>.Failure(removeResult.Errors);
|
||||
}
|
||||
HashSet<Guid> configuredPluginIds = getConfigurationsResult.Value.Select(configuration => configuration.PluginId).ToHashSet();
|
||||
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
|
||||
foreach (Guid pluginId in metadataProviderPluginIds)
|
||||
{
|
||||
if (configuredPluginIds.Contains(pluginId))
|
||||
continue;
|
||||
LibraryMetadataProviderConfigurationEntity configuration = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
LibraryId = libraryId,
|
||||
PluginId = pluginId,
|
||||
IsEnabled = false,
|
||||
Rank = nextRank,
|
||||
CreatedOnUtc = default,
|
||||
CreatedBy = default,
|
||||
UpdatedBy = default
|
||||
};
|
||||
Result<Updated> upsertResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResult.IsFailure)
|
||||
return Result<Success>.Failure(upsertResult.Errors);
|
||||
nextRank++;
|
||||
}
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the artwork provider configurations of the plugins that no longer support the library type, and adds a disabled
|
||||
/// configuration for every plugin that supports it and has none yet, keeping the configurations of the plugins that still apply.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are reconciled.</param>
|
||||
/// <param name="artworkProviderPluginIds">The Ids of the plugins providing artwork for the library type, in rank order.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
private async Task<Result<Success>> ReconcileArtworkConfigurationsAsync(Guid libraryId, IReadOnlyList<Guid> artworkProviderPluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
Result<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.ArtworkProviderConfigurationRepository.GetByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getConfigurationsResult.IsFailure)
|
||||
return Result<Success>.Failure(getConfigurationsResult.Errors);
|
||||
HashSet<Guid> supportedPluginIds = artworkProviderPluginIds.ToHashSet();
|
||||
List<Guid> stalePluginIds = getConfigurationsResult.Value
|
||||
.Select(configuration => configuration.PluginId)
|
||||
.Where(pluginId => !supportedPluginIds.Contains(pluginId))
|
||||
.ToList();
|
||||
if (stalePluginIds.Count > 0)
|
||||
{
|
||||
Result<Deleted> removeResult = await _unitOfWork.ArtworkProviderConfigurationRepository.DeleteByLibraryIdAndPluginIdsAsync(libraryId, stalePluginIds, cancellationToken).ConfigureAwait(false);
|
||||
if (removeResult.IsFailure)
|
||||
return Result<Success>.Failure(removeResult.Errors);
|
||||
}
|
||||
HashSet<Guid> configuredPluginIds = getConfigurationsResult.Value.Select(configuration => configuration.PluginId).ToHashSet();
|
||||
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
|
||||
foreach (Guid pluginId in artworkProviderPluginIds)
|
||||
{
|
||||
if (configuredPluginIds.Contains(pluginId))
|
||||
continue;
|
||||
LibraryArtworkProviderConfigurationEntity configuration = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
LibraryId = libraryId,
|
||||
PluginId = pluginId,
|
||||
IsEnabled = false,
|
||||
Rank = nextRank,
|
||||
CreatedOnUtc = default,
|
||||
CreatedBy = default,
|
||||
UpdatedBy = default
|
||||
};
|
||||
Result<Updated> upsertResult = await _unitOfWork.ArtworkProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResult.IsFailure)
|
||||
return Result<Success>.Failure(upsertResult.Errors);
|
||||
nextRank++;
|
||||
}
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a disabled metadata provider configuration for every provided plugin that has no configuration yet for the media library.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose metadata provider configurations are ensured.</param>
|
||||
/// <param name="metadataProviderPluginIds">The Ids of the plugins providing metadata for the library type, in rank order.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
private async Task<Result<Success>> EnsureMetadataConfigurationsAsync(Guid libraryId, IReadOnlyList<Guid> metadataProviderPluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
if (metadataProviderPluginIds.Count == 0)
|
||||
return Result.Success;
|
||||
Result<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.GetByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getConfigurationsResult.IsFailure)
|
||||
return Result<Success>.Failure(getConfigurationsResult.Errors);
|
||||
HashSet<Guid> configuredPluginIds = getConfigurationsResult.Value.Select(configuration => configuration.PluginId).ToHashSet();
|
||||
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
|
||||
foreach (Guid pluginId in metadataProviderPluginIds)
|
||||
{
|
||||
if (configuredPluginIds.Contains(pluginId))
|
||||
continue;
|
||||
LibraryMetadataProviderConfigurationEntity configuration = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
LibraryId = libraryId,
|
||||
PluginId = pluginId,
|
||||
IsEnabled = false,
|
||||
Rank = nextRank,
|
||||
CreatedOnUtc = default,
|
||||
CreatedBy = default,
|
||||
UpdatedBy = default
|
||||
};
|
||||
Result<Updated> upsertResult = await _unitOfWork.LibraryMetadataProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResult.IsFailure)
|
||||
return Result<Success>.Failure(upsertResult.Errors);
|
||||
nextRank++;
|
||||
}
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a disabled artwork provider configuration for every provided plugin that has no configuration yet for the media library.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library whose artwork provider configurations are ensured.</param>
|
||||
/// <param name="artworkProviderPluginIds">The Ids of the plugins providing artwork for the library type, in rank order.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>An <see cref="Result{TValue}"/> representing either a successful operation, or an error.</returns>
|
||||
private async Task<Result<Success>> EnsureArtworkConfigurationsAsync(Guid libraryId, IReadOnlyList<Guid> artworkProviderPluginIds, CancellationToken cancellationToken)
|
||||
{
|
||||
if (artworkProviderPluginIds.Count == 0)
|
||||
return Result.Success;
|
||||
Result<IReadOnlyList<LibraryArtworkProviderConfigurationEntity>> getConfigurationsResult = await _unitOfWork.ArtworkProviderConfigurationRepository.GetByLibraryIdAsync(libraryId, cancellationToken).ConfigureAwait(false);
|
||||
if (getConfigurationsResult.IsFailure)
|
||||
return Result<Success>.Failure(getConfigurationsResult.Errors);
|
||||
HashSet<Guid> configuredPluginIds = getConfigurationsResult.Value.Select(configuration => configuration.PluginId).ToHashSet();
|
||||
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
|
||||
foreach (Guid pluginId in artworkProviderPluginIds)
|
||||
{
|
||||
if (configuredPluginIds.Contains(pluginId))
|
||||
continue;
|
||||
LibraryArtworkProviderConfigurationEntity configuration = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
LibraryId = libraryId,
|
||||
PluginId = pluginId,
|
||||
IsEnabled = false,
|
||||
Rank = nextRank,
|
||||
CreatedOnUtc = default,
|
||||
CreatedBy = default,
|
||||
UpdatedBy = default
|
||||
};
|
||||
Result<Updated> upsertResult = await _unitOfWork.ArtworkProviderConfigurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
|
||||
if (upsertResult.IsFailure)
|
||||
return Result<Success>.Failure(upsertResult.Errors);
|
||||
nextRank++;
|
||||
}
|
||||
return Result.Success;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
@@ -106,7 +107,9 @@ internal sealed class PluginDetectionSyncJob : BackgroundService
|
||||
_logger.LogError("Failed to persist the detection of plugin '{PluginName}': {Error}", loadError.AssemblyName, upsertResult.FirstError.Description);
|
||||
}
|
||||
|
||||
// remove the rows of the plugins that are no longer detected, so that each plugin assembly has exactly one row regardless of its load status
|
||||
// remove the rows of the plugins that are no longer detected, so that each plugin assembly has exactly one row regardless of its load status,
|
||||
// and delete the provider configurations of the removed plugins, since their configuration only makes sense while the plugin is installed
|
||||
IMediaLibraryProviderConfigurationStore providerConfigurationStore = asyncServiceScope.ServiceProvider.GetRequiredService<IMediaLibraryProviderConfigurationStore>();
|
||||
Result<IEnumerable<PluginEntity>> getPluginsResult = await unitOfWork.PluginRepository.GetAllAsync(stoppingToken).ConfigureAwait(false);
|
||||
if (getPluginsResult.IsFailure)
|
||||
_logger.LogError("Failed to get the detected plugins while reconciling the plugin rows.");
|
||||
@@ -119,6 +122,23 @@ internal sealed class PluginDetectionSyncJob : BackgroundService
|
||||
Result<Deleted> deleteResult = await unitOfWork.PluginRepository.DeleteByIdAsync(plugin.Id, stoppingToken).ConfigureAwait(false);
|
||||
if (deleteResult.IsFailure)
|
||||
_logger.LogError("Failed to remove the stale detection of plugin '{PluginName}': {Error}", plugin.Name, deleteResult.FirstError.Description);
|
||||
Result<Deleted> removeConfigurationsResult = await providerConfigurationStore.RemoveProviderConfigurationsAsync(plugin.Id, stoppingToken).ConfigureAwait(false);
|
||||
if (removeConfigurationsResult.IsFailure)
|
||||
_logger.LogError("Failed to remove the provider configurations of the removed plugin '{PluginName}': {Error}", plugin.Name, removeConfigurationsResult.FirstError.Description);
|
||||
}
|
||||
}
|
||||
|
||||
// seed the provider configurations of the media libraries, so that every library lists the plugins providing metadata or artwork for its type
|
||||
Result<IEnumerable<LibraryEntity>> getLibrariesResult = await unitOfWork.LibraryRepository.GetAllAsync(stoppingToken).ConfigureAwait(false);
|
||||
if (getLibrariesResult.IsFailure)
|
||||
_logger.LogError("Failed to get the media libraries while seeding the provider configurations.");
|
||||
else
|
||||
{
|
||||
foreach (LibraryEntity library in getLibrariesResult.Value)
|
||||
{
|
||||
Result<Success> ensureResult = await providerConfigurationStore.EnsureProviderConfigurationsAsync(library.Id, library.LibraryType, stoppingToken).ConfigureAwait(false);
|
||||
if (ensureResult.IsFailure)
|
||||
_logger.LogError("Failed to seed the provider configurations of the media library '{LibraryTitle}': {Error}", library.Title, ensureResult.FirstError.Description);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user