about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 00:13:25 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-04 00:18:46 +0200
commitb933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1 (patch)
tree4176eb2b7f1befca685d00e119d842eb3f07da1f /MatrixRoomUtils.Web/Pages
parentSmall refactoring (diff)
downloadMatrixUtils-b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1.tar.xz
Refactoring
Diffstat (limited to 'MatrixRoomUtils.Web/Pages')
-rw-r--r--MatrixRoomUtils.Web/Pages/DataExportPage.razor5
-rw-r--r--MatrixRoomUtils.Web/Pages/Index.razor34
-rw-r--r--MatrixRoomUtils.Web/Pages/LoginPage.razor10
-rw-r--r--MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor5
-rw-r--r--MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor68
-rw-r--r--MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor6
-rw-r--r--MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor46
-rw-r--r--MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor10
-rw-r--r--MatrixRoomUtils.Web/Pages/UserImportPage.razor12
9 files changed, 77 insertions, 119 deletions
diff --git a/MatrixRoomUtils.Web/Pages/DataExportPage.razor b/MatrixRoomUtils.Web/Pages/DataExportPage.razor
index 5628d94..58d49fc 100644
--- a/MatrixRoomUtils.Web/Pages/DataExportPage.razor
+++ b/MatrixRoomUtils.Web/Pages/DataExportPage.razor
@@ -1,7 +1,8 @@
 @page "/export"
 @using MatrixRoomUtils.Web.Shared.IndexComponents
-@using MatrixRoomUtils.Authentication
 @using System.Text.Json
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Authentication
 @inject NavigationManager NavigationManager
 @inject ILocalStorageService LocalStorage
 
@@ -57,7 +58,7 @@ else
                     resolvedHomeservers++;
                     continue;
                 }
-                var resolvedHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(hs);
+                var resolvedHomeserver = (await new RemoteHomeServer(hs).Configure()).FullHomeServerDomain;
 
                 RuntimeCache.HomeserverResolutionCache.Add(hs, new() { Result = resolvedHomeserver, ResolutionTime = DateTime.Now });
                 await LocalStorageWrapper.SaveToLocalStorage(LocalStorage);
diff --git a/MatrixRoomUtils.Web/Pages/Index.razor b/MatrixRoomUtils.Web/Pages/Index.razor
index 8a5dcc4..67cefed 100644
--- a/MatrixRoomUtils.Web/Pages/Index.razor
+++ b/MatrixRoomUtils.Web/Pages/Index.razor
@@ -1,6 +1,7 @@
-    @page "/"
-    @using MatrixRoomUtils.Web.Shared.IndexComponents
-    @inject NavigationManager NavigationManager
+@page "/"
+@using MatrixRoomUtils.Web.Shared.IndexComponents
+@using MatrixRoomUtils.Core
+@inject NavigationManager NavigationManager
 @inject ILocalStorageService LocalStorage
 
 <PageTitle>Index</PageTitle>
@@ -11,20 +12,23 @@ Small collection of tools to do not-so-everyday things.
 <br/><br/>
 <h5>Signed in accounts - <a href="/Login">Add new account</a> or <a href="/ImportUsers">Import from TSV</a></h5>
 <hr/>
-@{
-    if (!RuntimeCache.WasLoaded)
-    {
-        Console.WriteLine("[INDEX] !!! LOCALSTORAGE WAS NOT LOADED !!!");
-        LocalStorageWrapper.LoadFromLocalStorage(LocalStorage).GetAwaiter().OnCompleted(() =>
-        {
-            Console.WriteLine("Users in cache: " + RuntimeCache .LoginSessions.Count);
-            StateHasChanged();
-        });
-    }
-}
 <form>
     @foreach (var (token, user) in RuntimeCache.LoginSessions)
     {
         <IndexUserItem User="@user"/>
     }
-</form>
\ No newline at end of file
+</form>
+
+@code
+{
+    protected override async Task OnInitializedAsync()
+    {
+        if (!RuntimeCache.WasLoaded)
+        {
+            Console.WriteLine("[INDEX] !!! LOCALSTORAGE WAS NOT LOADED !!!");
+            await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
+        }
+        await base.OnInitializedAsync();
+        await LocalStorageWrapper.ReloadLocalStorage(LocalStorage);
+    }
+}
\ No newline at end of file
diff --git a/MatrixRoomUtils.Web/Pages/LoginPage.razor b/MatrixRoomUtils.Web/Pages/LoginPage.razor
index da9345a..f318646 100644
--- a/MatrixRoomUtils.Web/Pages/LoginPage.razor
+++ b/MatrixRoomUtils.Web/Pages/LoginPage.razor
@@ -1,5 +1,6 @@
 @page "/Login"
