From def33cc092ae2c6defcc218b108b7c99cbfb8581 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sun, 2 Jul 2023 01:01:09 +0200 Subject: Prefetch room info --- MatrixRoomUtils.Web/Pages/ModalTest.razor | 2 +- .../Pages/PolicyList/PolicyListEditorPage.razor | 249 -------------------- .../Pages/PolicyList/PolicyListRoomList.razor | 107 --------- .../Pages/RoomManager/RoomManager.razor | 96 -------- .../Pages/RoomManager/RoomManagerTimeline.razor | 11 +- MatrixRoomUtils.Web/Pages/Rooms/Index.razor | 9 +- MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor | 250 +++++++++++++++++++++ 7 files changed, 261 insertions(+), 463 deletions(-) delete mode 100644 MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor delete mode 100644 MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor delete mode 100644 MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor create mode 100644 MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor (limited to 'MatrixRoomUtils.Web/Pages') diff --git a/MatrixRoomUtils.Web/Pages/ModalTest.razor b/MatrixRoomUtils.Web/Pages/ModalTest.razor index f32c672..79c7fcd 100644 --- a/MatrixRoomUtils.Web/Pages/ModalTest.razor +++ b/MatrixRoomUtils.Web/Pages/ModalTest.razor @@ -16,7 +16,7 @@ @code { - private Dictionary _windowInfos = new Dictionary(); + private Dictionary _windowInfos = new(); private class WindowInfo { public double X; diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor deleted file mode 100644 index 8e2609f..0000000 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor +++ /dev/null @@ -1,249 +0,0 @@ -@page "/PolicyListEditor/{RoomId}" -@using MatrixRoomUtils.Core.StateEventTypes -@using System.Text.Json -@using MatrixRoomUtils.Core.Helpers -@using MatrixRoomUtils.Core.Responses -

Policy list editor - Editing @RoomId

-
- -

- This policy list contains @PolicyEvents.Count(x => x.Type == "m.policy.rule.server") server bans, - @PolicyEvents.Count(x => x.Type == "m.policy.rule.room") room bans and - @PolicyEvents.Count(x => x.Type == "m.policy.rule.user") user bans. -

- - - -@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.server")) { -

No server policies

-} -else { -

Server policies

-
- - - - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { - var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; - - - - - - - } - -
ServerReasonExpiresActions
Entity: @policyData.Entity
State: @policyEvent.StateKey
@policyData.Reason - @policyData.ExpiryDateTime - - - @* *@ -
-
- Redacted events - - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { - var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; - - - - - } - -
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
-
-} -@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.room")) { -

No room policies

-} -else { -

Room policies

-
- - - - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { - var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; - - - - - - - } - -
RoomReasonExpiresActions
Entity: @policyData.Entity
State: @policyEvent.StateKey
@policyData.Reason - @policyData.ExpiryDateTime - - -
-
- Redacted events - - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { - - - - - } - -
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
-
-} -@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.user")) { -

No user policies

