From bb8c2637af3b7982e7a4b2fd15e2fbec613d0848 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 30 Jun 2023 03:36:58 +0200 Subject: Todays progress --- .../Pages/PolicyList/PolicyListEditorPage.razor | 95 +++++++++++----------- .../Pages/PolicyList/PolicyListRoomList.razor | 39 +++++---- 2 files changed, 68 insertions(+), 66 deletions(-) (limited to 'MatrixRoomUtils.Web/Pages/PolicyList') diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor index 0840ebf..8e2609f 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor @@ -1,9 +1,8 @@ @page "/PolicyListEditor/{RoomId}" @using MatrixRoomUtils.Core.StateEventTypes @using System.Text.Json +@using MatrixRoomUtils.Core.Helpers @using MatrixRoomUtils.Core.Responses -@inject ILocalStorageService LocalStorage -@inject NavigationManager NavigationManager

Policy list editor - Editing @RoomId


@@ -31,12 +30,13 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && x.Content.Entity != null)) { + @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; - Entity: @policyEvent.Content.Entity
State: @policyEvent.StateKey - @policyEvent.Content.Reason + Entity: @policyData.Entity
State: @policyEvent.StateKey + @policyData.Reason - @policyEvent.Content.ExpiryDateTime + @policyData.ExpiryDateTime @@ -47,7 +47,7 @@ else {
- Invalid events + Redacted events @@ -56,10 +56,11 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.server" && x.Content.Entity == null)) { + @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; - + } @@ -82,12 +83,13 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && x.Content.Entity != null)) { + @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; - - + +
@policyEvent.StateKey@policyEvent.Content.ToJson(false, true)@policyEvent.RawContent.ToJson(false, true)
Entity: @policyEvent.Content.Entity
State: @policyEvent.StateKey
@policyEvent.Content.ReasonEntity: @policyData.Entity
State: @policyEvent.StateKey
@policyData.Reason - @policyEvent.Content.ExpiryDateTime + @policyData.ExpiryDateTime @@ -97,7 +99,7 @@ else {
- Invalid events + Redacted events @@ -106,10 +108,10 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && x.Content.Entity == null)) { + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.room" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { - + } @@ -135,17 +137,18 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && x.Content.Entity != null)) { + @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) { } - - + +
@policyEvent.StateKey@policyEvent.Content.ToJson(false, true)@policyEvent.RawContent.ToJson(false, true)
- + Entity: @string.Join("", policyEvent.Content.Entity.Take(64))
State: @string.Join("", policyEvent.StateKey.Take(64))
@policyEvent.Content.ReasonEntity: @string.Join("", policyData.Entity.Take(64))
State: @string.Join("", policyEvent.StateKey.Take(64))
@policyData.Reason - @policyEvent.Content.ExpiryDateTime + @policyData.ExpiryDateTime @@ -155,7 +158,7 @@ else {
- Invalid events + Redacted events @@ -164,10 +167,10 @@ else { - @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && x.Content.Entity == null)) { + @foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && (x.TypedContent as PolicyRuleStateEventData).Entity == null)) { - + } @@ -191,36 +194,32 @@ else { static readonly Dictionary avatars = new(); static readonly Dictionary servers = new(); - public static List> PolicyEvents { get; set; } = new(); + public static List PolicyEvents { get; set; } = new(); protected override async Task OnInitializedAsync() { - await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - // if(RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null) - if (RuntimeCache.CurrentHomeServer == null) { - NavigationManager.NavigateTo("/Login"); - return; - } + 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() { - // using var client = new HttpClient(); - // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", LocalStorageWrapper.AccessToken); - // var response = await client.GetAsync($"{LocalStorageWrapper.CurrentHomeserver}/_matrix/client/v3/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 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 stateEvents = stateEventsQuery.Value.Deserialize>(); - PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule")) - .Select(x => JsonSerializer.Deserialize>(JsonSerializer.Serialize(x))).ToList(); + + + // 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(); } @@ -228,10 +227,10 @@ else { try { if (avatars.ContainsKey(userId)) return; var hs = userId.Split(':')[1]; - var server = servers.ContainsKey(hs) ? servers[hs] : await new RemoteHomeServer(userId.Split(':')[1]).Configure(); + 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, server.ResolveMediaUri(profile.AvatarUrl)); + avatars.Add(userId, MediaResolver.ResolveMediaUri(server.FullHomeServerDomain, profile.AvatarUrl)); servers.Add(userId, server); StateHasChanged(); } @@ -241,8 +240,8 @@ else { } private async Task GetAllAvatars() { - foreach (var policyEvent in PolicyEvents.Where(x => x.Type == "m.policy.rule.user" && x.Content.Entity != null)) { - await GetAvatar(policyEvent.Content.Entity); + 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(); } diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor index 8f711b5..4db2b5a 100644 --- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor +++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor @@ -1,4 +1,7 @@ @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

@@ -39,28 +42,26 @@ else { private int totalRoomCount { get; set; } protected override async Task OnInitializedAsync() { - await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); await base.OnInitializedAsync(); - if (RuntimeCache.CurrentHomeServer == null) { - NavigationManager.NavigateTo("/Login"); - return; - } + var hs = await MRUStorage.GetCurrentSessionOrNavigate(); + if (hs is null) return; await EnumeratePolicyRooms(); Console.WriteLine("Policy list editor initialized!"); } private async Task EnumeratePolicyRooms() { - var xxxrooms = await RuntimeCache.CurrentHomeServer.GetJoinedRooms(); - totalRoomCount = xxxrooms.Count; + var hs = await MRUStorage.GetCurrentSession(); + var rooms = await hs.GetJoinedRooms(); + totalRoomCount = rooms.Count; StateHasChanged(); - var xxxsemaphore = new SemaphoreSlim(1000); - var xxxtasks = new List>(); - foreach (var room in xxxrooms) { - xxxtasks.Add(GetPolicyRoomInfo(room.RoomId, xxxsemaphore)); + var semaphore = new SemaphoreSlim(8); + var tasks = new List>(); + foreach (var room in rooms) { + tasks.Add(GetPolicyRoomInfo(room.RoomId, semaphore)); } - var xxxresults = await Task.WhenAll(xxxtasks); - PolicyRoomList.AddRange(xxxresults.Where(x => x != null).Select(x => x.Value)); + 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()}"); } @@ -68,15 +69,15 @@ else { 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 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 var shortcode) ? shortcode.GetString() : null; + var r = await hs.GetRoom(room); + var shortcodeState = await r.GetStateAsync("org.matrix.mjolnir.shortcode"); + roomInfo.Shortcode = shortcodeState.Shortcode; - if (roomInfo.Shortcode != null) { + if (roomInfo.Shortcode is not null) { roomInfo.Name = await r.GetNameAsync(); return roomInfo; } @@ -90,6 +91,8 @@ else { } } + + public struct PolicyRoomInfo { public string RoomId { get; set; } -- cgit 1.5.1
@policyEvent.StateKey@policyEvent.Content.ToJson(false, true)@policyEvent.RawContent.ToJson(false, true)