-@using MatrixRoomUtils.Authentication
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Authentication
 @inject ILocalStorageService LocalStorage
 <h3>Login</h3>
 
@@ -27,17 +28,14 @@
         var result = await MatrixAuth.Login(homeserver, username, password);
         Console.WriteLine($"Obtained access token for {result.UserId}!");
 
-        RuntimeCache.AccessToken = result.AccessToken;
+        RuntimeCache.LastUsedToken = result.AccessToken;
 
         var userinfo = new UserInfo()
         {
             LoginResponse = result,
-            AccessToken = result.AccessToken,
-            Profile = await MatrixAuth.GetProfile(result.HomeServer, result.UserId)
+            Profile = await (await new RemoteHomeServer(result.HomeServer).Configure()).GetProfile(result.UserId)
         };
-    //TODO: refactor
         RuntimeCache.LoginSessions.Add(userinfo.AccessToken, userinfo);
-        RuntimeCache.CurrentHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(result.HomeServer);
 
         await LocalStorageWrapper.SaveToLocalStorage(LocalStorage);
     }
diff --git a/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor
index a411ccc..ec452f3 100644
--- a/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor
+++ b/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor
@@ -1,8 +1,9 @@
 @page "/PolicyListEditor/{RoomId}"
 @using System.Net.Http.Headers
 @using System.Text.Json
-@using MatrixRoomUtils.Extensions
-@using MatrixRoomUtils.StateEventTypes
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Extensions
+@using MatrixRoomUtils.Core.StateEventTypes
 @inject ILocalStorageService LocalStorage
 @inject NavigationManager NavigationManager
 <h3>Policy list editor</h3>
diff --git a/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor
index f1d26f1..924b68f 100644
--- a/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor
+++ b/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor
@@ -1,7 +1,8 @@
 @page "/PolicyListEditor"
 @using System.Net.Http.Headers
 @using System.Text.Json
-@using MatrixRoomUtils.Extensions
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Extensions
 @inject ILocalStorageService LocalStorage
 @inject NavigationManager NavigationManager
 <h3>Policy list editor</h3>
