@page "/" @using LibMatrix.Responses @using LibMatrix @using LibMatrix.Homeservers @using ArcaneLibs.Extensions 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 { private class AuthInfo { public UserAuth UserAuth { get; set; } public UserInfo UserInfo { get; set; } public ServerVersionResponse ServerVersion { 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); } catch (MatrixException e) { if (e.ErrorCode == "M_UNKNOWN_TOKEN") { NavigationManager.NavigateTo("/InvalidSession?ctx=" + token.AccessToken); return; } throw; } var roomCountTask = hs.GetJoinedRooms(); var profile = await hs.GetProfileAsync(hs.WhoAmI.UserId); userInfo.DisplayName = profile.DisplayName ?? hs.WhoAmI.UserId; Console.WriteLine(profile.ToJson()); userInfo.AvatarUrl = string.IsNullOrWhiteSpace(profile.AvatarUrl) ? "https://api.dicebear.com/6.x/identicon/svg?seed=" + hs.WhoAmI.UserId : hs.ResolveMediaUri(profile.AvatarUrl); userInfo.RoomCount = (await roomCountTask).Count; // _users.Add(token, userInfo); _auth.Add(new() { UserInfo = userInfo, UserAuth = token, ServerVersion = await hs.GetServerVersionAsync() }); // StateHasChanged(); }); 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)).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"); } }