From 383f7b633471dedf515907cb8a8752bc5885ae64 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 4 May 2023 20:34:16 +0200 Subject: Add room manager, profile caching --- .gitignore | 2 +- .idea/.idea.MatrixRoomUtils/.idea/vcs.xml | 6 ++ MatrixRoomUtils.Core/Authentication/MatrixAuth.cs | 1 + MatrixRoomUtils.Core/Interfaces/IHomeServer.cs | 26 +++++++ MatrixRoomUtils.Core/RemoteHomeServer.cs | 9 +-- MatrixRoomUtils.Core/Room.cs | 3 +- MatrixRoomUtils.Web/Pages/DataExportPage.razor | 4 +- MatrixRoomUtils.Web/Pages/DebugTools.razor | 87 ++++++++++++++++++++++ MatrixRoomUtils.Web/Pages/Index.razor | 2 +- .../Pages/PolicyList/PolicyListRoomList.razor | 40 +--------- MatrixRoomUtils.Web/Pages/RoomManager.razor | 40 ++++++++++ MatrixRoomUtils.Web/Shared/MainLayout.razor | 16 +++- MatrixRoomUtils.Web/Shared/NavMenu.razor | 7 +- MatrixRoomUtils.Web/Shared/RoomListItem.razor | 48 ++++++++++++ deploy.sh | 6 ++ 15 files changed, 244 insertions(+), 53 deletions(-) create mode 100644 .idea/.idea.MatrixRoomUtils/.idea/vcs.xml create mode 100644 MatrixRoomUtils.Web/Pages/DebugTools.razor create mode 100644 MatrixRoomUtils.Web/Pages/RoomManager.razor diff --git a/.gitignore b/.gitignore index 4d672f6..142bc62 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ **/bin/ **/obj/ MatrixRoomUtils/ - +MatrixRoomUtils.Web/wwwroot/MRU.tar.xz diff --git a/.idea/.idea.MatrixRoomUtils/.idea/vcs.xml b/.idea/.idea.MatrixRoomUtils/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.MatrixRoomUtils/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs b/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs index e744c4f..7bcd75f 100644 --- a/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs +++ b/MatrixRoomUtils.Core/Authentication/MatrixAuth.cs @@ -39,6 +39,7 @@ public class MatrixAuth //return token; } + [Obsolete("Migrate to IHomeServer instance")] public static async Task GetProfile(string homeserver, string mxid) => await (await new RemoteHomeServer(homeserver).Configure()).GetProfile(mxid); diff --git a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs index 438709f..c6d788c 100644 --- a/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs +++ b/MatrixRoomUtils.Core/Interfaces/IHomeServer.cs @@ -1,11 +1,13 @@ using System.Net.Http.Json; using System.Text.Json; using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Responses; namespace MatrixRoomUtils.Core.Interfaces; public class IHomeServer { + private Dictionary _profileCache = new(); public string HomeServerDomain { get; set; } public string FullHomeServerDomain { get; set; } @@ -69,4 +71,28 @@ public class IHomeServer } throw new InvalidDataException($"Failed to resolve homeserver, not on {homeserver}, nor do client or server well-knowns exist!"); } + public async Task GetProfile(string mxid, bool debounce = false, bool cache = true) + { + if (cache) + { + if(debounce) await Task.Delay(Random.Shared.Next(100, 500)); + if (_profileCache.ContainsKey(mxid)) + { + while (_profileCache[mxid] == null) + { + Console.WriteLine($"Waiting for profile cache for {mxid}, currently {_profileCache[mxid]?.ToJson()} within {_profileCache.Count} profiles..."); + await Task.Delay(Random.Shared.Next(50, 500)); + } + return _profileCache[mxid]; + } + } + + _profileCache.Add(mxid, null); + var resp = await _httpClient.GetAsync($"/_matrix/client/r0/profile/{mxid}"); + var data = await resp.Content.ReadFromJsonAsync(); + if(!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data.ToString()); + var profile = data.Deserialize(); + _profileCache[mxid] = profile; + return profile; + } } \ No newline at end of file diff --git a/MatrixRoomUtils.Core/RemoteHomeServer.cs b/MatrixRoomUtils.Core/RemoteHomeServer.cs index 1acea89..9c096c8 100644 --- a/MatrixRoomUtils.Core/RemoteHomeServer.cs +++ b/MatrixRoomUtils.Core/RemoteHomeServer.cs @@ -7,6 +7,8 @@ namespace MatrixRoomUtils.Core; public class RemoteHomeServer : IHomeServer { + + public RemoteHomeServer(string canonicalHomeServerDomain) { HomeServerDomain = canonicalHomeServerDomain; @@ -21,13 +23,6 @@ public class RemoteHomeServer : IHomeServer return this; } - public async Task GetProfile(string mxid) - { - var resp = await _httpClient.GetAsync($"/_matrix/client/r0/profile/{mxid}"); - var data = await resp.Content.ReadFromJsonAsync(); - if(!resp.IsSuccessStatusCode) Console.WriteLine("Profile: " + data.ToString()); - return data.Deserialize(); - } public async Task GetRoom(string roomId) { diff --git a/MatrixRoomUtils.Core/Room.cs b/MatrixRoomUtils.Core/Room.cs index d5eee2b..be470fa 100644 --- a/MatrixRoomUtils.Core/Room.cs +++ b/MatrixRoomUtils.Core/Room.cs @@ -33,10 +33,11 @@ public class Room var res = await GetStateAsync("m.room.name"); if (!res.HasValue) { + Console.WriteLine($"Room {RoomId} has no name!"); return null; } var resn = res?.TryGetProperty("name", out var name) ?? false ? name.GetString() : null; - Console.WriteLine($"Got name: {resn}"); + //Console.WriteLine($"Got name: {resn}"); return resn; } diff --git a/MatrixRoomUtils.Web/Pages/DataExportPage.razor b/MatrixRoomUtils.Web/Pages/DataExportPage.razor index 62d093b..6e1b5ec 100644 --- a/MatrixRoomUtils.Web/Pages/DataExportPage.razor +++ b/MatrixRoomUtils.Web/Pages/DataExportPage.razor @@ -1,4 +1,4 @@ -@page "/export" +@page "/Export" @using MatrixRoomUtils.Web.Shared.IndexComponents @using System.Text.Json @inject NavigationManager NavigationManager @@ -6,7 +6,7 @@ Export -

