From dc3201d641a03e051c6f0db07612eb6b0bb506c3 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Thu, 4 May 2023 15:26:17 +0200 Subject: Dark theme, fancier room list --- .../Pages/PolicyList/PolicyListEditorPage.razor | 228 +++++++++++++++++++++ .../Pages/PolicyList/PolicyListRoomList.razor | 158 ++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor create 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 new file mode 100644 index 0000000..5dfb2d6 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor @@ -0,0 +1,228 @@ +@page "/PolicyListEditor/{RoomId}" +@using System.Text.Json +@using MatrixRoomUtils.Core.Extensions +@using MatrixRoomUtils.Core.StateEventTypes +@inject ILocalStorageService LocalStorage +@inject NavigationManager NavigationManager +

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.content.Entity != null)) + { + + + + + + + } + +
ServerReasonExpiresActions
Entity: @policyEvent.content.Entity
State: @policyEvent.state_key
@policyEvent.content.Reason + @policyEvent.content.ExpiryDateTime + + +
+
+ Invalid events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.type == "m.policy.rule.server" && x.content.Entity == null)) + { + + + + + } + +
State keySerialised contents
@policyEvent.state_key@policyEvent.content.ToJson(indent: false, ignoreNull: 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.content.Entity != null)) + { + + + + + + + } + +
RoomReasonExpiresActions
Entity: @policyEvent.content.Entity
State: @policyEvent.state_key
@policyEvent.content.Reason + @policyEvent.content.ExpiryDateTime + + +
+
+ Invalid events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.type == "m.policy.rule.room" && x.content.Entity == null)) + { + + + + + } + +
State keySerialised contents
@policyEvent.state_key@policyEvent.content.ToJson(indent: false, ignoreNull: true)
+
+} +@if (!PolicyEvents.Any(x => x.type == "m.policy.rule.user")) +{ +

No user policies

+} +else +{ +

User policies

+
+ + + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.type == "m.policy.rule.user" && x.content.Entity != null)) + { + + + + + + + } + +
UserReasonExpiresActions
Entity: @string.Join("", policyEvent.content.Entity.Take(64))
State: @string.Join("", policyEvent.state_key.Take(64))
@policyEvent.content.Reason + @policyEvent.content.ExpiryDateTime + + +
+
+ Invalid events + + + + + + + + + @foreach (var policyEvent in PolicyEvents.Where(x => x.type == "m.policy.rule.user" && x.content.Entity == null)) + { + + + + + } + +
State keySerialised contents
@policyEvent.state_key@policyEvent.content.ToJson(indent: false, ignoreNull: 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; } + + public List> PolicyEvents { get; set; } = new(); + + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + await base.OnInitializedAsync(); + // if(RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null) + if (RuntimeCache.CurrentHomeServer == null) + { + NavigationManager.NavigateTo("/Login"); + return; + } + RoomId = RoomId.Replace('~', '.'); + await LoadStatesAsync(); + Console.WriteLine("Policy list editor initialized!"); + } + + private async Task LoadStatesAsync() + { + // using var client = new HttpClient(); + // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); + // var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state"); + // var content = await response.Content.ReadAsStringAsync(); + // Console.WriteLine(JsonSerializer.Deserialize(content).ToJson()); + // var stateEvents = JsonSerializer.Deserialize>(content); + var room = await RuntimeCache.CurrentHomeServer.GetRoom(RoomId); + var stateEventsQuery = await room.GetStateAsync(""); + if (stateEventsQuery == null) + { + Console.WriteLine("state events query is null!!!"); + } + var stateEvents = stateEventsQuery.Value.Deserialize>(); + PolicyEvents = stateEvents.Where(x => x.type.StartsWith("m.policy.rule")) + .Select(x => JsonSerializer.Deserialize>(JsonSerializer.Serialize(x))).ToList(); + StateHasChanged(); + } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor new file mode 100644 index 0000000..5656af9 --- /dev/null +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor @@ -0,0 +1,158 @@ +@page "/PolicyListEditor" +@using System.Text.Json +@using MatrixRoomUtils.Core.Extensions +@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) + { + [@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; } = 0; + private int totalRoomCount { get; set; } = 0; + + protected override async Task OnInitializedAsync() + { + if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); + await base.OnInitializedAsync(); + if (RuntimeCache.CurrentHomeServer == null) + { + NavigationManager.NavigateTo("/Login"); + return; + } + await EnumeratePolicyRooms(); + Console.WriteLine("Policy list editor initialized!"); + } + + private async Task EnumeratePolicyRooms() + { + var xxxrooms = await RuntimeCache.CurrentHomeServer.GetJoinedRooms(); + totalRoomCount = xxxrooms.Count; + StateHasChanged(); + + var xxxsemaphore = new SemaphoreSlim(256); + var xxxtasks = new List>(); + foreach (var room in xxxrooms) + { + xxxtasks.Add(GetPolicyRoomInfo(room.RoomId, xxxsemaphore)); + } + var xxxresults = await Task.WhenAll(xxxtasks); + PolicyRoomList.AddRange(xxxresults.Where(x => x != null).Select(x => x.Value)); + + 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) + { + try + { + //TODO: refactor!!!!! + await semaphore.WaitAsync(); + PolicyRoomInfo roomInfo = new() + { + RoomId = room + }; + + + // --- // + var r = await RuntimeCache.CurrentHomeServer.GetRoom(room); + var shortcodeState = await r.GetStateAsync("org.matrix.mjolnir.shortcode"); + if(!shortcodeState.HasValue) return null; + roomInfo.Shortcode = shortcodeState.Value.TryGetProperty("shortcode", out JsonElement shortcode) ? shortcode.GetString() : null; + + if (roomInfo.Shortcode != 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