🐛 Activity / localization bug fixes

This commit is contained in:
2025-05-08 22:14:24 +08:00
parent 9b589af816
commit d70b081752
21 changed files with 2859 additions and 190 deletions

View File

@ -271,8 +271,8 @@ public class AccountController(
var today = SystemClock.Instance.GetCurrentInstant().InUtc().Date;
var localTime = new TimeOnly(0, 0);
var startOfDay = today.ToDateOnly().ToDateTime(localTime).ToInstant();
var endOfDay = today.PlusDays(1).ToDateOnly().ToDateTime(localTime).ToInstant();
var startOfDay = today.ToDateOnly().ToDateTime(localTime).ToUniversalTime().ToInstant();
var endOfDay = today.PlusDays(1).ToDateOnly().ToDateTime(localTime).ToUniversalTime().ToInstant();
var result = await db.AccountCheckInResults
.Where(x => x.AccountId == userId)
@ -294,12 +294,13 @@ public class AccountController(
return BadRequest("Check-in is not available for today.");
var needsCaptcha = events.CheckInDailyDoAskCaptcha(currentUser);
if (needsCaptcha && string.IsNullOrWhiteSpace(captchaToken))
return StatusCode(423, "Captcha is required for this check-in.");
if (!await auth.ValidateCaptcha(captchaToken!))
return BadRequest("Invalid captcha token.");
return await events.CheckInDaily(currentUser);
return needsCaptcha switch
{
true when string.IsNullOrWhiteSpace(captchaToken) => StatusCode(423,
"Captcha is required for this check-in."),
true when !await auth.ValidateCaptcha(captchaToken!) => BadRequest("Invalid captcha token."),
_ => await events.CheckInDaily(currentUser)
};
}
[HttpGet("search")]

View File

@ -120,9 +120,7 @@ public class AccountEventService(
public async Task<CheckInResult> CheckInDaily(Account user)
{
if (await CheckInDailyIsAvailable(user)) throw new InvalidOperationException("Check-in is not available");
var localizer = acc.GetEventLocalizer(user.Language);
var localizer = AccountService.GetEventLocalizer(user.Language);
// Generate 2 positive tips
var positiveIndices = Enumerable.Range(1, FortuneTipCount)
@ -137,6 +135,7 @@ public class AccountEventService(
// Generate 2 negative tips
var negativeIndices = Enumerable.Range(1, FortuneTipCount)
.Except(positiveIndices)
.OrderBy(_ => Random.Next())
.Take(2)
.ToList();

View File

@ -1,18 +1,24 @@
using System.Globalization;
using System.Reflection;
using DysonNetwork.Sphere.Localization;
using DysonNetwork.Sphere.Permission;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging.Abstractions;
namespace DysonNetwork.Sphere.Account;
public class AccountService(AppDatabase db, PermissionService pm, IMemoryCache cache, IStringLocalizerFactory localizerFactory)
public class AccountService(
AppDatabase db,
IMemoryCache cache,
IStringLocalizerFactory factory
)
{
public async Task PurgeAccountCache(Account account)
{
cache.Remove($"dyn_user_friends_{account.Id}");
var sessions = await db.AuthSessions.Where(e => e.Account.Id == account.Id).Select(e => e.Id)
.ToListAsync();
foreach (var session in sessions)
@ -34,9 +40,32 @@ public class AccountService(AppDatabase db, PermissionService pm, IMemoryCache c
return null;
}
public IStringLocalizer GetEventLocalizer(string language)
public static IStringLocalizer GetEventLocalizer(string language)
{
return localizerFactory.Create(language, nameof(AccountEventResource));
var culture = new CultureInfo(language, false);
var originalCulture = CultureInfo.CurrentCulture;
try
{
// Set the desired culture
CultureInfo.CurrentUICulture = culture;
CultureInfo.CurrentCulture = culture;
// Now create the localizer
var localizer = new ResourceManagerStringLocalizerFactory(
new Microsoft.Extensions.Options.OptionsWrapper<LocalizationOptions>(new LocalizationOptions
{ ResourcesPath = "Resources" }),
NullLoggerFactory.Instance
).Create(typeof(AccountEventResource));
return localizer;
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
CultureInfo.CurrentUICulture = originalCulture;
}
}
}