From 9d534660afa6a738a113bf3d04e640fa96ae17f2 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 7 Jun 2025 22:55:47 +0800 Subject: [PATCH] :recycle: Better updating device name --- .../Account/AccountCurrentController.cs | 18 +++++++++++------- DysonNetwork.Sphere/Account/AccountService.cs | 14 ++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/DysonNetwork.Sphere/Account/AccountCurrentController.cs b/DysonNetwork.Sphere/Account/AccountCurrentController.cs index 04c8092..b0612d4 100644 --- a/DysonNetwork.Sphere/Account/AccountCurrentController.cs +++ b/DysonNetwork.Sphere/Account/AccountCurrentController.cs @@ -427,34 +427,38 @@ public class AccountCurrentController( public string? Label { get; set; } public string UserAgent { get; set; } = null!; public string DeviceId { get; set; } = null!; - public List Sessions { get; set; } = new(); + public ChallengePlatform Platform { get; set; } + public List Sessions { get; set; } = []; } [HttpGet("devices")] [Authorize] public async Task>> GetDevices() { - if (HttpContext.Items["CurrentUser"] is not Account currentUser) - { - return Unauthorized(); - } + if (HttpContext.Items["CurrentUser"] is not Account currentUser || + HttpContext.Items["CurrentSession"] is not Session currentSession) return Unauthorized(); + + Response.Headers.Append("X-Auth-Session", currentSession.Id.ToString()); // Group sessions by the related DeviceId, then create an AuthorizedDevice for each group. var deviceGroups = await db.AuthSessions .Where(s => s.Account.Id == currentUser.Id) - // Include the challenge if you need it to access DeviceId .Include(s => s.Challenge) .GroupBy(s => s.Challenge.DeviceId!) .Select(g => new AuthorizedDevice { DeviceId = g.Key!, UserAgent = g.First(x => x.Challenge.UserAgent != null).Challenge.UserAgent!, - Label = g.Where(x => string.IsNullOrWhiteSpace(x.Label)).Select(x => x.Label).FirstOrDefault(), + Platform = g.First().Challenge.Platform!, + Label = g.Where(x => !string.IsNullOrWhiteSpace(x.Label)).Select(x => x.Label).FirstOrDefault(), Sessions = g .OrderByDescending(x => x.LastGrantedAt) .ToList() }) .ToListAsync(); + deviceGroups = deviceGroups + .OrderByDescending(s => s.Sessions.First().LastGrantedAt) + .ToList(); return Ok(deviceGroups); } diff --git a/DysonNetwork.Sphere/Account/AccountService.cs b/DysonNetwork.Sphere/Account/AccountService.cs index 77c0a1c..769d6f7 100644 --- a/DysonNetwork.Sphere/Account/AccountService.cs +++ b/DysonNetwork.Sphere/Account/AccountService.cs @@ -328,11 +328,17 @@ public class AccountService( .Where(s => s.Id == sessionId && s.AccountId == account.Id) .FirstOrDefaultAsync(); if (session is null) throw new InvalidOperationException("Session was not found."); - - session.Label = label; - await db.SaveChangesAsync(); - await cache.RemoveAsync($"{DysonTokenAuthHandler.AuthCachePrefix}{session.Id}"); + await db.AuthChallenges + .Where(s => s.DeviceId == session.Challenge.DeviceId) + .ExecuteUpdateAsync(p => p.SetProperty(s => s.DeviceId, label)); + + var sessions = await db.AuthSessions + .Include(s => s.Challenge) + .Where(s => s.AccountId == session.Id && s.Challenge.DeviceId == session.Challenge.DeviceId) + .ToListAsync(); + foreach(var item in sessions) + await cache.RemoveAsync($"{DysonTokenAuthHandler.AuthCachePrefix}{item.Id}"); return session; }