From c562f52538285d5f6ed0c184c0ea5018489fd067 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 25 May 2025 12:26:12 +0800 Subject: [PATCH] :bug: Trying to fix check in locking --- .../Account/AccountController.cs | 21 ++++++++++++------- .../Account/AccountEventService.cs | 5 +++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/DysonNetwork.Sphere/Account/AccountController.cs b/DysonNetwork.Sphere/Account/AccountController.cs index 6172457..09b1183 100644 --- a/DysonNetwork.Sphere/Account/AccountController.cs +++ b/DysonNetwork.Sphere/Account/AccountController.cs @@ -362,14 +362,21 @@ public class AccountController( if (!isAvailable) return BadRequest("Check-in is not available for today."); - var needsCaptcha = await events.CheckInDailyDoAskCaptcha(currentUser); - return needsCaptcha switch + try { - 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) - }; + var needsCaptcha = await events.CheckInDailyDoAskCaptcha(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) + }; + } + catch (InvalidOperationException ex) + { + return BadRequest(ex.Message); + } } [HttpGet("me/calendar")] diff --git a/DysonNetwork.Sphere/Account/AccountEventService.cs b/DysonNetwork.Sphere/Account/AccountEventService.cs index 985be3c..10d1eab 100644 --- a/DysonNetwork.Sphere/Account/AccountEventService.cs +++ b/DysonNetwork.Sphere/Account/AccountEventService.cs @@ -142,7 +142,8 @@ public class AccountEventService( public async Task CheckInDaily(Account user) { var lockKey = $"{CheckInLockKey}{user.Id}"; - var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(10), TimeSpan.Zero); + + await using var lk = await cache.AcquireLockAsync(lockKey, TimeSpan.FromMinutes(10), TimeSpan.Zero); if (lk is null) throw new InvalidOperationException("Check-in was in progress."); var cultureInfo = new CultureInfo(user.Language, false); @@ -212,7 +213,7 @@ public class AccountEventService( ActivityVisibility.Friends ); - await lk.ReleaseAsync(); + // The lock will be automatically released by the await using statement return result; }