Data export

+

Data export



Signed in accounts - Add new account or Import from TSV
diff --git a/MatrixRoomUtils.Web/Pages/DebugTools.razor b/MatrixRoomUtils.Web/Pages/DebugTools.razor new file mode 100644 index 0000000..ffa2134 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/DebugTools.razor @@ -0,0 +1,87 @@ +@page "/Debug" +@using MatrixRoomUtils.Core.Interfaces +@using MatrixRoomUtils.Core.Extensions +@using System.Reflection +@inject ILocalStorageService LocalStorage +@inject NavigationManager NavigationManager +

Debug Tools

+
+@if (Rooms.Count == 0) +{ +

You are not in any rooms!

+ @*

Loading progress: @checkedRoomCount/@totalRoomCount

*@ +} +else +{ +
+ Room List + @foreach (var room in Rooms) + { + + } +
+ +} + +
+ Send GET request to URL +
+ + +
+
+
@get_request_result
+
+ +
+ + +@code { + public List Rooms { get; set; } = new(); + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + await base.OnInitializedAsync(); + if (RuntimeCache.CurrentHomeServer == null) + { + NavigationManager.NavigateTo("/Login"); + return; + } + Rooms = (await RuntimeCache.CurrentHomeServer.GetJoinedRooms()).Select(x=>x.RoomId).ToList(); + Console.WriteLine("Fetched joined rooms!"); + } + + + //send req + string get_request_url { get; set; } = ""; + string get_request_result { get; set; } = ""; + private async Task SendGetRequest() + { + var field = typeof(IHomeServer).GetRuntimeFields().First(x => x.ToString().Contains("<_httpClient>k__BackingField")); + var httpClient = field.GetValue(RuntimeCache.CurrentHomeServer) as HttpClient; + try + { + var res = await httpClient.GetAsync(get_request_url); + if (res.IsSuccessStatusCode) + { + if(res.Content.Headers.ContentType.MediaType == "application/json") + get_request_result = (await res.Content.ReadFromJsonAsync()).ToJson(); + else + get_request_result = await res.Content.ReadAsStringAsync(); + StateHasChanged(); + return; + } + if(res.Content.Headers.ContentType.MediaType == "application/json") + get_request_result = $"Error: {res.StatusCode}\n" + (await res.Content.ReadFromJsonAsync()).ToJson(); + else + get_request_result = $"Error: {res.StatusCode}\n" + await res.Content.ReadAsStringAsync(); + + } + catch (Exception e) + { + get_request_result = $"Error: {e}"; + } + StateHasChanged(); + } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/Index.razor b/MatrixRoomUtils.Web/Pages/Index.razor index 5fb7e94..7be4149 100644 --- a/MatrixRoomUtils.Web/Pages/Index.razor +++ b/MatrixRoomUtils.Web/Pages/Index.razor @@ -5,7 +5,7 @@ Index -