@@ -44,7 +45,7 @@ else
     {
         if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
         await base.OnInitializedAsync();
-        if (RuntimeCache.CurrentHomeServer != null)
+        if (RuntimeCache.CurrentHomeServer == null)
         {
             NavigationManager.NavigateTo("/Login");
             return;
@@ -70,7 +71,7 @@ else
 
         Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}");
         return;
-        /*
+    /*
         using HttpClient wc = new();
         wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken);
 
@@ -114,53 +115,28 @@ else
     {
         try
         {
-            //TODO: refactor!!!!!
+    //TODO: refactor!!!!!
             await semaphore.WaitAsync();
             PolicyRoomInfo roomInfo = new()
             {
                 RoomId = room
             };
-            using HttpClient wc = new();
-            wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
-            var sk = await wc.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/v3/rooms/{room}/state/org.matrix.mjolnir.shortcode");
-            if (sk.IsSuccessStatusCode)
-            {
-                var sko = await sk.Content.ReadFromJsonAsync<JsonElement>();
-                if (sko.TryGetProperty("shortcode", out JsonElement shortcode))
-                {
-                    Console.WriteLine($"Room {room} has a shortcode: {shortcode.GetString()}!");
-                    roomInfo.Shortcode = shortcode.GetString();
-                    // sk = await wc.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/rooms/{room}/state/m.room.name");
-                    // if (sk.IsSuccessStatusCode)
-                    // {
-                    //     Console.WriteLine($"Got content: {await sk.Content.ReadAsStringAsync()}");
-                    //     sko = await sk.Content.ReadFromJsonAsync<JsonElement>();
-                    //     if (sko.TryGetProperty("name", out JsonElement roomname))
-                    //     {
-                    //         Console.WriteLine($"Room {room} has a name: {roomname.GetString()}!");
-                    //         roomInfo.Name = roomname.GetString();
-                    //     }
-                    //     else Console.WriteLine("No record found...");
-                    // }
-                    // else if (sk.StatusCode == System.Net.HttpStatusCode.NotFound)
-                    // {
-                    // }
-                    // else Console.WriteLine($"Got failure while checking {room}: {sk.StatusCode} ({await sk.Content.ReadAsStringAsync()})...");
-                    var r = await RuntimeCache.CurrentHomeServer.GetRoom(room);
-                    roomInfo.Shortcode = (await r.GetStateAsync("org.matrix.mjolnir.shortcode")).Value.GetProperty("shortcode").GetString();
-                    roomInfo.Name = await r.GetNameAsync();
-                    return roomInfo;
-                }
-                else Console.WriteLine("No record found...");
-            }
-            else if (sk.StatusCode == System.Net.HttpStatusCode.NotFound)
+
+
+    // --- //
+            var r = await RuntimeCache.CurrentHomeServer.GetRoom(room);
+            roomInfo.Shortcode = (await r.GetStateAsync("org.matrix.mjolnir.shortcode")).Value.GetProperty("shortcode").GetString();
+
+            if (roomInfo.Shortcode != null)
             {
+                roomInfo.Name = await r.GetNameAsync();
+                return roomInfo;
             }
-            else Console.WriteLine($"Got failure while checking {room}: {sk.StatusCode} ({await sk.Content.ReadAsStringAsync()})...");
-            
+
             return null;
         }
         finally
+
         {
             checkedRoomCount++;
             StateHasChanged();
@@ -169,9 +145,15 @@ else
     }
 
     public struct PolicyRoomInfo
+
     {
-        public string RoomId { get; set; }
-        public string? Shortcode { get; set; }
-        public string? Name { get; set; }
+        public
+            string RoomId { get; set; }
+
+        public
+            string? Shortcode { get; set; }
+
+        public
+            string? Name { get; set; }
     }
     } 
\ No newline at end of file
diff --git a/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor b/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor
index e15ce20..47f2aba 100644
--- a/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor
+++ b/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor
@@ -1,6 +1,7 @@
 @page "/RoomStateViewer/{RoomId}/Edit"
 @using System.Net.Http.Headers
 @using System.Text.Json
+@using MatrixRoomUtils.Core
 @inject ILocalStorageService LocalStorage
 @inject NavigationManager NavigationManager
 <h3>Room state editor</h3>
@@ -65,8 +66,9 @@
     {
         int StateLoaded = 0;
         using var client = new HttpClient();
-        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
-        var response = await client.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state");
+        //TODO: can this be improved?
+        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.CurrentHomeServer.AccessToken);
+        var response = await client.GetAsync($"{RuntimeCache.CurrentHomeServer.FullHomeServerDomain}/_matrix/client/r0/rooms/{RoomId}/state");
     // var response = await client.GetAsync($"http://localhost:5117/matrix-hq-state.json");
     //var _events = await response.Content.ReadFromJsonAsync<Queue<StateEventStruct>>();
         var _data = await response.Content.ReadAsStreamAsync();
diff --git a/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor b/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor
index 6e846e9..92e7955 100644
--- a/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor
+++ b/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor
@@ -1,6 +1,7 @@
 @page "/RoomStateViewer"
 @using System.Net.Http.Headers
 @using System.Text.Json
+@using MatrixRoomUtils.Core
 @inject ILocalStorageService LocalStorage
 @inject NavigationManager NavigationManager
 <h3>Room state viewer</h3>
@@ -50,26 +51,9 @@ else
 
     private async Task EnumeratePolicyRooms()
     {
-        using HttpClient wc = new();
-        wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
+        var rooms = (await RuntimeCache.CurrentHomeServer.GetJoinedRooms()).Select(x=>x.RoomId).ToList();
 
-    //get room list
-    //temporary hack until rooms get enumerated...
-        string[] rooms = { "!fTjMjIzNKEsFlUIiru:neko.dev" };
-        var _rooms = await wc.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/v3/joined_rooms");
-        Console.WriteLine($"Got {_rooms.StatusCode}...");
-        if (!_rooms.IsSuccessStatusCode)
-        {
-            Console.WriteLine($"Failed to get rooms: {await _rooms.Content.ReadAsStringAsync()}");
-            return;
-        }
-        var _rooms_o = await _rooms.Content.ReadFromJsonAsync<JsonElement>();
-        if (_rooms_o.TryGetProperty("joined_rooms", out JsonElement _rooms_j))
-        {
-            rooms = _rooms_j.EnumerateArray().Select(x => x.GetString()).ToArray();
-        }
-
-        totalRoomCount = rooms.Length;
+        totalRoomCount = rooms.Count;
         StateHasChanged();
 
         var semaphore = new SemaphoreSlim(128);
@@ -89,29 +73,11 @@ else
         try
         {
             await semaphore.WaitAsync();
-            var roomInfo = new PolicyRoomInfo()
+            return new PolicyRoomInfo()
             {
-                RoomId = room
+                RoomId = room,
+                Name = await (await RuntimeCache.CurrentHomeServer.GetRoom(room)).GetNameAsync()
             };
-            using HttpClient wc = new();
-            wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
-            var sk = await wc.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/v3/rooms/{room}/state/m.room.name");
-            if (sk.IsSuccessStatusCode)
-            {
-                Console.WriteLine($"Got content: {await sk.Content.ReadAsStringAsync()}");
-                var sko = await sk.Content.ReadFromJsonAsync<JsonElement>();
-                if (sko.TryGetProperty("name", out JsonElement shortcode))
-                {
-                    Console.WriteLine($"Room {room} has a name: {shortcode.GetString()}!");
-                    roomInfo.Name = shortcode.GetString();
-                }
-                else Console.WriteLine("No record found...");
-            }
-            else if (sk.StatusCode == System.Net.HttpStatusCode.NotFound)
-            {
-            }
-            else Console.WriteLine($"Got failure while checking {room}: {sk.StatusCode} ({await sk.Content.ReadAsStringAsync()})...");
-            return roomInfo;
         }
         finally
         {
diff --git a/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor b/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor
index 72c5efa..199c75b 100644
--- a/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor
+++ b/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor
@@ -1,7 +1,8 @@
 @page "/RoomStateViewer/{RoomId}"
 @using System.Net.Http.Headers
 @using System.Text.Json
-@using MatrixRoomUtils.Extensions
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Extensions
 @inject ILocalStorageService LocalStorage
 @inject NavigationManager NavigationManager
 <h3>Room state viewer</h3>
@@ -76,7 +77,7 @@
     {
         if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
         await base.OnInitializedAsync();
-        if (RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null)
+        if (RuntimeCache.CurrentHomeServer == null)
         {
             NavigationManager.NavigateTo("/Login");
             return;
@@ -90,9 +91,10 @@
     private async Task LoadStatesAsync()
     {
         int StateLoaded = 0;
+        //TODO: can we improve this?
         using var client = new HttpClient();
-        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
-    var response = await client.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state");
+        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.CurrentHomeServer.AccessToken);
+    var response = await client.GetAsync($"{RuntimeCache.CurrentHomeServer.FullHomeServerDomain}/_matrix/client/r0/rooms/{RoomId}/state");
         // var response = await client.GetAsync($"http://localhost:5117/matrix-hq-state.json");
     //var _events = await response.Content.ReadFromJsonAsync<Queue<StateEventStruct>>();
         var _data = await response.Content.ReadAsStreamAsync();
diff --git a/MatrixRoomUtils.Web/Pages/UserImportPage.razor b/MatrixRoomUtils.Web/Pages/UserImportPage.razor
index 6b5eb77..97a1c44 100644
--- a/MatrixRoomUtils.Web/Pages/UserImportPage.razor
+++ b/MatrixRoomUtils.Web/Pages/UserImportPage.razor
@@ -1,10 +1,11 @@
 @page "/ImportUsers"
-@using MatrixRoomUtils.Authentication
 @using System.Text.Json
+@using MatrixRoomUtils.Core
+@using MatrixRoomUtils.Core.Authentication
 @inject ILocalStorageService LocalStorage
 <h3>Login</h3>
 
-<InputFile OnChange="@FileChanged"></InputFile>
+<InputFile OnChange="@FileChanged" accept=".tsv"></InputFile>
 <br/>
 <button @onclick="Login">Login</button>
 <br/><br/>
@@ -13,7 +14,7 @@
 <table border="1">
     @foreach (var (homeserver, username, password) in records)
     {
-        <tr style="background-color: @(LocalStorageWrapper.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}") ? "green" : "unset")">
+        <tr style="background-color: @(RuntimeCache.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}") ? "green" : "unset")">
             <td style="border-width: 1px;">@username</td>
             <td style="border-width: 1px;">@homeserver</td>
             <td style="border-width: 1px;">@password.Length chars</td>
@@ -31,7 +32,7 @@
     {
         foreach (var (homeserver, username, password) in records)
         {
-            if(LocalStorageWrapper.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}")) continue;
+            if(RuntimeCache.LoginSessions.Any(x => x.Value.LoginResponse.UserId == $"@{username}:{homeserver}")) continue;
             var result = await MatrixAuth.Login(homeserver, username, password);
             Console.WriteLine($"Obtained access token for {result.UserId}!");
 
@@ -40,8 +41,9 @@
                 LoginResponse = result
             };
             userinfo.Profile = await MatrixAuth.GetProfile(result.HomeServer, result.UserId);
+            RuntimeCache.LastUsedToken = result.AccessToken;
 
-            LocalStorageWrapper.LoginSessions.Add(result.AccessToken, userinfo);
+            RuntimeCache.LoginSessions.Add(result.AccessToken, userinfo);
             StateHasChanged();
         }