diff options
-rw-r--r-- | MatrixRoomUtils.Core/Room.cs | 1 | ||||
-rw-r--r-- | MatrixRoomUtils.Core/RuntimeCache.cs | 1 | ||||
-rw-r--r-- | MatrixRoomUtils.Web.Server/Program.cs | 2 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/App.razor | 15 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs | 7 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/DataExportPage.razor | 17 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/Index.razor | 17 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/LoginPage.razor | 21 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor | 35 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor | 21 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor | 16 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor | 18 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor | 14 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Pages/UserImportPage.razor | 2 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor | 15 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/Shared/MainLayout.razor | 12 | ||||
-rw-r--r-- | MatrixRoomUtils.Web/_Imports.razor | 20 |
17 files changed, 106 insertions, 128 deletions
diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs index 6cb439a..2a5abb4 100644 --- a/MatrixRoomUtils.Core/Room.cs +++ b/MatrixRoomUtils.Core/Room.cs @@ -1,4 +1,3 @@ -using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; diff --git a/MatrixRoomUtils.Core/RuntimeCache.cs b/MatrixRoomUtils.Core/RuntimeCache.cs index 45262db..3e9eebc 100644 --- a/MatrixRoomUtils.Core/RuntimeCache.cs +++ b/MatrixRoomUtils.Core/RuntimeCache.cs @@ -20,6 +20,7 @@ public class UserInfo { public ProfileResponse Profile { get; set; } = new(); public LoginResponse LoginResponse { get; set; } + public string AccessToken { get; set; } } public class HomeServerResolutionResult diff --git a/MatrixRoomUtils.Web.Server/Program.cs b/MatrixRoomUtils.Web.Server/Program.cs index 9808111..a486226 100644 --- a/MatrixRoomUtils.Web.Server/Program.cs +++ b/MatrixRoomUtils.Web.Server/Program.cs @@ -1,5 +1,3 @@ -using Microsoft.AspNetCore.ResponseCompression; - var builder = WebApplication.CreateBuilder(args); // Add services to the container. diff --git a/MatrixRoomUtils.Web/App.razor b/MatrixRoomUtils.Web/App.razor index c7730d1..4e2789d 100644 --- a/MatrixRoomUtils.Web/App.razor +++ b/MatrixRoomUtils.Web/App.razor @@ -9,4 +9,17 @@ <p role="alert">Sorry, there's nothing at this address.</p> </LayoutView> </NotFound> -</Router> \ No newline at end of file +</Router> + +@code { + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) + { + await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + Console.WriteLine("Loaded from local storage"); + StateHasChanged(); + } + } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs index 462364c..229d937 100644 --- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs +++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs @@ -1,12 +1,15 @@ using Blazored.LocalStorage; -using MatrixRoomUtils.Authentication; -using MatrixRoomUtils.Responses; namespace MatrixRoomUtils.Web.Classes; public partial class LocalStorageWrapper { //some basic logic + public static async Task ReloadLocalStorage(ILocalStorageService localStorage) + { + await LoadFromLocalStorage(localStorage); + await SaveToLocalStorage(localStorage); + } public static async Task LoadFromLocalStorage(ILocalStorageService localStorage) { RuntimeCache.AccessToken = await localStorage.GetItemAsync<string>("rory.matrixroomutils.token"); diff --git a/MatrixRoomUtils.Web/Pages/DataExportPage.razor b/MatrixRoomUtils.Web/Pages/DataExportPage.razor index f9a4c37..5628d94 100644 --- a/MatrixRoomUtils.Web/Pages/DataExportPage.razor +++ b/MatrixRoomUtils.Web/Pages/DataExportPage.razor @@ -1,10 +1,7 @@ @page "/export" -@using MatrixRoomUtils.Web.Classes @using MatrixRoomUtils.Web.Shared.IndexComponents -@using Blazored.LocalStorage @using MatrixRoomUtils.Authentication @using System.Text.Json -@using Microsoft.AspNetCore.Components.Rendering @inject NavigationManager NavigationManager @inject ILocalStorageService LocalStorage @@ -19,7 +16,7 @@ { @foreach (var (token, user) in RuntimeCache.LoginSessions) { - <IndexUserItem User="@user" Token="@token"/> + <IndexUserItem User="@user"/> <pre> @user.LoginResponse.UserId[1..].Split(":")[0]\auth\access_token=@token @user.LoginResponse.UserId[1..].Split(":")[0]\auth\device_id=@user.LoginResponse.DeviceId @@ -55,16 +52,18 @@ else StateHasChanged(); foreach (var hs in homeservers) { - if (LocalStorageWrapper.HomeserverResolutionCache.ContainsKey(hs)) continue; + if (RuntimeCache.HomeserverResolutionCache.ContainsKey(hs)) + { + resolvedHomeservers++; + continue; + } var resolvedHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(hs); - if (LocalStorageWrapper.HomeserverResolutionCache.ContainsKey(hs)) - LocalStorageWrapper.HomeserverResolutionCache.Remove(hs); - LocalStorageWrapper.HomeserverResolutionCache.Add(hs, new() { Result = resolvedHomeserver, ResolutionTime = DateTime.Now }); + RuntimeCache.HomeserverResolutionCache.Add(hs, new() { Result = resolvedHomeserver, ResolutionTime = DateTime.Now }); await LocalStorageWrapper.SaveToLocalStorage(LocalStorage); Console.WriteLine("Saved to local storage:"); - Console.WriteLine(JsonSerializer.Serialize(LocalStorageWrapper.HomeserverResolutionCache, new JsonSerializerOptions() + Console.WriteLine(JsonSerializer.Serialize(RuntimeCache.HomeserverResolutionCache, new JsonSerializerOptions() { WriteIndented = true })); diff --git a/MatrixRoomUtils.Web/Pages/Index.razor b/MatrixRoomUtils.Web/Pages/Index.razor index ea9404d..8a5dcc4 100644 --- a/MatrixRoomUtils.Web/Pages/Index.razor +++ b/MatrixRoomUtils.Web/Pages/Index.razor @@ -1,8 +1,6 @@ -@page "/" -@using MatrixRoomUtils.Web.Classes -@using MatrixRoomUtils.Web.Shared.IndexComponents -@using Blazored.LocalStorage -@inject NavigationManager NavigationManager + @page "/" + @using MatrixRoomUtils.Web.Shared.IndexComponents + @inject NavigationManager NavigationManager @inject ILocalStorageService LocalStorage <PageTitle>Index</PageTitle> @@ -14,20 +12,19 @@ Small collection of tools to do not-so-everyday things. <h5>Signed in accounts - <a href="/Login">Add new account</a> or <a href="/ImportUsers">Import from TSV</a></h5> <hr/> @{ - Console.WriteLine("Users in cache: " + LocalStorageWrapper.LoginSessions.Count); - if (!LocalStorageWrapper.WasLoaded) + if (!RuntimeCache.WasLoaded) { Console.WriteLine("[INDEX] !!! LOCALSTORAGE WAS NOT LOADED !!!"); LocalStorageWrapper.LoadFromLocalStorage(LocalStorage).GetAwaiter().OnCompleted(() => { - Console.WriteLine("Users in cache: " + LocalStorageWrapper.LoginSessions.Count); + Console.WriteLine("Users in cache: " + RuntimeCache .LoginSessions.Count); StateHasChanged(); }); } } <form> - @foreach (var (token, user) in LocalStorageWrapper.LoginSessions) + @foreach (var (token, user) in RuntimeCache.LoginSessions) { - <IndexUserItem User="@user" Token="@token"/> + <IndexUserItem User="@user"/> } </form> \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/LoginPage.razor b/MatrixRoomUtils.Web/Pages/LoginPage.razor index aead5e8..da9345a 100644 --- a/MatrixRoomUtils.Web/Pages/LoginPage.razor +++ b/MatrixRoomUtils.Web/Pages/LoginPage.razor @@ -1,7 +1,5 @@ @page "/Login" @using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @inject ILocalStorageService LocalStorage <h3>Login</h3> @@ -23,24 +21,25 @@ string homeserver = ""; string username = ""; string password = ""; + async Task Login() { var result = await MatrixAuth.Login(homeserver, username, password); Console.WriteLine($"Obtained access token for {result.UserId}!"); - - LocalStorageWrapper.AccessToken = result.AccessToken; + + RuntimeCache.AccessToken = result.AccessToken; var userinfo = new UserInfo() { - LoginResponse = result + LoginResponse = result, + AccessToken = result.AccessToken, + Profile = await MatrixAuth.GetProfile(result.HomeServer, result.UserId) }; - userinfo.Profile = await MatrixAuth.GetProfile(result.HomeServer, result.UserId); - - LocalStorageWrapper.LoginSessions.Add(result.AccessToken, userinfo); - LocalStorageWrapper.CurrentHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(result.HomeServer); - + //TODO: refactor + RuntimeCache.LoginSessions.Add(userinfo.AccessToken, userinfo); + RuntimeCache.CurrentHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(result.HomeServer); await LocalStorageWrapper.SaveToLocalStorage(LocalStorage); - } + } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor index 6c276b6..a411ccc 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyListEditorPage.razor @@ -1,7 +1,4 @@ @page "/PolicyListEditor/{RoomId}" -@using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Net.Http.Headers @using System.Text.Json @using MatrixRoomUtils.Extensions @@ -146,7 +143,7 @@ else @foreach (var policyEvent in PolicyEvents.Where(x => x.type == "m.policy.rule.user" && x.content.Entity != null)) { <tr> - <td style="word-wrap: anywhere;">Entity: @string.Join("", policyEvent.content.Entity.Take(64))<br/>State: @string.Join("",policyEvent.state_key.Take(64))</td> + <td style="word-wrap: anywhere;">Entity: @string.Join("", policyEvent.content.Entity.Take(64))<br/>State: @string.Join("", policyEvent.state_key.Take(64))</td> <td>@policyEvent.content.Reason</td> <td> @policyEvent.content.ExpiryDateTime @@ -195,9 +192,10 @@ else protected override async Task OnInitializedAsync() { - if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if(LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) + // if(RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null) + if (RuntimeCache.CurrentHomeServer == null) { NavigationManager.NavigateTo("/Login"); return; @@ -209,24 +207,17 @@ else private async Task LoadStatesAsync() { - using var client = new HttpClient(); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state"); - var content = await response.Content.ReadAsStringAsync(); - // Console.WriteLine(JsonSerializer.Deserialize<object>(content).ToJson()); - var stateEvents = JsonSerializer.Deserialize<List<StateEvent>>(content); + // using var client = new HttpClient(); + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); + // var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state"); + // var content = await response.Content.ReadAsStringAsync(); + // Console.WriteLine(JsonSerializer.Deserialize<object>(content).ToJson()); + // var stateEvents = JsonSerializer.Deserialize<List<StateEvent>>(content); + var room = await RuntimeCache.CurrentHomeServer.GetRoom(RoomId); + var stateEvents = (await room.GetStateAsync(""))!.Value.Deserialize<List<StateEvent>>(); PolicyEvents = stateEvents.Where(x => x.type.StartsWith("m.policy.rule")) .Select(x => JsonSerializer.Deserialize<StateEvent<PolicyRuleStateEventData>>(JsonSerializer.Serialize(x))).ToList(); StateHasChanged(); - // foreach (var stateEvent in PolicyEvents.Where(x => x.replaces_state != "" && x.replaces_state != null)) - // { - // Console.WriteLine($"{stateEvent.replaces_state} -> {PolicyEvents.Any(x => x.state_key == stateEvent.replaces_state)}"); - // } - // foreach (var policyEvent in PolicyEvents) - // { - // Console.WriteLine(policyEvent.ToJson()); - // } } -} - +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor index 032d150..f1d26f1 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyListRoomList.razor @@ -1,12 +1,7 @@ @page "/PolicyListEditor" -@using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Net.Http.Headers @using System.Text.Json -@using System.Xml.Schema @using MatrixRoomUtils.Extensions -@using MatrixRoomUtils.StateEventTypes @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager <h3>Policy list editor</h3> @@ -47,9 +42,9 @@ else protected override async Task OnInitializedAsync() { - if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) + if (RuntimeCache.CurrentHomeServer != null) { NavigationManager.NavigateTo("/Login"); return; @@ -60,7 +55,7 @@ else private async Task EnumeratePolicyRooms() { - var xxxrooms = await LocalStorageWrapper.CurrentHomeServer.GetJoinedRooms(); + var xxxrooms = await RuntimeCache.CurrentHomeServer.GetJoinedRooms(); totalRoomCount = xxxrooms.Count; StateHasChanged(); @@ -75,7 +70,7 @@ else Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); return; - + /* using HttpClient wc = new(); wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); @@ -112,20 +107,22 @@ else //print to console Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); + */ } private async Task<PolicyRoomInfo?> GetPolicyRoomInfo(string room, SemaphoreSlim semaphore) { try { + //TODO: refactor!!!!! await semaphore.WaitAsync(); PolicyRoomInfo roomInfo = new() { RoomId = room }; using HttpClient wc = new(); - wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - var sk = await wc.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/rooms/{room}/state/org.matrix.mjolnir.shortcode"); + 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>(); @@ -149,7 +146,7 @@ else // { // } // else Console.WriteLine($"Got failure while checking {room}: {sk.StatusCode} ({await sk.Content.ReadAsStringAsync()})..."); - var r = await LocalStorageWrapper.CurrentHomeServer.GetRoom(room); + 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; diff --git a/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor b/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor index 6ad99d9..e15ce20 100644 --- a/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor +++ b/MatrixRoomUtils.Web/Pages/RoomStateEditorPage.razor @@ -1,14 +1,6 @@ @page "/RoomStateViewer/{RoomId}/Edit" -@using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Net.Http.Headers @using System.Text.Json -@using System.Xml.Schema -@using MatrixRoomUtils.Extensions -@using MatrixRoomUtils.StateEventTypes -@using MatrixRoomUtils.Web.Shared.IndexComponents -@using Microsoft.Win32.SafeHandles @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager <h3>Room state editor</h3> @@ -55,9 +47,9 @@ protected override async Task OnInitializedAsync() { - if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) + if (RuntimeCache.CurrentHomeServer != null) { NavigationManager.NavigateTo("/Login"); return; @@ -73,8 +65,8 @@ { int StateLoaded = 0; using var client = new HttpClient(); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state"); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken); + var response = await client.GetAsync($"{RuntimeCache.CurrentHomeserver}/_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 b13d069..6e846e9 100644 --- a/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor +++ b/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor @@ -1,12 +1,6 @@ @page "/RoomStateViewer" -@using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Net.Http.Headers @using System.Text.Json -@using System.Xml.Schema -@using MatrixRoomUtils.Extensions -@using MatrixRoomUtils.StateEventTypes @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager <h3>Room state viewer</h3> @@ -43,9 +37,9 @@ else protected override async Task OnInitializedAsync() { - if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) + if (RuntimeCache.CurrentHomeServer == null) { NavigationManager.NavigateTo("/Login"); return; @@ -57,12 +51,12 @@ else private async Task EnumeratePolicyRooms() { using HttpClient wc = new(); - wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); + wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken); //get room list //temporary hack until rooms get enumerated... string[] rooms = { "!fTjMjIzNKEsFlUIiru:neko.dev" }; - var _rooms = await wc.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/joined_rooms"); + var _rooms = await wc.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/v3/joined_rooms"); Console.WriteLine($"Got {_rooms.StatusCode}..."); if (!_rooms.IsSuccessStatusCode) { @@ -100,8 +94,8 @@ else RoomId = room }; using HttpClient wc = new(); - wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - var sk = await wc.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/rooms/{room}/state/m.room.name"); + 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()}"); diff --git a/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor b/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor index 8c33d89..72c5efa 100644 --- a/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor +++ b/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor @@ -1,13 +1,7 @@ @page "/RoomStateViewer/{RoomId}" -@using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Net.Http.Headers @using System.Text.Json -@using System.Xml.Schema @using MatrixRoomUtils.Extensions -@using MatrixRoomUtils.StateEventTypes -@using MatrixRoomUtils.Web.Shared.IndexComponents @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager <h3>Room state viewer</h3> @@ -80,9 +74,9 @@ protected override async Task OnInitializedAsync() { - if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) + if (RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null) { NavigationManager.NavigateTo("/Login"); return; @@ -97,8 +91,8 @@ { int StateLoaded = 0; using var client = new HttpClient(); - client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state"); + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken); + var response = await client.GetAsync($"{RuntimeCache.CurrentHomeserver}/_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 612b3ce..6b5eb77 100644 --- a/MatrixRoomUtils.Web/Pages/UserImportPage.razor +++ b/MatrixRoomUtils.Web/Pages/UserImportPage.razor @@ -1,7 +1,5 @@ @page "/ImportUsers" @using MatrixRoomUtils.Authentication -@using MatrixRoomUtils.Web.Classes -@using Blazored.LocalStorage @using System.Text.Json @inject ILocalStorageService LocalStorage <h3>Login</h3> diff --git a/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor b/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor index 33054e5..d0fb2f0 100644 --- a/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor +++ b/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor @@ -9,14 +9,12 @@ <div style="margin-bottom: 1em;"> <img style="border-radius: 50%; height: 3em; width: 3em;" src="@_avatarUrl"/> - <span style="margin-left: 1em;"><input type="radio" name="csa" checked="@(LocalStorageWrapper.AccessToken == Token)" onclick="@SetCurrent" style="text-decoration-line: unset;"/> <b>@User.Profile.DisplayName</b> on <b>@User.LoginResponse.HomeServer</b></span> + <span style="margin-left: 1em;"><input type="radio" name="csa" checked="@(RuntimeCache.AccessToken == User.AccessToken)" onclick="@SetCurrent" style="text-decoration-line: unset;"/> <b>@User.Profile.DisplayName</b> on <b>@User.LoginResponse.HomeServer</b></span> <a href="#" onclick="@RemoveUser">Remove</a> </div> @code { [Parameter] - public string Token { get; set; } - [Parameter] public UserInfo User { get; set; } private string _avatarUrl { get; set; } @@ -32,18 +30,17 @@ private async Task RemoveUser() { - LocalStorageWrapper.LoginSessions.Remove(Token); - await LocalStorageWrapper.SaveToLocalStorage(LocalStorage); + RuntimeCache.LoginSessions.Remove(User.AccessToken); + await LocalStorageWrapper.ReloadLocalStorage(LocalStorage); _removed = true; StateHasChanged(); } private async Task SetCurrent() { - LocalStorageWrapper.AccessToken = Token; - LocalStorageWrapper.CurrentHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(LocalStorageWrapper.LoginSessions[Token].LoginResponse.HomeServer); - await LocalStorageWrapper.SaveToLocalStorage(LocalStorage); - await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + RuntimeCache.AccessToken = User.AccessToken; + //RuntimeCache.CurrentHomeserver = await MatrixAuth.ResolveHomeserverFromWellKnown(LocalStorageWrapper.LoginSessions[Token].LoginResponse.HomeServer); + await LocalStorageWrapper.ReloadLocalStorage(LocalStorage); StateHasChanged(); } diff --git a/MatrixRoomUtils.Web/Shared/MainLayout.razor b/MatrixRoomUtils.Web/Shared/MainLayout.razor index ae7c3f5..055cec7 100644 --- a/MatrixRoomUtils.Web/Shared/MainLayout.razor +++ b/MatrixRoomUtils.Web/Shared/MainLayout.razor @@ -1,7 +1,4 @@ @inherits LayoutComponentBase - -@using Blazored.LocalStorage -@using MatrixRoomUtils.Web.Classes @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager @@ -23,14 +20,5 @@ @code { - protected override async Task OnInitializedAsync() - { - if (!LocalStorageWrapper.WasLoaded) - { - await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); - Console.WriteLine("Loaded from local storage"); - StateHasChanged(); - } - } } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/_Imports.razor b/MatrixRoomUtils.Web/_Imports.razor index c675706..dd81444 100644 --- a/MatrixRoomUtils.Web/_Imports.razor +++ b/MatrixRoomUtils.Web/_Imports.razor @@ -1,5 +1,6 @@ @using System.Net.Http @using System.Net.Http.Json +@using Blazored.LocalStorage @using Microsoft.AspNetCore.Components.Forms @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.Web @@ -7,4 +8,21 @@ @using Microsoft.AspNetCore.Components.WebAssembly.Http @using Microsoft.JSInterop @using MatrixRoomUtils.Web -@using MatrixRoomUtils.Web.Shared \ No newline at end of file +@using MatrixRoomUtils.Web.Classes +@using MatrixRoomUtils.Web.Shared + +@inject ILocalStorageService LocalStorage + +@code +{ + + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) + { + await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + Console.WriteLine("Loaded from local storage"); + StateHasChanged(); + } + } +} |