Adds a restore action for soft deleted bundled themes

This commit is contained in:
BinaryAlley committed 2026-08-21 11:47:51 -07:00
1 parent 15945c2e56
commit 7e54c990ad
49 files changed
+1666 -26

No files matched your search

@@ -0,0 +1,59 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Presentation.Web.Common.Authorization;
using Lumina.Presentation.Web.Common.Requests.Themes;
using Lumina.Presentation.Web.Common.Routes;
using Lumina.Presentation.Web.Core.Endpoints.Common;
using Lumina.Presentation.Web.Core.Themes;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Core.Endpoints.Admin.Themes.RestoreTheme;
/// <summary>
/// API endpoint for the <c>/{culture}/admin/themes/api-restore-theme/{themeId}</c> route.
/// </summary>
public class RestoreThemeEndpoint : BaseEndpoint<RestoreThemeRequest, IResult>
{
private readonly ThemeService _themeService;
/// <summary>
/// Initializes a new instance of the <see cref="RestoreThemeEndpoint"/> class.
/// </summary>
/// <param name="themeService">Injected service for interactions with the remote theme endpoints.</param>
public RestoreThemeEndpoint(ThemeService themeService)
{
_themeService = themeService;
}
/// <summary>
/// Configures the endpoint.
/// </summary>
public override void Configure()
{
base.Configure();
Verbs(Http.POST);
Routes(WebRoutes.Themes.RESTORE_THEME);
DontAutoTag();
Options(options => options.WithTags("Themes"));
Policies(AuthorizationPolicies.REQUIRE_ADMIN_ROLE);
EnableAntiforgery();
}
/// <summary>
/// Restores the soft deleted bundled theme identified by <paramref name="request"/>.
/// </summary>
/// <param name="request">The request containing the theme to restore.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(RestoreThemeRequest request, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(request.ThemeId))
return Results.Problem(statusCode: StatusCodes.Status400BadRequest, detail: "The theme id cannot be empty.");
await _themeService.RestoreThemeAsync(request.ThemeId, cancellationToken).ConfigureAwait(false);
return JsonSuccess();
}
}
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Herunterladen</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Wiederherstellen</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Hochladen</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Download</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Restore</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Upload</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Descargar</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Restaurar</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Subir</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Télécharger</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Restaurer</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Téléverser</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Scarica</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Ripristina</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Carica</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>ダウンロード</value>
</data>
<data name="Restore" xml:space="preserve">
<value>復元</value>
</data>
<data name="Upload" xml:space="preserve">
<value>アップロード</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Download</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Restore</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Upload</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Descarcă</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Restaurează</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Încarcă</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>Скачать</value>
</data>
<data name="Restore" xml:space="preserve">
<value>Восстановить</value>
</data>
<data name="Upload" xml:space="preserve">
<value>Загрузить</value>
</data>
@@ -144,6 +144,9 @@
<data name="Download" xml:space="preserve">
<value>下载</value>
</data>
<data name="Restore" xml:space="preserve">
<value>恢复</value>
</data>
<data name="Upload" xml:space="preserve">
<value>上传</value>
</data>
@@ -94,6 +94,16 @@ public sealed class ThemeService
await _apiHttpClient.DeleteAsync(ApiRoutes.Themes.DELETE_THEME.Replace("{themeId}", themeId), cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Restores a soft deleted bundled theme from its shipped archive.
/// </summary>
/// <param name="themeId">The identifier of the bundled theme to restore.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async Task RestoreThemeAsync(string themeId, CancellationToken cancellationToken = default)
{
await _apiHttpClient.PostAsync<ThemeResponseDto, object>(ApiRoutes.Themes.RESTORE_THEME.Replace("{themeId}", themeId), new { }, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Gets the theme and raw template source selected for a page key.
/// </summary>
@@ -134,6 +144,6 @@ public sealed class ThemeService
private static ThemeInfoDto ToThemeInfo(ThemeResponseDto theme)
{
string previewUrl = string.IsNullOrWhiteSpace(theme.PreviewPath) ? "/admin/theme-placeholder.svg" : $"/theme-assets/{theme.ThemeId}/{theme.PreviewPath}";
return new ThemeInfoDto(theme.ThemeId, theme.Name, theme.Description, theme.Author, theme.Version, previewUrl, theme.InstallSource == ThemeInstallSource.Bundled);
return new ThemeInfoDto(theme.ThemeId, theme.Name, theme.Description, theme.Author, theme.Version, previewUrl, theme.InstallSource == ThemeInstallSource.Bundled, theme.IsDeleted);
}
}
@@ -5,6 +5,7 @@
@using Lumina.Presentation.Web.Common.DTO.Themes
@using Lumina.Presentation.Web.Common.Services
@using Microsoft.AspNetCore.Mvc.Localization
@using System.Linq
@{
var themes = ViewData["themes"] as IReadOnlyList<ThemeInfoDto>;
@@ -14,7 +15,7 @@
int bundledThemeCount = 0;
if (themes is not null)
foreach (ThemeInfoDto theme in themes)
if (theme.IsBundled)
if (theme.IsBundled && !theme.IsDeleted)
bundledThemeCount++;
}
@@ -109,7 +110,7 @@
@foreach (ThemeInfoDto theme in themes)
{
bool isCurrent = theme.Id == currentThemeId;
bool isLastRemaining = themes.Count <= 1 || (theme.IsBundled && bundledThemeCount == 1);
bool isLastRemaining = themes.Count(theme => !theme.IsDeleted) <= 1 || (theme.IsBundled && bundledThemeCount == 1);
<tr data-theme-id="@theme.Id">
<td class="text-center">
<button type="button" class="theme-preview-button" data-preview-url="@theme.PreviewUrl" data-preview-title="@theme.Name" title="@_localizer["Preview"]">
@@ -122,9 +123,16 @@
<td>@(theme.IsBundled ? _localizer["Bundled"] : _localizer["UserProvided"])</td>
<td class="text-end">
<div class="form-row">
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
<button type="button" class="confirm-button download-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DOWNLOAD_THEME, new { themeId = theme.Id })">@_localizer["Download"]</button>
<button type="button" class="abort-button delete-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DELETE_THEME, new { themeId = theme.Id })" @(isLastRemaining ? "disabled" : null)>@_localizer["Delete"]</button>
@if (theme.IsDeleted)
{
<button type="button" class="confirm-button restore-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.RESTORE_THEME, new { themeId = theme.Id })">@_localizer["Restore"]</button>
}
else
{
<button type="button" class="confirm-button activate-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.SET_CURRENT_THEME)" @(isCurrent ? "disabled" : null)>@_localizer["Activate"]</button>
<button type="button" class="confirm-button download-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DOWNLOAD_THEME, new { themeId = theme.Id })">@_localizer["Download"]</button>
<button type="button" class="abort-button delete-theme-button" data-url="@_urlService.GetAbsoluteUrl(WebRoutes.Themes.DELETE_THEME, new { themeId = theme.Id })" @(isLastRemaining ? "disabled" : null)>@_localizer["Delete"]</button>
}
</div>
</td>
</tr>
@@ -171,6 +179,16 @@
}
}
// restores the soft deleted bundled theme of the clicked row
async function handleRestoreClick(event) {
const button = event.target.closest('.restore-theme-button');
if (!button)
return;
// an empty JSON body is sent so the antiforgery-protected POST request binds the route theme id without a JsonException
await callApiPostAsync(button.dataset.url, {});
reloadPage();
}
// downloads the archive of the theme of the clicked row
function handleDownloadClick(event) {
const button = event.target.closest('.download-theme-button');
@@ -227,6 +245,7 @@
document.addEventListener('click', handleActivateClick);
document.addEventListener('click', handleDownloadClick);
document.addEventListener('click', handleDeleteClick);
document.addEventListener('click', handleRestoreClick);
document.addEventListener('click', handlePreviewClick);
// register cleanup for these listeners
@@ -236,6 +255,7 @@
document.removeEventListener('click', handleActivateClick);
document.removeEventListener('click', handleDownloadClick);
document.removeEventListener('click', handleDeleteClick);
document.removeEventListener('click', handleRestoreClick);
document.removeEventListener('click', handlePreviewClick);
});