diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-03 18:40:53 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-03 18:40:53 +0200 |
commit | 3d3edeae16252a311704b390cfad6faa435a8b84 (patch) | |
tree | 34974194435fbe9789de5140ef9a9c0ddb834c74 /MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor | |
parent | Add policy room discovery ,add room state viewer (diff) | |
download | MatrixUtils-3d3edeae16252a311704b390cfad6faa435a8b84.tar.xz |
Refactor
Diffstat (limited to 'MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor')
-rw-r--r-- | MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor | 73 |
1 files changed, 53 insertions, 20 deletions
diff --git a/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor b/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor index 5a55b01..b13d069 100644 --- a/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor +++ b/MatrixRoomUtils.Web/Pages/RoomStateRoomList.razor @@ -9,7 +9,7 @@ @using MatrixRoomUtils.StateEventTypes @inject ILocalStorageService LocalStorage @inject NavigationManager NavigationManager -<h3>Policy list editor</h3> +<h3>Room state viewer</h3> <h5>Room list</h5> <hr/> @@ -26,7 +26,7 @@ else } foreach (var s in PolicyRoomList) { - <a href="@(NavigationManager.Uri + "/" + s.RoomId.Replace('.', '~'))">@s.Name</a> + <a href="@(NavigationManager.Uri + "/" + s.RoomId.Replace('.', '~'))">@s.Name (@s.RoomId)</a> <br/> } <div style="margin-bottom: 4em;"></div> @@ -43,9 +43,9 @@ else protected override async Task OnInitializedAsync() { - if (!RuntimeStorage.WasLoaded) await RuntimeStorage.LoadFromLocalStorage(LocalStorage); + if (!LocalStorageWrapper.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (RuntimeStorage.AccessToken == null || RuntimeStorage.CurrentHomeserver == null) + if (LocalStorageWrapper.AccessToken == null || LocalStorageWrapper.CurrentHomeserver == null) { NavigationManager.NavigateTo("/Login"); return; @@ -57,12 +57,12 @@ else private async Task EnumeratePolicyRooms() { using HttpClient wc = new(); - wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeStorage.AccessToken); + wc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); //get room list //temporary hack until rooms get enumerated... string[] rooms = { "!fTjMjIzNKEsFlUIiru:neko.dev" }; - var _rooms = await wc.GetAsync($"{RuntimeStorage.CurrentHomeserver}/_matrix/client/v3/joined_rooms"); + var _rooms = await wc.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/joined_rooms"); Console.WriteLine($"Got {_rooms.StatusCode}..."); if (!_rooms.IsSuccessStatusCode) { @@ -78,26 +78,59 @@ else totalRoomCount = rooms.Length; StateHasChanged(); - // var semaphore = new SemaphoreSlim(128); - // var tasks = new List<Task<PolicyRoomInfo?>>(); - // foreach (string room in rooms) - // { - // tasks.Add(GetPolicyRoomInfo(room, semaphore)); - // } - // var results = await Task.WhenAll(tasks); - // PolicyRoomList.AddRange(results.Where(x => x != null).Select(x=>x.Value)); - PolicyRoomList.AddRange(rooms.Select(x=>new PolicyRoomInfo() { Name = x, RoomId = x, Shortcode = "N/A" })); + var semaphore = new SemaphoreSlim(128); + var tasks = new List<Task<PolicyRoomInfo?>>(); + foreach (string room in rooms) + { + tasks.Add(GetPolicyRoomInfo(room, semaphore)); + } + var results = await Task.WhenAll(tasks); + PolicyRoomList.AddRange(results.Where(x => x != null).Select(x=>x.Value)); + StateHasChanged(); + } - //print to console - Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); + private async Task<PolicyRoomInfo?> GetPolicyRoomInfo(string room, SemaphoreSlim semaphore) + { + try + { + await semaphore.WaitAsync(); + var roomInfo = new PolicyRoomInfo() + { + 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"); + 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 + { + checkedRoomCount++; + StateHasChanged(); + semaphore.Release(); + } } - - + + public struct PolicyRoomInfo { public string RoomId { get; set; } - public string Shortcode { get; set; } public string Name { get; set; } } } \ No newline at end of file |