Adds the plugins and metadata provider management API

This commit is contained in:
BinaryAlley committed 2026-08-12 11:05:53 -07:00
1 parent 6f501a51f3
commit 7d417c1710
51 files changed
+2294

No files matched your search

@@ -0,0 +1,18 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Mediator;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.ReorderLibraryMetadataProviders;
/// <summary>
/// Command for reordering the metadata providers of a media library.
/// </summary>
/// <param name="LibraryId">The Id of the media library whose metadata providers are reordered.</param>
/// <param name="PluginIds">The plugin Ids in the new order, from highest to lowest rank.</param>
public record ReorderLibraryMetadataProvidersCommand(
Guid LibraryId,
IReadOnlyList<Guid> PluginIds
) : IRequest<ErrorOr<Success>>;
@@ -0,0 +1,63 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.ReorderLibraryMetadataProviders;
/// <summary>
/// Handler for the command to reorder the metadata providers of a media library.
/// </summary>
public class ReorderLibraryMetadataProvidersCommandHandler : IRequestHandler<ReorderLibraryMetadataProvidersCommand, ErrorOr<Success>>
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="ReorderLibraryMetadataProvidersCommandHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public ReorderLibraryMetadataProvidersCommandHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the command to reorder the metadata providers of a media library.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.
/// </returns>
public async ValueTask<ErrorOr<Success>> Handle(ReorderLibraryMetadataProvidersCommand request, CancellationToken cancellationToken)
{
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
ErrorOr<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(request.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsError)
return getConfigurationsResult.Errors;
Dictionary<Guid, LibraryMetadataProviderConfigurationEntity> configurationsByPluginId = getConfigurationsResult.Value.ToDictionary(configuration => configuration.PluginId);
for (int rank = 0; rank < request.PluginIds.Count; rank++)
{
Guid pluginId = request.PluginIds[rank];
if (configurationsByPluginId.TryGetValue(pluginId, out LibraryMetadataProviderConfigurationEntity? configuration))
{
configuration.Rank = rank + 1;
ErrorOr<Updated> upsertResult = await configurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsError)
return upsertResult.Errors;
}
}
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return Result.Success;
}
}
@@ -0,0 +1,24 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Domain.SharedKernel.Common.Errors;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.ReorderLibraryMetadataProviders;
/// <summary>
/// Validates the needed validation rules for <see cref="ReorderLibraryMetadataProvidersCommand"/>.
/// </summary>
public class ReorderLibraryMetadataProvidersCommandValidator : AbstractValidator<ReorderLibraryMetadataProvidersCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="ReorderLibraryMetadataProvidersCommandValidator"/> class.
/// </summary>
public ReorderLibraryMetadataProvidersCommandValidator()
{
RuleFor(x => x.LibraryId)
.NotEmpty().WithMessage(Errors.Plugins.LibraryIdCannotBeEmpty.Description);
RuleFor(x => x.PluginIds)
.NotNull().WithMessage(Errors.Plugins.PluginIdsListCannotBeNull.Description)
.NotEmpty().WithMessage(Errors.Plugins.PluginIdsListCannotBeEmpty.Description);
}
}
@@ -0,0 +1,19 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Mediator;
using System;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.SetLibraryMetadataProviderEnabled;
/// <summary>
/// Command for enabling or disabling a metadata provider for a media library.
/// </summary>
/// <param name="LibraryId">The Id of the media library whose metadata provider is enabled or disabled.</param>
/// <param name="PluginId">The unique identifier of the plugin providing the metadata.</param>
/// <param name="IsEnabled">Whether the metadata provider should be enabled for the media library.</param>
public record SetLibraryMetadataProviderEnabledCommand(
Guid LibraryId,
Guid PluginId,
bool IsEnabled
) : IRequest<ErrorOr<Success>>;
@@ -0,0 +1,79 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.SetLibraryMetadataProviderEnabled;
/// <summary>
/// Handler for the command to enable or disable a metadata provider for a media library.
/// </summary>
public class SetLibraryMetadataProviderEnabledCommandHandler : IRequestHandler<SetLibraryMetadataProviderEnabledCommand, ErrorOr<Success>>
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="SetLibraryMetadataProviderEnabledCommandHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public SetLibraryMetadataProviderEnabledCommandHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the command to enable or disable a metadata provider for a media library.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.
/// </returns>
public async ValueTask<ErrorOr<Success>> Handle(SetLibraryMetadataProviderEnabledCommand request, CancellationToken cancellationToken)
{
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
ErrorOr<LibraryMetadataProviderConfigurationEntity?> getConfigurationResult = await configurationRepository.GetByLibraryAndPluginIdAsync(request.LibraryId, request.PluginId, cancellationToken).ConfigureAwait(false);
if (getConfigurationResult.IsError)
return getConfigurationResult.Errors;
LibraryMetadataProviderConfigurationEntity configuration;
if (getConfigurationResult.Value is not null)
{
configuration = getConfigurationResult.Value;
configuration.IsEnabled = request.IsEnabled;
}
else
{
// compute the rank to assign to the new configuration, appending it after the existing ones
ErrorOr<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(request.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsError)
return getConfigurationsResult.Errors;
int nextRank = getConfigurationsResult.Value.Count == 0 ? 1 : getConfigurationsResult.Value.Max(configuration => configuration.Rank) + 1;
configuration = new LibraryMetadataProviderConfigurationEntity
{
Id = Guid.NewGuid(),
LibraryId = request.LibraryId,
PluginId = request.PluginId,
IsEnabled = request.IsEnabled,
Rank = nextRank,
CreatedOnUtc = default,
CreatedBy = default,
UpdatedBy = default
};
}
ErrorOr<Updated> upsertResult = await configurationRepository.UpsertAsync(configuration, cancellationToken).ConfigureAwait(false);
if (upsertResult.IsError)
return upsertResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return Result.Success;
}
}
@@ -0,0 +1,23 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Domain.SharedKernel.Common.Errors;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.SetLibraryMetadataProviderEnabled;
/// <summary>
/// Validates the needed validation rules for <see cref="SetLibraryMetadataProviderEnabledCommand"/>.
/// </summary>
public class SetLibraryMetadataProviderEnabledCommandValidator : AbstractValidator<SetLibraryMetadataProviderEnabledCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="SetLibraryMetadataProviderEnabledCommandValidator"/> class.
/// </summary>
public SetLibraryMetadataProviderEnabledCommandValidator()
{
RuleFor(x => x.LibraryId)
.NotEmpty().WithMessage(Errors.Plugins.LibraryIdCannotBeEmpty.Description);
RuleFor(x => x.PluginId)
.NotEmpty().WithMessage(Errors.Plugins.PluginIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,18 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Mediator;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.UpdatePluginSettings;
/// <summary>
/// Command for updating the settings of a plugin.
/// </summary>
/// <param name="PluginId">The unique identifier of the plugin.</param>
/// <param name="Settings">The settings of the plugin.</param>
public record UpdatePluginSettingsCommand(
Guid PluginId,
IReadOnlyDictionary<string, string>? Settings
) : IRequest<ErrorOr<Success>>;
@@ -0,0 +1,47 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Mediator;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.UpdatePluginSettings;
/// <summary>
/// Handler for the command to update the settings of a plugin.
/// </summary>
public class UpdatePluginSettingsCommandHandler : IRequestHandler<UpdatePluginSettingsCommand, ErrorOr<Success>>
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePluginSettingsCommandHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public UpdatePluginSettingsCommandHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the command to update the settings of a plugin.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> representing either a successful operation, or an error.
/// </returns>
public async ValueTask<ErrorOr<Success>> Handle(UpdatePluginSettingsCommand request, CancellationToken cancellationToken)
{
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
string? settingsJson = request.Settings is not null ? JsonSerializer.Serialize(request.Settings) : null;
ErrorOr<Updated> updateResult = await pluginRepository.UpdateSettingsAsync(request.PluginId, settingsJson, cancellationToken).ConfigureAwait(false);
if (updateResult.IsError)
return updateResult.Errors;
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
return Result.Success;
}
}
@@ -0,0 +1,21 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation;
using Lumina.Domain.SharedKernel.Common.Errors;
#endregion
namespace Lumina.Application.Core.Plugins.Commands.UpdatePluginSettings;
/// <summary>
/// Validates the needed validation rules for <see cref="UpdatePluginSettingsCommand"/>.
/// </summary>
public class UpdatePluginSettingsCommandValidator : AbstractValidator<UpdatePluginSettingsCommand>
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdatePluginSettingsCommandValidator"/> class.
/// </summary>
public UpdatePluginSettingsCommandValidator()
{
RuleFor(x => x.PluginId)
.NotEmpty().WithMessage(Errors.Plugins.PluginIdCannotBeEmpty.Description);
}
}
@@ -0,0 +1,17 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Plugins;
using Mediator;
using System;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetLibraryMetadataProviders;
/// <summary>
/// Query for getting the metadata providers configured for a media library.
/// </summary>
/// <param name="LibraryId">The Id of the media library whose metadata providers are retrieved.</param>
public record GetLibraryMetadataProvidersQuery(
Guid LibraryId
) : IRequest<ErrorOr<IReadOnlyList<LibraryMetadataProviderResponse>>>;
@@ -0,0 +1,61 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Mapping.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Mediator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetLibraryMetadataProviders;
/// <summary>
/// Handler for the query to get the metadata providers configured for a media library.
/// </summary>
public class GetLibraryMetadataProvidersQueryHandler : IRequestHandler<GetLibraryMetadataProvidersQuery, ErrorOr<IReadOnlyList<LibraryMetadataProviderResponse>>>
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="GetLibraryMetadataProvidersQueryHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetLibraryMetadataProvidersQueryHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the query to get the metadata providers configured for a media library.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="LibraryMetadataProviderResponse"/>, or an error.
/// </returns>
public async ValueTask<ErrorOr<IReadOnlyList<LibraryMetadataProviderResponse>>> Handle(GetLibraryMetadataProvidersQuery request, CancellationToken cancellationToken)
{
ILibraryMetadataProviderConfigurationRepository configurationRepository = _unitOfWork.GetRepository<ILibraryMetadataProviderConfigurationRepository>();
ErrorOr<IReadOnlyList<LibraryMetadataProviderConfigurationEntity>> getConfigurationsResult = await configurationRepository.GetByLibraryIdAsync(request.LibraryId, cancellationToken).ConfigureAwait(false);
if (getConfigurationsResult.IsError)
return getConfigurationsResult.Errors;
// build a plugin name lookup from the detected plugins
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
ErrorOr<IEnumerable<PluginEntity>> getPluginsResult = await pluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
if (getPluginsResult.IsError)
return getPluginsResult.Errors;
Dictionary<Guid, string> pluginNames = getPluginsResult.Value.ToDictionary(plugin => plugin.Id, plugin => plugin.Name);
return getConfigurationsResult.Value
.OrderBy(configuration => configuration.Rank)
.Select(configuration => configuration.ToResponse(pluginNames.GetValueOrDefault(configuration.PluginId) ?? string.Empty))
.ToList();
}
}
@@ -0,0 +1,16 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Plugins;
using Mediator;
using System;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetPluginSettings;
/// <summary>
/// Query for getting the settings of a plugin and their schema.
/// </summary>
/// <param name="PluginId">The unique identifier of the plugin.</param>
public record GetPluginSettingsQuery(
Guid PluginId
) : IRequest<ErrorOr<PluginSettingsResponse>>;
@@ -0,0 +1,64 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Infrastructure.Plugins;
using Lumina.Application.Common.Mapping.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Lumina.Domain.SharedKernel.Common.Errors;
using Lumina.Plugins.Contracts.Core.Plugins;
using Mediator;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetPluginSettings;
/// <summary>
/// Handler for the query to get the settings of a plugin and their schema.
/// </summary>
public class GetPluginSettingsQueryHandler : IRequestHandler<GetPluginSettingsQuery, ErrorOr<PluginSettingsResponse>>
{
private readonly IUnitOfWork _unitOfWork;
private readonly IPluginManager _pluginManager;
/// <summary>
/// Initializes a new instance of the <see cref="GetPluginSettingsQueryHandler"/> 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>
public GetPluginSettingsQueryHandler(IUnitOfWork unitOfWork, IPluginManager pluginManager)
{
_unitOfWork = unitOfWork;
_pluginManager = pluginManager;
}
/// <summary>
/// Handles the query to get the settings of a plugin and their schema.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a <see cref="PluginSettingsResponse"/>, or an error.
/// </returns>
public async ValueTask<ErrorOr<PluginSettingsResponse>> Handle(GetPluginSettingsQuery request, CancellationToken cancellationToken)
{
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
ErrorOr<PluginEntity?> getPluginResult = await pluginRepository.GetByIdAsync(request.PluginId, cancellationToken).ConfigureAwait(false);
if (getPluginResult.IsError)
return getPluginResult.Errors;
PluginEntity? pluginEntity = getPluginResult.Value;
if (pluginEntity is null)
return Errors.Plugins.PluginNotFound;
// the schema comes from the loaded plugin, the current values from the storage medium
IPlugin? loadedPlugin = _pluginManager.GetPlugin(request.PluginId);
IReadOnlyList<PluginSettingDescriptorResponse> schema = loadedPlugin is not null
? loadedPlugin.GetSettingsSchema().Select(setting => setting.ToResponse()).ToList()
: [];
return new PluginSettingsResponse(request.PluginId, schema, pluginEntity.ToSettings());
}
}
@@ -0,0 +1,13 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Contracts.Responses.Plugins;
using Mediator;
using System.Collections.Generic;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetPlugins;
/// <summary>
/// Query for getting all the detected plugins.
/// </summary>
public record GetPluginsQuery() : IRequest<ErrorOr<IReadOnlyList<PluginResponse>>>;
@@ -0,0 +1,49 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Common.Mapping.Plugins;
using Lumina.Contracts.Responses.Plugins;
using Mediator;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Plugins.Queries.GetPlugins;
/// <summary>
/// Handler for the query to get all the detected plugins.
/// </summary>
public class GetPluginsQueryHandler : IRequestHandler<GetPluginsQuery, ErrorOr<IReadOnlyList<PluginResponse>>>
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="GetPluginsQueryHandler"/> class.
/// </summary>
/// <param name="unitOfWork">Injected unit of work for interacting with the data access layer repositories.</param>
public GetPluginsQueryHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
/// <summary>
/// Handles the query to get all the detected plugins.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a collection of <see cref="PluginResponse"/>, or an error.
/// </returns>
public async ValueTask<ErrorOr<IReadOnlyList<PluginResponse>>> Handle(GetPluginsQuery request, CancellationToken cancellationToken)
{
IPluginRepository pluginRepository = _unitOfWork.GetRepository<IPluginRepository>();
ErrorOr<IEnumerable<PluginEntity>> getPluginsResult = await pluginRepository.GetAllAsync(cancellationToken).ConfigureAwait(false);
return getPluginsResult.Match(
plugins => ErrorOrFactory.From<IReadOnlyList<PluginResponse>>(plugins.Select(plugin => plugin.ToResponse()).ToList()),
errors => errors);
}
}