From def33cc092ae2c6defcc218b108b7c99cbfb8581 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sun, 2 Jul 2023 01:01:09 +0200 Subject: Prefetch room info --- .../Pages/PolicyList/PolicyListEditorPage.razor | 249 --------------------- .../Pages/PolicyList/PolicyListRoomList.razor | 107 --------- 2 files changed, 356 deletions(-) delete mode 100644 MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor delete mode 100644 MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor (limited to 'MatrixRoomUtils.Web/Pages/PolicyList') 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 -- cgit 1.5.1