Rory&::MatrixUtils

+

Rory&::MatrixUtils

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

diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor index 5656af9..e9d1be4 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor @@ -57,7 +57,7 @@ else totalRoomCount = xxxrooms.Count; StateHasChanged(); - var xxxsemaphore = new SemaphoreSlim(256); + var xxxsemaphore = new SemaphoreSlim(1000); var xxxtasks = new List>(); foreach (var room in xxxrooms) { @@ -68,44 +68,6 @@ else Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); return; - /* - using HttpClient wc = new(); - 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($"{LocalStorageWrapper.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(); - if (_rooms_o.TryGetProperty("joined_rooms", out JsonElement _rooms_j)) - { - rooms = _rooms_j.EnumerateArray().Select(x => x.GetString()).ToArray(); - } - - totalRoomCount = rooms.Length; - StateHasChanged(); - - var semaphore = new SemaphoreSlim(256); - var tasks = new List>(); - 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)); - - - //print to console - Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); - */ } private async Task GetPolicyRoomInfo(string room, SemaphoreSlim semaphore) diff --git a/MatrixRoomUtils.Web/Pages/RoomManager.razor b/MatrixRoomUtils.Web/Pages/RoomManager.razor new file mode 100644 index 0000000..deb6fd5 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/RoomManager.razor @@ -0,0 +1,40 @@ +@page "/RoomManager" +@inject ILocalStorageService LocalStorage +@inject NavigationManager NavigationManager +

Room manager

+
+@if (Rooms.Count == 0) +{ +

You are not in any rooms!

+ @*

Loading progress: @checkedRoomCount/@totalRoomCount

*@ +} +else +{ +
+ Room List + @foreach (var room in Rooms) + { + + } +
+ +} + +
+ + +@code { + public List Rooms { get; set; } = new(); + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + await base.OnInitializedAsync(); + if (RuntimeCache.CurrentHomeServer == null) + { + NavigationManager.NavigateTo("/Login"); + return; + } + Rooms = (await RuntimeCache.CurrentHomeServer.GetJoinedRooms()).Select(x=>x.RoomId).ToList(); + Console.WriteLine("Fetched joined rooms!"); + } +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/MainLayout.razor b/MatrixRoomUtils.Web/Shared/MainLayout.razor index 4aa01c6..87442d8 100644 --- a/MatrixRoomUtils.Web/Shared/MainLayout.razor +++ b/MatrixRoomUtils.Web/Shared/MainLayout.razor @@ -1,4 +1,6 @@ -@inherits LayoutComponentBase +@using MatrixRoomUtils.Core.Extensions +@using System.Net +@inherits LayoutComponentBase
@code { + private bool showDownload { get; set; } = false; + protected override async Task OnInitializedAsync() + { + using var hc = new HttpClient(); + var hr = await hc.SendAsync(new(HttpMethod.Head, NavigationManager.ToAbsoluteUri("/MRU.tar.xz").AbsoluteUri)); + showDownload = hr.StatusCode == HttpStatusCode.OK; + await base.OnInitializedAsync(); + } } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/NavMenu.razor b/MatrixRoomUtils.Web/Shared/NavMenu.razor index 5d80154..18ea33d 100644 --- a/MatrixRoomUtils.Web/Shared/NavMenu.razor +++ b/MatrixRoomUtils.Web/Shared/NavMenu.razor @@ -15,10 +15,15 @@
+