[frontend/core] Update stored userdata on user select (ISH-705)

This commit is contained in:
Lilian 2025-02-09 14:32:22 +01:00
parent 13a25c78ff
commit 1b84d3b87e
No known key found for this signature in database
2 changed files with 48 additions and 12 deletions

View file

@ -1,5 +1,6 @@
using Blazored.LocalStorage; using Blazored.LocalStorage;
using Iceshrimp.Frontend.Core.Schemas; using Iceshrimp.Frontend.Core.Schemas;
using Iceshrimp.Shared.Schemas.Web;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop; using Microsoft.JSInterop;
@ -7,11 +8,14 @@ namespace Iceshrimp.Frontend.Core.Services;
internal class SessionService internal class SessionService
{ {
public SessionService(ApiService apiService, ISyncLocalStorageService localStorage, IJSRuntime js) public SessionService(
ApiService apiService, ISyncLocalStorageService localStorage, IJSRuntime js, ILogger<SessionService> logger
)
{ {
ApiService = apiService; ApiService = apiService;
LocalStorage = localStorage; LocalStorage = localStorage;
Js = js; Js = js;
Logger = logger;
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? []; Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
var lastUser = LocalStorage.GetItem<string?>("last_user"); var lastUser = LocalStorage.GetItem<string?>("last_user");
if (lastUser != null) if (lastUser != null)
@ -20,9 +24,10 @@ internal class SessionService
} }
} }
[Inject] public ISyncLocalStorageService LocalStorage { get; } private ISyncLocalStorageService LocalStorage { get; }
[Inject] public ApiService ApiService { get; } private ApiService ApiService { get; }
[Inject] public IJSRuntime Js { get; } private IJSRuntime Js { get; }
private ILogger<SessionService> Logger { get; }
public Dictionary<string, StoredUser> Users { get; } public Dictionary<string, StoredUser> Users { get; }
public StoredUser? Current { get; private set; } public StoredUser? Current { get; private set; }
@ -36,15 +41,45 @@ internal class SessionService
try try
{ {
Users.Add(user.Id, user); Users.Add(user.Id, user);
Logger.LogInformation($"User {user.Id} added.");
} }
catch (ArgumentException) // Update user if it already exists. catch (ArgumentException) // Update user if it already exists.
{ {
Users[user.Id] = user; Users[user.Id] = user;
Logger.LogInformation($"User {user.Id} updated.");
} }
WriteUsers(); WriteUsers();
} }
public async Task UpdateCurrentUserAsync()
{
if (Current == null) return;
var res = await ApiService.Auth.GetAuthStatusAsync();
if (res.Status is AuthStatusEnum.Authenticated)
{
var user = new StoredUser
{ // Token nor user will ever be null on an authenticated response
Id = res.User!.Id,
Username = res.User.Username,
DisplayName = res.User.DisplayName,
AvatarUrl = res.User.AvatarUrl,
BannerUrl = res.User.BannerUrl,
InstanceName = res.User.InstanceName,
InstanceIconUrl = res.User.InstanceIconUrl,
Token = res.Token!,
Host = res.User.Host,
IsAdmin = res.IsAdmin ?? false,
IsModerator = res.IsModerator ?? false,
Emojis = res.User.Emojis,
MovedTo = res.User.MovedTo
};
AddUser(user);
Current = user;
Logger.LogInformation($"Updated current user {user.Id}");
}
}
public void DeleteUser(string id) public void DeleteUser(string id)
{ {
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user."); if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");

View file

@ -209,9 +209,10 @@
} }
} }
private void LoginUser(StoredUser user) private async Task LoginUser(StoredUser user)
{ {
SessionService.SetSession(user.Id); SessionService.SetSession(user.Id);
await SessionService.UpdateCurrentUserAsync();
Navigation.NavigateTo(Uri.TryCreate(Redirect, UriKind.Relative, out _) ? Redirect : "/", true); Navigation.NavigateTo(Uri.TryCreate(Redirect, UriKind.Relative, out _) ? Redirect : "/", true);
} }