From 7d9c3eb1d7a8bb913e6b7bfe224817ac575f76df Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 14 Apr 2025 23:42:38 +0200 Subject: Refactor session store (WIP) --- MatrixUtils.Web/Pages/Index.razor | 78 ++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 38 deletions(-) (limited to 'MatrixUtils.Web/Pages/Index.razor') diff --git a/MatrixUtils.Web/Pages/Index.razor b/MatrixUtils.Web/Pages/Index.razor index b9d3233..7103dc5 100644 --- a/MatrixUtils.Web/Pages/Index.razor +++ b/MatrixUtils.Web/Pages/Index.razor @@ -22,7 +22,7 @@ Small collection of tools to do not-so-everyday things.
@foreach (var session in _sessions.OrderByDescending(x => x.UserInfo.RoomCount)) { - var auth = session.UserAuth; + var auth = session.Auth; @@ -79,16 +80,16 @@ Small collection of tools to do not-so-everyday things. } @@ -108,19 +109,19 @@ Small collection of tools to do not-so-everyday things. } @@ -136,34 +137,34 @@ Small collection of tools to do not-so-everyday things. private const bool _debug = false; #endif - private class AuthInfo { - public UserAuth? UserAuth { get; set; } + private class HomepageSessionInfo : RmuSessionStore.SessionInfo { public UserInfo? UserInfo { get; set; } public ServerVersionResponse? ServerVersion { get; set; } public AuthenticatedHomeserverGeneric? Homeserver { get; set; } } - private readonly List _sessions = []; - private readonly List _offlineSessions = []; - private readonly List _invalidSessions = []; - private LoginResponse? _currentSession; + private readonly List _sessions = []; + private readonly List _offlineSessions = []; + private readonly List _invalidSessions = []; + private RmuSessionStore.SessionInfo? _currentSession; int scannedSessions, totalSessions = 1; private SvgIdenticonGenerator _identiconGenerator = new(); protected override async Task OnInitializedAsync() { Console.WriteLine("Index.OnInitializedAsync"); logger.LogDebug("Initialising index page"); - _currentSession = await RmuStorage.GetCurrentToken(); + await sessionStore.RunMigrations(); + + _currentSession = await sessionStore.GetCurrentSession(); _sessions.Clear(); _offlineSessions.Clear(); - var tokens = await RmuStorage.GetAllTokens(); + var tokens = await sessionStore.GetAllSessions(); scannedSessions = 0; totalSessions = tokens.Count; logger.LogDebug("Found {0} tokens", totalSessions); if (tokens is not { Count: > 0 }) { Console.WriteLine("No tokens found, trying migration from MRU..."); - await RmuStorage.MigrateFromMRU(); - tokens = await RmuStorage.GetAllTokens(); + tokens = await sessionStore.GetAllSessions(); if (tokens is not { Count: > 0 }) { Console.WriteLine("No tokens found"); return; @@ -173,8 +174,9 @@ Small collection of tools to do not-so-everyday things. List offlineServers = []; var sema = new SemaphoreSlim(8, 8); var updateSw = Stopwatch.StartNew(); - var tasks = tokens.Select(async token => { + var tasks = tokens.Select(async session => { await sema.WaitAsync(); + var token = session.Value.Auth; AuthenticatedHomeserverGeneric hs; try { @@ -197,14 +199,15 @@ Small collection of tools to do not-so-everyday things. var joinedRoomsTask = hs.GetJoinedRooms(); var profileTask = hs.GetProfileAsync(hs.WhoAmI.UserId); _sessions.Add(new() { + Auth = token, + SessionId = session.Value.SessionId, + Homeserver = hs, UserInfo = new() { AvatarUrl = (await profileTask).AvatarUrl, RoomCount = (await joinedRoomsTask).Count, DisplayName = (await profileTask).DisplayName ?? hs.WhoAmI.UserId }, - UserAuth = token, ServerVersion = await (serverVersionTask ?? Task.FromResult(null)!), - Homeserver = hs }); if (updateSw.ElapsedMilliseconds > 25) { updateSw.Restart(); @@ -214,7 +217,7 @@ Small collection of tools to do not-so-everyday things. catch (MatrixException e) { if (e is { ErrorCode: "M_UNKNOWN_TOKEN" }) { logger.LogWarning("Got unknown token error for {0} via {1}", token.UserId, token.Homeserver); - _invalidSessions.Add(token); + _invalidSessions.Add(session.Value); } else { logger.LogError("Failed to get info for {0} via {1}: {2}", token.UserId, token.Homeserver, e); @@ -248,9 +251,10 @@ Small collection of tools to do not-so-everyday things. internal int RoomCount { get; set; } } - private async Task RemoveUser(UserAuth auth, bool logout = false) { + private async Task RemoveUser(string sessionId, bool logout = false) { try { if (logout) { + var auth = (await sessionStore.GetSession(sessionId))?.Auth; await (await HsProvider.GetAuthenticatedWithToken(auth.Homeserver, auth.AccessToken, auth.Proxy)).Logout(); } } @@ -263,21 +267,19 @@ Small collection of tools to do not-so-everyday things. Console.WriteLine(e); } - await RmuStorage.RemoveToken(auth); - if ((await RmuStorage.GetCurrentToken())?.AccessToken == auth.AccessToken) - await RmuStorage.SetCurrentToken((await RmuStorage.GetAllTokens() ?? throw new InvalidOperationException()).FirstOrDefault()); + await sessionStore.RemoveSession(sessionId); StateHasChanged(); } - private async Task SwitchSession(UserAuth auth) { - Console.WriteLine($"Switching to {auth.Homeserver} {auth.UserId} via {auth.Proxy}"); - await RmuStorage.SetCurrentToken(auth); - _currentSession = auth; + private async Task SwitchSession(string sessionId) { + Console.WriteLine($"Switching to {sessionId}"); + await sessionStore.SetCurrentSession(sessionId); + _currentSession = await sessionStore.GetCurrentSession(); StateHasChanged(); } - private async Task ManageUser(UserAuth auth) { - await SwitchSession(auth); + private async Task ManageUser(string sessionId) { + await sessionStore.SetCurrentSession(sessionId); NavigationManager.NavigateTo("/User/Profile"); } } \ No newline at end of file -- cgit 1.5.1
@if (!string.IsNullOrWhiteSpace(@session.UserInfo?.AvatarUrl)) { @@ -36,7 +36,7 @@ Small collection of tools to do not-so-everyday things.

- Manage - Remove - Log out + Manage + Remove + Log out

@{ - string[] parts = session.UserId.Split(':'); + string[] parts = session.Auth.UserId.Split(':'); } @parts[0][1..] on @parts[1] - @if (!string.IsNullOrWhiteSpace(session.Proxy)) { - (proxied via @session.Proxy) + @if (!string.IsNullOrWhiteSpace(session.Auth.Proxy)) { + (proxied via @session.Auth.Proxy) }

- Remove + Remove

@{ - string[] parts = session.UserId.Split(':'); + string[] parts = session.Auth.UserId.Split(':'); } @parts[0][1..] on @parts[1] - @if (!string.IsNullOrWhiteSpace(session.Proxy)) { - (proxied via @session.Proxy) + @if (!string.IsNullOrWhiteSpace(session.Auth.Proxy)) { + (proxied via @session.Auth.Proxy) }

- Re-login + Re-login - Remove + Remove