From e83895255ef5bf1376b6e9cfd7551ac21023e009 Mon Sep 17 00:00:00 2001 From: sam Date: Thu, 26 Sep 2024 17:09:27 +0200 Subject: [PATCH] fix(backend): return last_sid_reroll in API, update last sid reroll + last active correctly --- Foxnouns.Backend/Controllers/MembersController.cs | 9 +++++++-- Foxnouns.Backend/Controllers/UsersController.cs | 4 +++- Foxnouns.Backend/Services/UserRendererService.cs | 10 +++++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Foxnouns.Backend/Controllers/MembersController.cs b/Foxnouns.Backend/Controllers/MembersController.cs index 59bec37..44bb9ce 100644 --- a/Foxnouns.Backend/Controllers/MembersController.cs +++ b/Foxnouns.Backend/Controllers/MembersController.cs @@ -128,13 +128,18 @@ public class MembersController( if (CurrentUser!.LastSidReroll > minTimeAgo) throw new ApiError.BadRequest("Cannot reroll short ID yet"); + // Using ExecuteUpdateAsync here as the new short ID is generated by the database await db.Members.Where(m => m.Id == member.Id) .ExecuteUpdateAsync(s => s .SetProperty(m => m.Sid, _ => db.FindFreeMemberSid())); - // Re-fetch member so we can be sure the sid is correct - var updatedMember = await db.ResolveMemberAsync(CurrentUser!.Id, memberRef); + await db.Users.Where(u => u.Id == CurrentUser.Id) + .ExecuteUpdateAsync(s => s + .SetProperty(u => u.LastSidReroll, clock.GetCurrentInstant()) + .SetProperty(u => u.LastActive, clock.GetCurrentInstant())); + // Re-fetch member to fetch the new sid + var updatedMember = await db.ResolveMemberAsync(CurrentUser!.Id, memberRef); return Ok(memberRenderer.RenderMember(updatedMember, CurrentToken)); } } \ No newline at end of file diff --git a/Foxnouns.Backend/Controllers/UsersController.cs b/Foxnouns.Backend/Controllers/UsersController.cs index 7d1177e..fddd798 100644 --- a/Foxnouns.Backend/Controllers/UsersController.cs +++ b/Foxnouns.Backend/Controllers/UsersController.cs @@ -225,10 +225,12 @@ public class UsersController( if (CurrentUser!.LastSidReroll > minTimeAgo) throw new ApiError.BadRequest("Cannot reroll short ID yet"); + // Using ExecuteUpdateAsync here as the new short ID is generated by the database await db.Users.Where(u => u.Id == CurrentUser.Id) .ExecuteUpdateAsync(s => s .SetProperty(u => u.Sid, _ => db.FindFreeUserSid()) - .SetProperty(u => u.LastSidReroll, _ => clock.GetCurrentInstant())); + .SetProperty(u => u.LastSidReroll, clock.GetCurrentInstant()) + .SetProperty(u => u.LastActive, clock.GetCurrentInstant())); var user = await db.ResolveUserAsync(CurrentUser.Id); return Ok(await userRenderer.RenderUserAsync(user, CurrentUser, CurrentToken, renderMembers: false)); diff --git a/Foxnouns.Backend/Services/UserRendererService.cs b/Foxnouns.Backend/Services/UserRendererService.cs index 5a62a1a..0f8fe2e 100644 --- a/Foxnouns.Backend/Services/UserRendererService.cs +++ b/Foxnouns.Backend/Services/UserRendererService.cs @@ -37,7 +37,8 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe : []; return new UserResponse( - user.Id, user.Sid, user.Username, user.DisplayName, user.Bio, user.MemberTitle, AvatarUrlFor(user), user.Links, + user.Id, user.Sid, user.Username, user.DisplayName, user.Bio, user.MemberTitle, AvatarUrlFor(user), + user.Links, user.Names, user.Pronouns, user.Fields, user.CustomPreferences, renderMembers ? members.Select(m => memberRenderer.RenderPartialMember(m, tokenHidden)) : null, renderAuthMethods @@ -47,7 +48,8 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe )) : null, tokenHidden ? user.ListHidden : null, - tokenHidden ? user.LastActive : null + tokenHidden ? user.LastActive : null, + tokenHidden ? user.LastSidReroll : null ); } @@ -77,7 +79,9 @@ public class UserRendererService(DatabaseContext db, MemberRendererService membe [property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] bool? MemberListHidden, [property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] - Instant? LastActive + Instant? LastActive, + [property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + Instant? LastSidReroll ); public record AuthenticationMethodResponse(