From fc749b3e57098740377e6eabd5d010d133256fa5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Tue, 13 Jun 2023 01:49:10 +0200 Subject: Improved many features --- MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs | 1 + MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj | 4 + MatrixRoomUtils.Web/Pages/DevOptions.razor | 7 +- MatrixRoomUtils.Web/Pages/HSAdmin/RoomQuery.razor | 86 ++++++++++++++++++++++ .../Pages/PolicyList/PolicyListEditorPage.razor | 2 +- .../Pages/RoomManager/RoomManager.razor | 1 - .../Pages/RoomManager/RoomManagerSpace.razor | 13 +++- .../Pages/RoomManager/RoomManagerTimeline.razor | 59 +++++++++++++++ .../Shared/IndexComponents/IndexUserItem.razor | 2 +- MatrixRoomUtils.Web/Shared/InlineUserItem.razor | 63 ++++++++++++++++ MatrixRoomUtils.Web/Shared/RoomListItem.razor | 27 +++---- MatrixRoomUtils.Web/Shared/RoomListItem.razor.css | 10 +++ .../TimelineComponents/TimelineMemberItem.razor | 42 +++++++++++ .../TimelineComponents/TimelineMessageItem.razor | 11 +++ .../TimelineComponents/TimelineUnknownItem.razor | 21 ++++++ MatrixRoomUtils.Web/Shared/UserListItem.razor | 4 +- 16 files changed, 332 insertions(+), 21 deletions(-) create mode 100644 MatrixRoomUtils.Web/Pages/HSAdmin/RoomQuery.razor create mode 100644 MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor create mode 100644 MatrixRoomUtils.Web/Shared/InlineUserItem.razor create mode 100644 MatrixRoomUtils.Web/Shared/RoomListItem.razor.css create mode 100644 MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor create mode 100644 MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor create mode 100644 MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor (limited to 'MatrixRoomUtils.Web') diff --git a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs index e21b363..f70572b 100644 --- a/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs +++ b/MatrixRoomUtils.Web/Classes/LocalStorageWrapper.cs @@ -16,6 +16,7 @@ public partial class LocalStorageWrapper RuntimeCache.Save = Save; RuntimeCache.SaveObject = async (key, obj) => await localStorage.SetItemAsync(key, obj); + RuntimeCache.RemoveObject = async (key) => await localStorage.RemoveItemAsync(key); if (RuntimeCache.LastUsedToken != null) { Console.WriteLine($"Access token is not null, creating authenticated home server"); diff --git a/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj b/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj index 12555c3..cf6ce87 100644 --- a/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj +++ b/MatrixRoomUtils.Web/MatrixRoomUtils.Web.csproj @@ -15,5 +15,9 @@ + + + + diff --git a/MatrixRoomUtils.Web/Pages/DevOptions.razor b/MatrixRoomUtils.Web/Pages/DevOptions.razor index c2894b3..3ca86b4 100644 --- a/MatrixRoomUtils.Web/Pages/DevOptions.razor +++ b/MatrixRoomUtils.Web/Pages/DevOptions.razor @@ -59,7 +59,12 @@ protected async Task DropCaches() { - RuntimeCache.GenericResponseCache.Clear(); + foreach (var (key, value) in RuntimeCache.GenericResponseCache) + { + value.Cache.Clear(); + } + + //RuntimeCache.GenericResponseCache.Clear(); RuntimeCache.HomeserverResolutionCache.Clear(); await LocalStorageWrapper.SaveCacheToLocalStorage(LocalStorage); } diff --git a/MatrixRoomUtils.Web/Pages/HSAdmin/RoomQuery.razor b/MatrixRoomUtils.Web/Pages/HSAdmin/RoomQuery.razor new file mode 100644 index 0000000..109ad7d --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/HSAdmin/RoomQuery.razor @@ -0,0 +1,86 @@ +@page "/RoomQuery" +@using MatrixRoomUtils.Core.Extensions +@using System.Runtime.InteropServices +@using System.ComponentModel +@using MatrixRoomUtils.Core.Responses.Admin +

Homeserver Administration - Room Query

+ + +
+ +
+ +
+ +
+ +
+ +@foreach (var res in Results) +{ +
+ +

@res.CanonicalAlias, created by

+

@res.StateEvents state events

+

@res.JoinedMembers members, of which @res.JoinedLocalMembers are on this server

+
+} + +@code { + + [Parameter, SupplyParameterFromQuery(Name = "order_by")] + public string? OrderBy { get; set; } + + [Parameter, SupplyParameterFromQuery(Name = "search_term")] + public string SearchTerm { get; set; } + + [Parameter, SupplyParameterFromQuery(Name = "content_search_term")] + public string ContentSearchTerm { get; set; } + + [Parameter, SupplyParameterFromQuery(Name = "ascending")] + public bool Ascending { get; set; } + + public List Results { get; set; } = new(); + + protected override async Task OnParametersSetAsync() + { + if(Ascending == null) + Ascending = true; + OrderBy ??= "name"; + } + + private async Task Search() + { + Results.Clear(); + var searchRooms = RuntimeCache.CurrentHomeServer.Admin.SearchRoomsAsync(orderBy: OrderBy!, dir: Ascending ? "f" : "b", searchTerm: SearchTerm, contentSearch: ContentSearchTerm).GetAsyncEnumerator(); + while (await searchRooms.MoveNextAsync()) + { + var room = searchRooms.Current; + Console.WriteLine("Hit: " + room.ToJson(indent: false)); + Results.Add(room); + } + } + + private Dictionary validOrderBy = new Dictionary() + { + { "name", "Room name" }, + { "canonical_alias", "Main alias address" }, + { "joined_members", "Number of members (reversed)" }, + { "joined_local_members", "Number of local members (reversed)" }, + { "version", "Room version" }, + { "creator", "Creator of the room" }, + { "encryption", "End-to-end encryption algorithm" }, + { "federatable", "Is room federated" }, + { "public", "Visibility in room list" }, + { "join_rules", "Join rules" }, + { "guest_access", "Guest access" }, + { "history_visibility", "Visibility of history" }, + { "state_events", "Number of state events" }, + }; + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor index 4b9c2f6..bf03ee3 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor @@ -200,7 +200,7 @@ else private bool _enableAvatars = false; - static Dictionary avatars = new Dictionary(); + static Dictionary avatars = new Dictionary(); static Dictionary servers = new Dictionary(); public static List> PolicyEvents { get; set; } = new(); diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor index a8b8fd4..5daa97c 100644 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor +++ b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor @@ -6,7 +6,6 @@ @if (Rooms.Count == 0) {

You are not in any rooms!

- @*

Loading progress: @checkedRoomCount/@totalRoomCount

*@ } else { diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerSpace.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerSpace.razor index e9d1421..1e7e065 100644 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerSpace.razor +++ b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerSpace.razor @@ -29,6 +29,7 @@ private StateEventResponse[] States { get; set; } = Array.Empty>(); private List Rooms { get; set; } = new(); + private List ServersInSpace { get; set; } = new(); protected override async Task OnInitializedAsync() { @@ -37,7 +38,7 @@ var state = await Room.GetStateAsync(""); if (state != null) { - Console.WriteLine(state.Value.ToJson()); + // Console.WriteLine(state.Value.ToJson()); States = state.Value.Deserialize[]>()!; foreach (var stateEvent in States) @@ -52,6 +53,14 @@ Rooms.Add(room); } } + else if (stateEvent.Type == "m.room.member") + { + var serverName = stateEvent.StateKey.Split(':').Last(); + if (!ServersInSpace.Contains(serverName)) + { + ServersInSpace.Add(serverName); + } + } } // if(state.Value.TryGetProperty("Type", out var Type)) @@ -70,7 +79,7 @@ { foreach (var room in Rooms) { - room.JoinAsync(); + room.JoinAsync(ServersInSpace.ToArray()); } } diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor new file mode 100644 index 0000000..a8a7fc2 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor @@ -0,0 +1,59 @@ +@page "/RoomManager/Timeline/{RoomId}" +@using MatrixRoomUtils.Web.Shared.TimelineComponents +@using MatrixRoomUtils.Core.Extensions +

RoomManagerTimeline

+
+

Loaded @Events.Count events...

+ +@foreach (var evt in Events) +{ +
+ +
+} + +@code { + + [Parameter] + public string RoomId { get; set; } = "invalid!!!!!!"; + + private List Messages { get; set; } = new(); + private List Events { get; set; } = new(); + + protected override async Task OnInitializedAsync() + { + await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + RoomId = RoomId.Replace('~', '.'); + Console.WriteLine("RoomId: " + RoomId); + var room = await RuntimeCache.CurrentHomeServer.GetRoom(RoomId); + MessagesResponse? msgs = null; + do + { + msgs = await room.GetMessagesAsync(limit: 250, from: msgs?.End, dir: "b"); + Messages.Add(msgs); + Console.WriteLine($"Got {msgs.Chunk.Count} messages"); + msgs.Chunk.Reverse(); + Events.InsertRange(0, msgs.Chunk); + StateHasChanged(); + } while (msgs.End != null); + + + await base.OnInitializedAsync(); + } + + private StateEventResponse GetProfileEventBefore(StateEventResponse Event) => Events.TakeWhile(x => x != Event).Last(e => e.Type == "m.room.member" && e.StateKey == Event.Sender); + + + private Type ComponentType(StateEventResponse Event) => Event.Type switch { + "m.room.message" => typeof(TimelineMessageItem), + "m.room.member" => typeof(TimelineMemberItem), + _ => typeof(TimelineUnknownItem) + }; + + private Dictionary ComponentParameters(Type ComponentType, StateEventResponse Event) => ComponentType switch { + Type t when t == typeof(TimelineMessageItem) => new Dictionary { { "Event", Event }, { "Events", Events } }, + Type t when t == typeof(TimelineMemberItem) => new Dictionary { { "Event", Event }, { "Events", Events } }, + _ => new Dictionary { { "Event", Event }, { "Events", Events } } + }; + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor b/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor index 42f2c09..03a7145 100644 --- a/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor +++ b/MatrixRoomUtils.Web/Shared/IndexComponents/IndexUserItem.razor @@ -24,7 +24,7 @@ [Parameter] public UserInfo User { get; set; } = null!; - private string _avatarUrl { get; set; } + private string? _avatarUrl { get; set; } private int _roomCount { get; set; } = 0; protected override async Task OnInitializedAsync() diff --git a/MatrixRoomUtils.Web/Shared/InlineUserItem.razor b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor new file mode 100644 index 0000000..56131c8 --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/InlineUserItem.razor @@ -0,0 +1,63 @@ +@using MatrixRoomUtils.Core.Responses +
+ + @ProfileName + +
+ @if (ChildContent != null) + { + @ChildContent + } +
+ +
+ +@code { + + [Parameter] + public RenderFragment? ChildContent { get; set; } + + [Parameter] + public ProfileResponse User { get; set; } + + [Parameter] + public string UserId { get; set; } + + [Parameter] + public string? ProfileAvatar { get; set; } = null; + + [Parameter] + public string? ProfileName { get; set; } = null; + + + private static SemaphoreSlim _semaphoreSlim = new(128); + + protected override async Task OnInitializedAsync() + { + await base.OnInitializedAsync(); + await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + + await _semaphoreSlim.WaitAsync(); + + var hs = await new AuthenticatedHomeServer(RuntimeCache.CurrentHomeServer.UserId, RuntimeCache.CurrentHomeServer.AccessToken, RuntimeCache.CurrentHomeServer.HomeServerDomain).Configure(); + + if (User == null) + { + if (UserId == null) + { + throw new ArgumentNullException(nameof(UserId)); + } + User = await hs.GetProfile(UserId); + } + else + { + // UserId = User.; + } + + ProfileAvatar ??= RuntimeCache.CurrentHomeServer.ResolveMediaUri(User.AvatarUrl); + ProfileName ??= User.DisplayName; + + _semaphoreSlim.Release(); + } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/RoomListItem.razor b/MatrixRoomUtils.Web/Shared/RoomListItem.razor index 4990b3c..fb28c3c 100644 --- a/MatrixRoomUtils.Web/Shared/RoomListItem.razor +++ b/MatrixRoomUtils.Web/Shared/RoomListItem.razor @@ -1,11 +1,11 @@ @using MatrixRoomUtils.Core.Authentication @using System.Text.Json @using MatrixRoomUtils.Core.Extensions -
+
@if (ShowOwnProfile) { - - @profileName + + @(profileName ?? "Loading...") -> } @@ -34,10 +34,10 @@ public bool ShowOwnProfile { get; set; } = false; private string roomName { get; set; } = "Loading..."; - private string roomIcon { get; set; } = "/icon-192.png"; + private string? roomIcon { get; set; } = "/icon-192.png"; - private string profileAvatar { get; set; } = "/icon-192.png"; - private string profileName { get; set; } = "Loading..."; + private string? profileAvatar { get; set; } + private string? profileName { get; set; } private bool hasCustomProfileAvatar { get; set; } = false; private bool hasCustomProfileName { get; set; } = false; @@ -53,8 +53,8 @@ await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await _semaphoreSlim.WaitAsync(); - - var hs = await new AuthenticatedHomeServer(RuntimeCache.CurrentHomeServer.UserId, RuntimeCache.CurrentHomeServer.AccessToken, RuntimeCache.CurrentHomeServer.HomeServerDomain).Configure(); + + var hs = RuntimeCache.CurrentHomeServer; //await new AuthenticatedHomeServer(RuntimeCache.CurrentHomeServer.UserId, RuntimeCache.CurrentHomeServer.AccessToken, RuntimeCache.CurrentHomeServer.HomeServerDomain).Configure(); if (Room == null) { @@ -69,11 +69,7 @@ RoomId = Room.RoomId; } - roomName = await Room.GetNameAsync(); - if (roomName == null) - { - roomName = "Unnamed room: " + RoomId; - } + roomName = await Room.GetNameAsync() ?? "Unnamed room: " + RoomId; var ce = await Room.GetCreateEventAsync(); if (ce != null) @@ -143,4 +139,9 @@ await LocalStorageWrapper.SaveCacheToLocalStorage(LocalStorage); } + private void Callback(ProgressEventArgs obj) + { + Console.WriteLine("prog: " + obj.ToJson(indent: false)); + } + } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/RoomListItem.razor.css b/MatrixRoomUtils.Web/Shared/RoomListItem.razor.css new file mode 100644 index 0000000..8b9a9f6 --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/RoomListItem.razor.css @@ -0,0 +1,10 @@ +/*.imageUnloaded {*/ +/* scale: 3;*/ +/* opacity: 0.5;*/ +/* transition: scale 0.5s ease-in-out;*/ +/*}*/ + +.imageLoaded { + opacity: 1; + scale: 1; + } \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor new file mode 100644 index 0000000..3803d38 --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMemberItem.razor @@ -0,0 +1,42 @@ +@using MatrixRoomUtils.Core.Extensions +@if (Event.ContentAsJsonNode["membership"]!.GetValue() == "ban") +{ + @Event.StateKey was banned +} +else if (Event.ContentAsJsonNode["membership"]!.GetValue() == "invite") +{ + @Event.StateKey was invited +} +else if (Event.ContentAsJsonNode["membership"]!.GetValue() == "join") +{ + @if (Event.ReplacesState != null) + { + @Event.StateKey changed their display name to @(Event.ContentAsJsonNode["displayname"]!.GetValue()) + } + else + { + joined + } +} +else if (Event.ContentAsJsonNode["membership"]!.GetValue() == "leave") +{ + @Event.StateKey left +} +else if (Event.ContentAsJsonNode["membership"]!.GetValue() == "knock") +{ + @Event.StateKey knocked +} +else +{ + @Event.StateKey has an unknown state: +
+        @Event.ToJson()
+    
+} + +@code { + + [Parameter] + public StateEvent Event { get; set; } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor new file mode 100644 index 0000000..8d688ea --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineMessageItem.razor @@ -0,0 +1,11 @@ +@using MatrixRoomUtils.Core.Extensions +
+    @ObjectExtensions.ToJson(Event.Content, indent: false)
+
+ +@code { + + [Parameter] + public StateEventResponse Event { get; set; } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor new file mode 100644 index 0000000..f78bdc9 --- /dev/null +++ b/MatrixRoomUtils.Web/Shared/TimelineComponents/TimelineUnknownItem.razor @@ -0,0 +1,21 @@ +@using MatrixRoomUtils.Core.Extensions +
+ +
+ + + Unknown event type:
@Event.Type
+
+
+
+           @Event.ToJson()
+        
+
+
+ +@code { + + [Parameter] + public StateEvent Event { get; set; } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Shared/UserListItem.razor b/MatrixRoomUtils.Web/Shared/UserListItem.razor index ae1fcd1..d357b0d 100644 --- a/MatrixRoomUtils.Web/Shared/UserListItem.razor +++ b/MatrixRoomUtils.Web/Shared/UserListItem.razor @@ -23,8 +23,8 @@ [Parameter] public string UserId { get; set; } - private string profileAvatar { get; set; } = "/icon-192.png"; - private string profileName { get; set; } = "Loading..."; + private string? profileAvatar { get; set; } = "/icon-192.png"; + private string? profileName { get; set; } = "Loading..."; private static SemaphoreSlim _semaphoreSlim = new(128); -- cgit 1.5.1