diff options
author | Rory& <root@rory.gay> | 2024-02-23 11:26:00 +0000 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-02-23 11:26:00 +0000 |
commit | e12117061631523aa136ff81638abdeb1fb23997 (patch) | |
tree | a04688939c6717b4b2e66cadd2939aa0de10f638 /MatrixUtils.Web/Pages/Index.razor | |
parent | New tools, fix room list items (diff) | |
download | MatrixUtils-e12117061631523aa136ff81638abdeb1fb23997.tar.xz |
HS emulator
Diffstat (limited to 'MatrixUtils.Web/Pages/Index.razor')
-rw-r--r-- | MatrixUtils.Web/Pages/Index.razor | 133 |
1 files changed, 95 insertions, 38 deletions
diff --git a/MatrixUtils.Web/Pages/Index.razor b/MatrixUtils.Web/Pages/Index.razor index e1cb60e..0c0c87a 100644 --- a/MatrixUtils.Web/Pages/Index.razor +++ b/MatrixUtils.Web/Pages/Index.razor @@ -2,9 +2,7 @@ @inject ILogger<Index> logger @using LibMatrix.Responses @using LibMatrix -@using LibMatrix.Homeservers @using ArcaneLibs.Extensions -@using MatrixUtils.Web.Pages.Dev <PageTitle>Index</PageTitle> @@ -12,7 +10,10 @@ Small collection of tools to do not-so-everyday things. <br/><br/> -<h5>Signed in accounts - <a href="/Login">Add new account</a></h5> +<h5>@totalSessions signed in sessions - <a href="/Login">Add new account</a></h5> +@if (scannedSessions != totalSessions) { + <progress max="@totalSessions" value="@scannedSessions"></progress> +} <hr/> <form> <table> @@ -91,16 +92,17 @@ Small collection of tools to do not-so-everyday things. #endif private class AuthInfo { - public UserAuth UserAuth { get; set; } - public UserInfo UserInfo { get; set; } - public ServerVersionResponse ServerVersion { get; set; } - public AuthenticatedHomeserverGeneric Homeserver { get; set; } + public UserAuth? UserAuth { get; set; } + public UserInfo? UserInfo { get; set; } + public ServerVersionResponse? ServerVersion { get; set; } + public AuthenticatedHomeserverGeneric? Homeserver { get; set; } } // private Dictionary<UserAuth, UserInfo> _users = new(); private readonly List<AuthInfo> _sessions = []; private readonly List<UserAuth> _offlineSessions = []; private LoginResponse? _currentSession; + int scannedSessions = 0, totalSessions = 1; protected override async Task OnInitializedAsync() { Console.WriteLine("Index.OnInitializedAsync"); @@ -108,6 +110,8 @@ Small collection of tools to do not-so-everyday things. _sessions.Clear(); _offlineSessions.Clear(); var tokens = await RMUStorage.GetAllTokens(); + scannedSessions = 0; + totalSessions = tokens.Count; if (tokens is not { Count: > 0 }) { Console.WriteLine("No tokens found, trying migration from MRU..."); await RMUStorage.MigrateFromMRU(); @@ -118,44 +122,94 @@ Small collection of tools to do not-so-everyday things. } } - var profileTasks = tokens.Select(async token => { - UserInfo userInfo = new(); + List<string> offlineServers = []; + var sema = new SemaphoreSlim(64, 64); + var tasks = tokens.Select(async token => { + await sema.WaitAsync(); + scannedSessions++; + if ((!string.IsNullOrWhiteSpace(token.Proxy) && offlineServers.Contains(token.Proxy)) || offlineServers.Contains(token.Homeserver)) { + _offlineSessions.Add(token); + sema.Release(); + return; + } + AuthenticatedHomeserverGeneric hs; - Console.WriteLine($"Getting hs for {token.ToJson()}"); try { hs = await hsProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken, token.Proxy); + var joinedRoomsTask = hs.GetJoinedRooms(); + var profileTask = hs.GetProfileAsync(hs.WhoAmI.UserId); + var serverVersionTask = hs.FederationClient?.GetServerVersionAsync(); + _sessions.Add(new() { + UserInfo = new() { + AvatarUrl = "/blobfox_outage.gif", + RoomCount = (await joinedRoomsTask).Count, + DisplayName = (await profileTask).DisplayName ?? hs.WhoAmI.UserId + }, + UserAuth = token, + ServerVersion = await (serverVersionTask ?? Task.FromResult<ServerVersionResponse?>(null)!), + Homeserver = hs + }); } catch (MatrixException e) { - if (e.ErrorCode != "M_UNKNOWN_TOKEN") throw; - NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken); - return; + if (e is { ErrorCode: "M_UNKNOWN_TOKEN" }) _offlineSessions.Add(token); + else throw; } - catch (Exception e) { - logger.LogError(e, $"Failed to instantiate AuthenticatedHomeserver for {token.ToJson()}, homeserver may be offline?", token.UserId); - _offlineSessions.Add(token); - return; + catch { + if (!string.IsNullOrWhiteSpace(token.Proxy)) { + offlineServers.Add(token.Proxy); + + sema.Release(); + return; + } + + offlineServers.Add(token.Homeserver); } - Console.WriteLine($"Got hs for {token.ToJson()}"); - - var roomCountTask = hs.GetJoinedRooms(); - var profile = await hs.GetProfileAsync(hs.WhoAmI.UserId); - userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId; - Console.WriteLine(profile.ToJson()); - _sessions.Add(new() { - UserInfo = new() { - AvatarUrl = string.IsNullOrWhiteSpace(profile.AvatarUrl) ? "https://api.dicebear.com/6.x/identicon/svg?seed=" + hs.WhoAmI.UserId : hs.ResolveMediaUri(profile.AvatarUrl), - RoomCount = (await roomCountTask).Count, - DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId - }, - UserAuth = token, - ServerVersion = await (hs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult<ServerVersionResponse?>(null)), - Homeserver = hs - }); + sema.Release(); + + StateHasChanged(); }).ToList(); - Console.WriteLine($"Waiting for {profileTasks.Count} profile tasks"); - await Task.WhenAll(profileTasks); - Console.WriteLine("Done waiting for profile tasks"); + await Task.WhenAll(tasks); + + // var profileTasks = tokens.Select(async token => { + // UserInfo userInfo = new(); + // AuthenticatedHomeserverGeneric hs; + // Console.WriteLine($"Getting hs for {token.ToJson()}"); + // try { + // hs = await hsProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken, token.Proxy); + // } + // catch (MatrixException e) { + // if (e.ErrorCode != "M_UNKNOWN_TOKEN") throw; + // _offlineSessions.Add(token); + // return; + // NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken); + // } + // catch (Exception e) { + // logger.LogError(e, $"Failed to instantiate AuthenticatedHomeserver for {token.ToJson()}, homeserver may be offline?", token.UserId); + // _offlineSessions.Add(token); + // return; + // } + // + // Console.WriteLine($"Got hs for {token.ToJson()}"); + // + // var roomCountTask = hs.GetJoinedRooms(); + // var profile = await hs.GetProfileAsync(hs.WhoAmI.UserId); + // userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId; + // Console.WriteLine(profile.ToJson()); + // _sessions.Add(new() { + // UserInfo = new() { + // AvatarUrl = string.IsNullOrWhiteSpace(profile.AvatarUrl) ? "/blobfox_outage.gif" : hs.ResolveMediaUri(profile.AvatarUrl), + // RoomCount = (await roomCountTask).Count, + // DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId + // }, + // UserAuth = token, + // ServerVersion = await (hs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult<ServerVersionResponse?>(null)), + // Homeserver = hs + // }); + // }).ToList(); + // Console.WriteLine($"Waiting for {profileTasks.Count} profile tasks"); + // await Task.WhenAll(profileTasks); + // Console.WriteLine("Done waiting for profile tasks"); await base.OnInitializedAsync(); } @@ -183,13 +237,16 @@ Small collection of tools to do not-so-everyday things. await RMUStorage.RemoveToken(auth); if ((await RMUStorage.GetCurrentToken())?.AccessToken == auth.AccessToken) await RMUStorage.SetCurrentToken((await RMUStorage.GetAllTokens() ?? throw new InvalidOperationException()).FirstOrDefault()); - await OnInitializedAsync(); + // await OnInitializedAsync(); + StateHasChanged(); } private async Task SwitchSession(UserAuth auth) { Console.WriteLine($"Switching to {auth.Homeserver} {auth.UserId} via {auth.Proxy}"); await RMUStorage.SetCurrentToken(auth); - await OnInitializedAsync(); + _currentSession = auth; + // await OnInitializedAsync(); + StateHasChanged(); } private async Task ManageUser(UserAuth auth) { |