Adds a theme caching toggle to the user settings

This commit is contained in:
BinaryAlley committed 2026-08-21 04:04:54 -07:00
1 parent 60b551558f
commit 3da7167538
43 files changed
+1611 -56

No files matched your search

@@ -3,9 +3,12 @@ using FastEndpoints;
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Common.Routes;
using Lumina.Presentation.Web.Common.Services;
using Lumina.Presentation.Web.Core.Endpoints.Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -18,14 +21,17 @@ namespace Lumina.Presentation.Web.Core.Endpoints.Tools.Settings.GetUserSettings;
public class GetUserSettingsEndpoint : BaseEndpoint<EmptyRequest, IResult>
{
private readonly IApiHttpClient _apiHttpClient;
private readonly ThemeCachePreferenceService _themeCachePreferenceService;
/// <summary>
/// Initializes a new instance of the <see cref="GetUserSettingsEndpoint"/> class.
/// </summary>
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
public GetUserSettingsEndpoint(IApiHttpClient apiHttpClient)
/// <param name="themeCachePreferenceService">Injected service for storing the per-user theme cache preference.</param>
public GetUserSettingsEndpoint(IApiHttpClient apiHttpClient, ThemeCachePreferenceService themeCachePreferenceService)
{
_apiHttpClient = apiHttpClient;
_themeCachePreferenceService = themeCachePreferenceService;
}
/// <summary>
@@ -48,6 +54,22 @@ public class GetUserSettingsEndpoint : BaseEndpoint<EmptyRequest, IResult>
public override async Task<IResult> ExecuteAsync(EmptyRequest request, CancellationToken cancellationToken)
{
UserSettingsDto response = await _apiHttpClient.GetAsync<UserSettingsDto>(ApiRoutes.Users.GET_USER_SETTINGS, cancellationToken).ConfigureAwait(false);
// refresh the per-user theme cache preference from the stored settings, so the handler always applies the latest value
Guid? userId = GetCurrentUserId();
if (userId is not null)
await _themeCachePreferenceService.SetAsync(userId.Value, response.IsThemeCachingEnabled, cancellationToken).ConfigureAwait(false);
return JsonSuccess(response);
}
/// <summary>
/// Gets the unique identifier of the currently authenticated user.
/// </summary>
/// <returns>The unique identifier of the current user, or <see langword="null"/> when the request is anonymous.</returns>
private Guid? GetCurrentUserId()
{
string? userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
return Guid.TryParse(userId, out Guid parsedUserId) ? parsedUserId : null;
}
}
@@ -3,9 +3,12 @@ using FastEndpoints;
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.DTO.UsersManagement;
using Lumina.Presentation.Web.Common.Routes;
using Lumina.Presentation.Web.Common.Services;
using Lumina.Presentation.Web.Core.Endpoints.Common;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -18,14 +21,17 @@ namespace Lumina.Presentation.Web.Core.Endpoints.Tools.Settings.UpdateUserSettin
public class UpdateUserSettingsEndpoint : BaseEndpoint<UserSettingsDto, IResult>
{
private readonly IApiHttpClient _apiHttpClient;
private readonly ThemeCachePreferenceService _themeCachePreferenceService;
/// <summary>
/// Initializes a new instance of the <see cref="UpdateUserSettingsEndpoint"/> class.
/// </summary>
/// <param name="apiHttpClient">Injected service for interactions with the API.</param>
public UpdateUserSettingsEndpoint(IApiHttpClient apiHttpClient)
/// <param name="themeCachePreferenceService">Injected service for storing the per-user theme cache preference.</param>
public UpdateUserSettingsEndpoint(IApiHttpClient apiHttpClient, ThemeCachePreferenceService themeCachePreferenceService)
{
_apiHttpClient = apiHttpClient;
_themeCachePreferenceService = themeCachePreferenceService;
}
/// <summary>
@@ -49,6 +55,22 @@ public class UpdateUserSettingsEndpoint : BaseEndpoint<UserSettingsDto, IResult>
public override async Task<IResult> ExecuteAsync(UserSettingsDto request, CancellationToken cancellationToken)
{
await _apiHttpClient.PutAsync<Web.Common.Requests.Common.EmptyRequest, UserSettingsDto>(ApiRoutes.Users.UPDATE_USER_SETTINGS, request, cancellationToken).ConfigureAwait(false);
// keep the per-user theme cache preference in sync with the stored settings, so the handler applies it immediately
Guid? userId = GetCurrentUserId();
if (userId is not null)
await _themeCachePreferenceService.SetAsync(userId.Value, request.IsThemeCachingEnabled, cancellationToken).ConfigureAwait(false);
return JsonSuccess(new { isUpdated = true });
}
/// <summary>
/// Gets the unique identifier of the currently authenticated user.
/// </summary>
/// <returns>The unique identifier of the current user, or <see langword="null"/> when the request is anonymous.</returns>
private Guid? GetCurrentUserId()
{
string? userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
return Guid.TryParse(userId, out Guid parsedUserId) ? parsedUserId : null;
}
}