-} -else { -

User policies

-
- - - - @if (_enableAvatars) { - - } - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { - var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; - - @if (_enableAvatars) { - - } - - - - - - } - -
UserReasonExpiresActions
- - Entity: @string.Join("", policyData.Entity.Take(64))
State: @string.Join("", policyEvent.StateKey.Take(64))
@policyData.Reason - @policyData.ExpiryDateTime - - -
-
- Redacted events - - - - - - - - - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { - - - - - } - -
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
-
-} - - - -@code { - //get room list - // - sync withroom list filter - // Type = support.feline.msc3784 - //support.feline.policy.lists.msc.v1 - - [Parameter] - public string? RoomId { get; set; } - - private bool _enableAvatars; - - static readonly Dictionary avatars = new(); - static readonly Dictionary servers = new(); - - public static List PolicyEvents { get; set; } = new(); - - protected override async Task OnInitializedAsync() { - await base.OnInitializedAsync(); - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - RoomId = RoomId.Replace('~', '.'); - await LoadStatesAsync(); - Console.WriteLine("Policy list editor initialized!"); - } - - private async Task LoadStatesAsync() { - var hs = await MRUStorage.GetCurrentSession(); - var room = await hs.GetRoom(RoomId); - - var states = room.GetFullStateAsync(); - await foreach (var state in states) { - if (!state.Type.StartsWith("m.policy.rule")) continue; - PolicyEvents.Add(state); - } - - - // var stateEventsQuery = await room.GetStateAsync(""); - // var stateEvents = stateEventsQuery.Value.Deserialize>(); - // PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule")) - // .Select(x => JsonSerializer.Deserialize(JsonSerializer.Serialize(x))).ToList(); - StateHasChanged(); - } - - private async Task GetAvatar(string userId) { - try { - if (avatars.ContainsKey(userId)) return; - var hs = userId.Split(':')[1]; - var server = servers.ContainsKey(hs) ? servers[hs] : new RemoteHomeServer(userId.Split(':')[1]); - if (!servers.ContainsKey(hs)) servers.Add(hs, server); - var profile = await server.GetProfile(userId); - avatars.Add(userId, MediaResolver.ResolveMediaUri(server.FullHomeServerDomain, profile.AvatarUrl)); - servers.Add(userId, server); - StateHasChanged(); - } - catch { - // ignored - } - } - - private async Task GetAllAvatars() { - foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { - await GetAvatar((policyEvent.TypedContent as PolicyRuleStateEventData).Entity); - } - StateHasChanged(); - } - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor deleted file mode 100644 index 4db2b5a..0000000 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor +++ /dev/null @@ -1,107 +0,0 @@ -@page "/PolicyListEditor" -@using System.Text.Json.Serialization -@using MatrixRoomUtils.Core.Interfaces -@using MatrixRoomUtils.Core.StateEventTypes -@inject ILocalStorageService LocalStorage -@inject NavigationManager NavigationManager -

Policy list editor - Room list

-
- -@if (PolicyRoomList.Count == 0) { -

No policy rooms found.

-

Loading progress: @checkedRoomCount/@totalRoomCount

-} -else { - @if (checkedRoomCount != totalRoomCount) { -

Loading progress: @checkedRoomCount/@totalRoomCount

- } - foreach (var s in PolicyRoomList) { - - -
- Shortcode: @s.Shortcode -
-
- @* [@s.Shortcode] @s.Name (@s.RoomId) *@ - @*
*@ - } -} - -
- - -@code { - //get room list - // - sync withroom list filter - // Type = support.feline.msc3784 - //support.feline.policy.lists.msc.v1 - - public List PolicyRoomList { get; set; } = new(); - - private int checkedRoomCount { get; set; } - private int totalRoomCount { get; set; } - - protected override async Task OnInitializedAsync() { - await base.OnInitializedAsync(); - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - await EnumeratePolicyRooms(); - Console.WriteLine("Policy list editor initialized!"); - } - - private async Task EnumeratePolicyRooms() { - var hs = await MRUStorage.GetCurrentSession(); - var rooms = await hs.GetJoinedRooms(); - totalRoomCount = rooms.Count; - StateHasChanged(); - - var semaphore = new SemaphoreSlim(8); - var tasks = new List>(); - foreach (var room in rooms) { - tasks.Add(GetPolicyRoomInfo(room.RoomId, semaphore)); - } - var results = await Task.WhenAll(tasks); - PolicyRoomList.AddRange(results.Where(x => x is not null).Select(x => x.Value)); - - Console.WriteLine($"Detected policy lists: {PolicyRoomList.ToJson()}"); - } - - private async Task GetPolicyRoomInfo(string room, SemaphoreSlim semaphore) { - try { - await semaphore.WaitAsync(); - var hs = await MRUStorage.GetCurrentSession(); - PolicyRoomInfo roomInfo = new() { - RoomId = room - }; - var r = await hs.GetRoom(room); - var shortcodeState = await r.GetStateAsync("org.matrix.mjolnir.shortcode"); - roomInfo.Shortcode = shortcodeState.Shortcode; - - if (roomInfo.Shortcode is not null) { - roomInfo.Name = await r.GetNameAsync(); - return roomInfo; - } - - return null; - } - 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 diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor deleted file mode 100644 index 087adf8..0000000 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor +++ /dev/null @@ -1,96 +0,0 @@ -@page "/RoomManager" -@inject ILocalStorageService LocalStorage -@inject NavigationManager NavigationManager -

Room manager

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

You are not in any rooms!

-} -else { -

You are in @Rooms.Count rooms and @Spaces.Count spaces

-

- Create room -

- -
- Space List - @foreach (var room in Spaces) { - - - - } -
-
- Room List - @foreach (var room in Rooms) { - - - - } -
-} - -
- - -@code { - public List Rooms { get; set; } = new(); - public List Spaces { get; set; } = new(); - - protected override async Task OnInitializedAsync() { - Console.WriteLine("Initializing room manager"); - Console.WriteLine("Loaded from local storage"); - await base.OnInitializedAsync(); - Console.WriteLine("Initialized base"); - var hs = await MRUStorage.GetCurrentSessionOrNavigate(); - if (hs is null) return; - Console.WriteLine("Fetching joined rooms"); - var _rooms = await hs.GetJoinedRooms(); - StateHasChanged(); - Console.WriteLine($"Got {_rooms.Count} rooms"); - var semaphore = new SemaphoreSlim(10); - var tasks = new List>(); - foreach (var room in _rooms) { - tasks.Add(CheckIfSpace(room, semaphore)); - } - await Task.WhenAll(tasks); - - Console.WriteLine("Fetched joined rooms!"); - } - - private async Task CheckIfSpace(GenericRoom room, SemaphoreSlim semaphore) { - await semaphore.WaitAsync(); - // Console.WriteLine($"Checking if {room.RoomId} is a space"); - try { - var state = await room.GetStateAsync("m.room.create"); - if (state is not null) { - //Console.WriteLine(state.Value.ToJson()); - if (state.Type is not null) { - if (state.Type == "m.space") { - Console.WriteLine($"Room {room.RoomId} is a space!"); - Spaces.Add(room); - StateHasChanged(); - return room; - } - else { - Console.WriteLine($"Encountered unknown room type {state.Type}"); - } - } - else { - Rooms.Add(room); - //this is fine, apprently... - // Console.WriteLine($"Room {room.RoomId} has no Content.type in m.room.create!"); - } - } - } - catch (Exception e) { - Console.WriteLine(e); - return null; - } - finally { - semaphore.Release(); - } - return null; - } - -} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor index e32b5cb..3225c15 100644 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor +++ b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerTimeline.razor @@ -14,13 +14,12 @@ @code { [Parameter] - public string RoomId { get; set; } = "invalid!!!!!!"; + public string RoomId { get; set; } private List Messages { get; } = new(); private List Events { get; } = new(); protected override async Task OnInitializedAsync() { - RoomId = RoomId.Replace('~', '.'); Console.WriteLine("RoomId: " + RoomId); var hs = await MRUStorage.GetCurrentSessionOrNavigate(); if (hs is null) return; @@ -46,10 +45,10 @@ _ => 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 } } + private Dictionary ComponentParameters(Type componentType, StateEventResponse @event) => componentType switch { + not null when componentType == typeof(TimelineMessageItem) => new Dictionary { { "Event", @event }, { "Events", Events } }, + not null when componentType == 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/Pages/Rooms/Index.razor b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor index 932748d..d88d5b2 100644 --- a/MatrixRoomUtils.Web/Pages/Rooms/Index.razor +++ b/MatrixRoomUtils.Web/Pages/Rooms/Index.razor @@ -1,5 +1,6 @@ @page "/Rooms" @using MatrixRoomUtils.Core.StateEventTypes +@using MatrixRoomUtils.Core.StateEventTypes.Spec

Room list

@if (Rooms is not null) { @@ -9,14 +10,14 @@ @code { - private List Rooms { get; set; } - private ProfileResponse GlobalProfile { get; set; } - + private List Rooms { get; set; } + private ProfileResponseEventData GlobalProfile { get; set; } + protected override async Task OnInitializedAsync() { var hs = await MRUStorage.GetCurrentSessionOrNavigate(); if (hs is null) return; GlobalProfile = await hs.GetProfile(hs.WhoAmI.UserId); - Rooms = await hs.GetJoinedRooms(); + Rooms = (await hs.GetJoinedRooms()).Select(x => new RoomInfo() { Room = x }).ToList(); await base.OnInitializedAsync(); } diff --git a/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor b/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor new file mode 100644 index 0000000..4cb16b8 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/Rooms/PolicyList.razor @@ -0,0 +1,250 @@ +@page "/Rooms/{RoomId}/Policies" +@using MatrixRoomUtils.Core.StateEventTypes +@using System.Text.Json +@using MatrixRoomUtils.Core.Helpers +@using MatrixRoomUtils.Core.Responses +@using MatrixRoomUtils.Core.StateEventTypes.Spec +

Policy list editor - Editing @RoomId

+
+ +

+ This policy list contains @PolicyEvents.Count(x => x.Type == "m.policy.rule.server") server bans, + @PolicyEvents.Count(x => x.Type == "m.policy.rule.room") room bans and + @PolicyEvents.Count(x => x.Type == "m.policy.rule.user") user bans. +

+ + + +@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.server")) { +

No server policies

+} +else { +

Server policies

+
+ + + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { + var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; + + + + + + + } + +
ServerReasonExpiresActions
Entity: @policyData.Entity
State: @policyEvent.StateKey
@policyData.Reason + @policyData.ExpiryDateTime + + + @* *@ +
+
+ Redacted events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { + var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; + + + + + } + +
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
+
+} +@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.room")) { +

No room policies

+} +else { +

Room policies

+
+ + + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { + var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; + + + + + + + } + +
RoomReasonExpiresActions
Entity: @policyData.Entity
State: @policyEvent.StateKey
@policyData.Reason + @policyData.ExpiryDateTime + + +
+
+ Redacted events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { + + + + + } + +
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
+
+} +@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.user")) { +

No user policies

+} +else { +

User policies

+
+ + + + @if (_enableAvatars) { + + } + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { + var policyData = policyEvent.TypedContent as PolicyRuleStateEventData; + + @if (_enableAvatars) { + + } + + + + + + } + +
UserReasonExpiresActions
+ + Entity: @string.Join("", policyData.Entity.Take(64))
State: @string.Join("", policyEvent.StateKey.Take(64))
@policyData.Reason + @policyData.ExpiryDateTime + + +
+
+ Redacted events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { + + + + + } + +
State keySerialised Contents
@policyEvent.StateKey@policyEvent.RawContent.ToJson(false, true)
+
+} + + + +@code { + //get room list + // - sync withroom list filter + // Type = support.feline.msc3784 + //support.feline.policy.lists.msc.v1 + + [Parameter] + public string? RoomId { get; set; } + + private bool _enableAvatars; + + static readonly Dictionary avatars = new(); + static readonly Dictionary servers = new(); + + public static List PolicyEvents { get; set; } = new(); + + protected override async Task OnInitializedAsync() { + await base.OnInitializedAsync(); + var hs = await MRUStorage.GetCurrentSessionOrNavigate(); + if (hs is null) return; + RoomId = RoomId.Replace('~', '.'); + await LoadStatesAsync(); + Console.WriteLine("Policy list editor initialized!"); + } + + private async Task LoadStatesAsync() { + var hs = await MRUStorage.GetCurrentSession(); + var room = await hs.GetRoom(RoomId); + + var states = room.GetFullStateAsync(); + await foreach (var state in states) { + if (!state.Type.StartsWith("m.policy.rule")) continue; + PolicyEvents.Add(state); + } + + + // var stateEventsQuery = await room.GetStateAsync(""); + // var stateEvents = stateEventsQuery.Value.Deserialize>(); + // PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule")) + // .Select(x => JsonSerializer.Deserialize(JsonSerializer.Serialize(x))).ToList(); + StateHasChanged(); + } + + private async Task GetAvatar(string userId) { + try { + if (avatars.ContainsKey(userId)) return; + var hs = userId.Split(':')[1]; + var server = servers.ContainsKey(hs) ? servers[hs] : new RemoteHomeServer(userId.Split(':')[1]); + if (!servers.ContainsKey(hs)) servers.Add(hs, server); + var profile = await server.GetProfile(userId); + avatars.Add(userId, MediaResolver.ResolveMediaUri(server.FullHomeServerDomain, profile.AvatarUrl)); + servers.Add(userId, server); + StateHasChanged(); + } + catch { + // ignored + } + } + + private async Task GetAllAvatars() { + foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity is not null)) { + await GetAvatar((policyEvent.TypedContent as PolicyRuleStateEventData).Entity); + } + StateHasChanged(); + } + +} \ No newline at end of file -- cgit 1.5.1