Adds the server-side theme repository and reworks the web theme engine onto it

This commit is contained in:
BinaryAlley committed 2026-08-19 22:50:07 -07:00
1 parent 85928f7c3c
commit bf8faba24d
136 files changed
+6942 -1197

No files matched your search

@@ -0,0 +1,90 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Domain.SharedKernel.Common.Enums.Themes;
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.DataAccess.Entities.Themes;
/// <summary>
/// Repository entity for a theme.
/// </summary>
[DebuggerDisplay("ThemeId: {ThemeId}, Name: {Name}")]
public class ThemeEntity : IStorageEntity, IAuditableEntity
{
/// <summary>
/// Gets the Id of the theme.
/// </summary>
public required Guid Id { get; init; }
/// <summary>
/// Gets or sets the unique identifier of the theme, taken from its manifest and used by clients to reference it.
/// </summary>
public required string ThemeId { get; set; }
/// <summary>
/// Gets or sets the display name of the theme.
/// </summary>
public required string Name { get; set; }
/// <summary>
/// Gets or sets the description of the theme.
/// </summary>
public required string Description { get; set; }
/// <summary>
/// Gets or sets the author of the theme.
/// </summary>
public required string Author { get; set; }
/// <summary>
/// Gets or sets the version of the theme, using semantic version form.
/// </summary>
public required string Version { get; set; }
/// <summary>
/// Gets or sets the path of the theme preview image, relative to the theme pack root, or <see langword="null"/> when the theme has no preview.
/// </summary>
public string? PreviewPath { get; set; }
/// <summary>
/// Gets or sets the source the theme was installed from.
/// </summary>
public required ThemeInstallSource InstallSource { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this is the currently active theme. At most one theme can be current.
/// </summary>
public bool? IsCurrent { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the theme was soft deleted, so it is not shown and not reinstalled automatically.
/// </summary>
public bool IsDeleted { get; set; }
/// <summary>
/// Gets or sets the UTC timestamp at which the theme was installed.
/// </summary>
public required DateTime InstalledAtUtc { get; set; }
/// <summary>
/// Gets or sets the time and date when the entity was added.
/// </summary>
public required DateTime CreatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the Id of the user that created the entity.
/// </summary>
public required Guid CreatedBy { get; set; }
/// <summary>
/// Gets or sets the optional time and date when the entity was updated.
/// </summary>
public DateTime? UpdatedOnUtc { get; set; }
/// <summary>
/// Gets or sets the optional Id of the user that updated the entity.
/// </summary>
public required Guid? UpdatedBy { get; set; }
}
@@ -0,0 +1,34 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Application.Common.DataAccess.Entities.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Common.Actions;
using Lumina.Application.Common.DataAccess.Repositories.Common.Base;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.DataAccess.Repositories.Themes;
/// <summary>
/// Interface for the repository for themes.
/// </summary>
public interface IThemeRepository : IRepository<ThemeEntity>,
IGetAllRepositoryAction<ThemeEntity>,
IInsertRepositoryAction<ThemeEntity>,
IUpdateRepositoryAction<ThemeEntity>
{
/// <summary>
/// Gets a theme identified by its manifest <paramref name="themeId"/> from the storage medium.
/// </summary>
/// <param name="themeId">The manifest id of the theme to get.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a <see cref="ThemeEntity"/> identified by <paramref name="themeId"/>, or an error.</returns>
Task<Result<ThemeEntity?>> GetByThemeIdAsync(string themeId, CancellationToken cancellationToken);
/// <summary>
/// Gets the currently active theme from the storage medium.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the active <see cref="ThemeEntity"/>, or an error.</returns>
Task<Result<ThemeEntity?>> GetCurrentAsync(CancellationToken cancellationToken);
}
@@ -3,6 +3,7 @@ using Lumina.Application.Common.DataAccess.Repositories.Authorization;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Application.Common.DataAccess.Repositories.Plugins;
using Lumina.Application.Common.DataAccess.Repositories.Themes;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using System.Threading;
using System.Threading.Tasks;
@@ -75,6 +76,11 @@ public interface IUnitOfWork
/// </summary>
IPluginRepository PluginRepository { get; }
/// <summary>
/// Gets the theme repository.
/// </summary>
IThemeRepository ThemeRepository { get; }
/// <summary>
/// Gets the user repository.
/// </summary>
@@ -0,0 +1,19 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
/// <summary>
/// Data transfer object for a downloadable archive of a theme pack.
/// </summary>
/// <param name="Bytes">The bytes of the ZIP archive.</param>
/// <param name="FileName">The file name to expose when downloading the archive.</param>
/// <param name="ContentType">The MIME content type of the archive.</param>
[DebuggerDisplay("FileName: {FileName}")]
public sealed record ThemeArchiveDto(
byte[] Bytes,
string FileName,
string ContentType
);
@@ -0,0 +1,17 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
/// <summary>
/// Data transfer object for a theme asset file served by the theme service.
/// </summary>
/// <param name="Bytes">The bytes of the asset file.</param>
/// <param name="ContentType">The MIME content type of the asset file.</param>
[DebuggerDisplay("ContentType: {ContentType}")]
public sealed record ThemeAssetDto(
byte[] Bytes,
string ContentType
);
@@ -0,0 +1,54 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Collections.Generic;
using System.Diagnostics;
#endregion
namespace Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
/// <summary>
/// Data transfer object for the theme.json manifest of a theme pack.
/// </summary>
[DebuggerDisplay("Id: {Id}, Name: {Name}")]
public sealed class ThemeManifestDto
{
/// <summary>
/// Gets or sets the supported schema version of the manifest.
/// </summary>
public int SchemaVersion { get; set; }
/// <summary>
/// Gets or sets the unique identifier of the theme, a lowercase kebab-case value used by clients to reference it.
/// </summary>
public string Id { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the display name of the theme.
/// </summary>
public string Name { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the description of the theme.
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the author of the theme.
/// </summary>
public string Author { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the version of the theme, using semantic version form.
/// </summary>
public string Version { get; set; } = string.Empty;
/// <summary>
/// Gets or sets the preview image path relative to the theme pack root, or <see langword="null"/> when the theme has no preview.
/// </summary>
public string? Preview { get; set; }
/// <summary>
/// Gets or sets the template mappings from template key to a file path under templates/.
/// </summary>
public Dictionary<string, string> Templates { get; set; } = new(StringComparer.OrdinalIgnoreCase);
}
@@ -0,0 +1,103 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Models.DTO.Themes;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Common.Infrastructure.Themes;
/// <summary>
/// Service for the storage and serving of theme packs.
/// </summary>
public interface IThemeService
{
/// <summary>
/// Gets a value indicating whether theme templates may contain script elements and theme assets may include script files.
/// </summary>
bool AllowThemeScripts { get; }
/// <summary>
/// Gets the maximum allowed size of a theme archive, in bytes.
/// </summary>
long MaxArchiveBytes { get; }
/// <summary>
/// Gets the identifier of the theme selected when no valid current theme is available.
/// </summary>
string DefaultThemeId { get; }
/// <summary>
/// Installs a theme pack from the provided archive, replacing the files of an existing theme with the same manifest id.
/// </summary>
/// <param name="archive">The ZIP archive stream of the theme pack.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the parsed manifest of the installed theme, or an error.</returns>
Task<Result<ThemeManifestDto>> InstallAsync(Stream archive, CancellationToken cancellationToken);
/// <summary>
/// Reads the manifest of a theme pack archive without installing it.
/// </summary>
/// <param name="archivePath">The path of the theme pack ZIP archive.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the parsed manifest, or an error.</returns>
Task<Result<ThemeManifestDto>> ReadManifestFromArchiveAsync(string archivePath, CancellationToken cancellationToken);
/// <summary>
/// Deletes the stored files of a theme pack.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</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>> DeleteAsync(string themeId, CancellationToken cancellationToken);
/// <summary>
/// Loads the manifest of an installed theme pack from its storage location.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the parsed manifest, or an error.</returns>
Task<Result<ThemeManifestDto>> LoadManifestAsync(string themeId, CancellationToken cancellationToken);
/// <summary>
/// Gets the sanitized content of the template selected by a page key, falling back to the default template when the key is missing.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</param>
/// <param name="pageKey">The page key that selects the template.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the template content, or an error.</returns>
Task<Result<string>> GetTemplateAsync(string themeId, string pageKey, CancellationToken cancellationToken);
/// <summary>
/// Gets a theme asset file.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</param>
/// <param name="assetPath">The asset path relative to the theme pack root.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the asset, or an error.</returns>
Task<Result<ThemeAssetDto>> GetAssetAsync(string themeId, string assetPath, CancellationToken cancellationToken);
/// <summary>
/// Builds a downloadable ZIP archive of an installed theme pack.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either the archive, or an error.</returns>
Task<Result<ThemeArchiveDto>> BuildArchiveAsync(string themeId, CancellationToken cancellationToken);
/// <summary>
/// Restores the files of a bundled theme from its shipped archive, used when the stored files were removed externally.
/// </summary>
/// <param name="themeId">The manifest id of the theme.</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>> RestoreBundledThemeAsync(string themeId, CancellationToken cancellationToken);
/// <summary>
/// Gets the paths of the theme pack archives shipped with the application.
/// </summary>
/// <returns>The list of bundled theme archive paths.</returns>
IReadOnlyList<string> GetBundledThemeArchivePaths();
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Commands.DeleteTheme;
using Lumina.Contracts.Requests.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="DeleteThemeRequest"/>.
/// </summary>
public static class DeleteThemeRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="DeleteThemeCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static DeleteThemeCommand ToCommand(this DeleteThemeRequest request)
{
return new DeleteThemeCommand(request.ThemeId);
}
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Queries.GetThemeArchive;
using Lumina.Contracts.Requests.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="GetThemeArchiveRequest"/>.
/// </summary>
public static class GetThemeArchiveRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetThemeArchiveQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetThemeArchiveQuery ToQuery(this GetThemeArchiveRequest request)
{
return new GetThemeArchiveQuery(request.ThemeId);
}
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Queries.GetThemeAsset;
using Lumina.Contracts.Requests.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="GetThemeAssetRequest"/>.
/// </summary>
public static class GetThemeAssetRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetThemeAssetQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetThemeAssetQuery ToQuery(this GetThemeAssetRequest request)
{
return new GetThemeAssetQuery(request.ThemeId, request.AssetPath);
}
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Queries.GetThemeTemplate;
using Lumina.Contracts.Requests.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="GetThemeTemplateRequest"/>.
/// </summary>
public static class GetThemeTemplateRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="GetThemeTemplateQuery"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted query.</returns>
public static GetThemeTemplateQuery ToQuery(this GetThemeTemplateRequest request)
{
return new GetThemeTemplateQuery(request.ThemeId, request.PageKey);
}
}
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Commands.InstallTheme;
using Lumina.Contracts.Requests.Themes;
using System.IO;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="InstallThemeRequest"/>.
/// </summary>
public static class InstallThemeRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="InstallThemeCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <param name="archive">The ZIP archive stream of the theme pack.</param>
/// <param name="fileName">The file name of the uploaded archive.</param>
/// <returns>The converted command.</returns>
public static InstallThemeCommand ToCommand(this InstallThemeRequest request, Stream? archive, string? fileName)
{
return new InstallThemeCommand(archive, fileName);
}
}
@@ -0,0 +1,22 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Themes.Management.Commands.SetCurrentTheme;
using Lumina.Contracts.Requests.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="SetCurrentThemeRequest"/>.
/// </summary>
public static class SetCurrentThemeRequestMapping
{
/// <summary>
/// Converts <paramref name="request"/> to <see cref="SetCurrentThemeCommand"/>.
/// </summary>
/// <param name="request">The request to be converted.</param>
/// <returns>The converted command.</returns>
public static SetCurrentThemeCommand ToCommand(this SetCurrentThemeRequest request)
{
return new SetCurrentThemeCommand(request.ThemeId);
}
}
@@ -0,0 +1,32 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.DataAccess.Entities.Themes;
using Lumina.Contracts.Responses.Themes;
#endregion
namespace Lumina.Application.Common.Mapping.Themes;
/// <summary>
/// Extension methods for converting <see cref="ThemeEntity"/>.
/// </summary>
public static class ThemeEntityMapping
{
/// <summary>
/// Converts <paramref name="theme"/> to <see cref="ThemeResponse"/>.
/// </summary>
/// <param name="theme">The repository entity to be converted.</param>
/// <returns>The converted response.</returns>
public static ThemeResponse ToResponse(this ThemeEntity theme)
{
return new ThemeResponse(
theme.Id,
theme.ThemeId,
theme.Name,
theme.Description,
theme.Author,
theme.Version,
theme.PreviewPath,
theme.InstallSource,
theme.IsCurrent,
theme.InstalledAtUtc);
}
}
@@ -39,6 +39,33 @@ public static class ValidatorUtilities
return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare);
}
/// <summary>
/// Defines a validation rule that ensures the long property is greater than the specified value.
/// </summary>
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
/// <param name="ruleBuilder">The rule builder instance.</param>
/// <param name="valueToCompare">The value that the property must be greater than.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
public static IRuleBuilder<TRequest, long> GreaterThan<TRequest>(this IRuleBuilder<TRequest, long> ruleBuilder, long valueToCompare)
{
return ruleBuilder.Must(value => value > valueToCompare);
}
/// <summary>
/// Defines a validation rule that ensures the nullable long property is greater than the specified value.
/// </summary>
/// <remarks>
/// If the property is <see langword="null"/>, this rule passes (use <see cref="NotNull{TRequest, TProperty}(IRuleBuilder{TRequest, TProperty})"/> to check for missing values).
/// </remarks>
/// <typeparam name="TRequest">The type of the request being validated.</typeparam>
/// <param name="ruleBuilder">The rule builder instance.</param>
/// <param name="valueToCompare">The value that the property must be greater than.</param>
/// <returns>The current <see cref="IRuleBuilder{TRequest, TProperty}"/> instance for method chaining.</returns>
public static IRuleBuilder<TRequest, long?> GreaterThan<TRequest>(this IRuleBuilder<TRequest, long?> ruleBuilder, long valueToCompare)
{
return ruleBuilder.Must(value => !value.HasValue || value.Value > valueToCompare);
}
/// <summary>
/// Defines a validation rule that ensures the decimal property is greater than the specified value.
/// </summary>