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

Rory&::MatrixUtils

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

Signed in accounts - Add new account

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

Manage Remove Log out

@code { #if DEBUG bool DEBUG = true; #else 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 List _auth = new(); protected override async Task OnInitializedAsync() { _currentSession = await MRUStorage.GetCurrentToken(); // _users.Clear(); _auth.Clear(); var tokens = await MRUStorage.GetAllTokens(); var profileTasks = tokens.Select(async token => { UserInfo userInfo = new(); AuthenticatedHomeserverGeneric hs; try { hs = await hsProvider.GetAuthenticatedWithToken(token.Homeserver, token.AccessToken, token.Proxy); } catch (MatrixException e) { if (e.ErrorCode == "M_UNKNOWN_TOKEN") { NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken); return; } throw; } catch (HttpRequestException e) { logger.LogError(e, $"Failed to instantiate AuthenticatedHomeserver for {token.ToJson()}, homeserver may be offline?", token.UserId); return; } var roomCountTask = hs.GetJoinedRooms(); var profile = await hs.GetProfileAsync(hs.WhoAmI.UserId); userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId; Console.WriteLine(profile.ToJson()); _auth.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.GetServerVersionAsync(), Homeserver = hs }); }); await Task.WhenAll(profileTasks); 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 MRUStorage.RemoveToken(auth); if ((await MRUStorage.GetCurrentToken())?.AccessToken == auth.AccessToken) await MRUStorage.SetCurrentToken((await MRUStorage.GetAllTokens() ?? throw new InvalidOperationException()).FirstOrDefault()); await OnInitializedAsync(); } private LoginResponse _currentSession; private async Task SwitchSession(UserAuth auth) { Console.WriteLine($"Switching to {auth.Homeserver} {auth.UserId} via {auth.Proxy}"); await MRUStorage.SetCurrentToken(auth); await OnInitializedAsync(); } private async Task ManageUser(UserAuth auth) { await SwitchSession(auth); NavigationManager.NavigateTo("/User/Profile"); } }