@page "/" @inject ILogger logger @using LibMatrix.Responses @using LibMatrix @using LibMatrix.Homeservers @using ArcaneLibs.Extensions @using MatrixUtils.Web.Pages.Dev Index

Rory&::MatrixUtils

Small collection of tools to do not-so-everyday things.

Signed in accounts - Add new account

@foreach (var session in _sessions.OrderByDescending(x => x.UserInfo.RoomCount)) { var _auth = session.UserAuth; }

Manage Remove Log out

@if (_offlineSessions.Count > 0) {

Sessions on unreachable servers

@foreach (var session in _offlineSessions) { }

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

Remove
} @code { #if DEBUG private const bool _debug = true; #else private const bool _debug = false; #endif private class AuthInfo { public UserAuth UserAuth { get; set; } public UserInfo UserInfo { get; set; } public ServerVersionResponse ServerVersion { get; set; } public AuthenticatedHomeserverGeneric Homeserver { get; set; } } // private Dictionary _users = new(); private readonly List _sessions = []; private readonly List _offlineSessions = []; private LoginResponse? _currentSession; protected override async Task OnInitializedAsync() { Console.WriteLine("Index.OnInitializedAsync"); _currentSession = await RMUStorage.GetCurrentToken(); _sessions.Clear(); _offlineSessions.Clear(); var tokens = await RMUStorage.GetAllTokens(); if (tokens is not { Count: > 0 }) { Console.WriteLine("No tokens found, trying migration from MRU..."); await RMUStorage.MigrateFromMRU(); tokens = await RMUStorage.GetAllTokens(); if (tokens is not { Count: > 0 }) { Console.WriteLine("No tokens found"); return; } } 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; NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken); return; } 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) ? "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(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(); } private class UserInfo { internal string AvatarUrl { get; set; } internal string DisplayName { get; set; } internal int RoomCount { get; set; } } private async Task RemoveUser(UserAuth auth, bool logout = false) { try { if (logout) { await (await hsProvider.GetAuthenticatedWithToken(auth.Homeserver, auth.AccessToken, auth.Proxy)).Logout(); } } catch (Exception e) { if (e is MatrixException { ErrorCode: "M_UNKNOWN_TOKEN" }) { //todo: handle this return; } 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 OnInitializedAsync(); } private async Task SwitchSession(UserAuth auth) { Console.WriteLine($"Switching to {auth.Homeserver} {auth.UserId} via {auth.Proxy}"); await RMUStorage.SetCurrentToken(auth); await OnInitializedAsync(); } private async Task ManageUser(UserAuth auth) { await SwitchSession(auth); NavigationManager.NavigateTo("/User/Profile"); } }