about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/PolicyList
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Web/Pages/PolicyList')
-rw-r--r--MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor95
-rw-r--r--MatrixRoomUtils.Web/Pages/PolicyList/PolicyListRoomList.razor39
2 files changed, 68 insertions, 66 deletions
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
 <h3>Policy list editor - Editing @RoomId</h3>
 <hr/>
 
@@ -31,12 +30,13 @@ else {
         </tr>
         </thead>
         <tbody>
-        @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;
             <tr>
-                <td>Entity: @policyEvent.Content.Entity<br/>State: @policyEvent.StateKey</td>
-                <td>@policyEvent.Content.Reason</td>
+                <td>Entity: @policyData.Entity<br/>State: @policyEvent.StateKey</td>
+                <td>@policyData.Reason</td>
                 <td>
-                    @policyEvent.Content.ExpiryDateTime
+                    @policyData.ExpiryDateTime
                 </td>
                 <td>
                     <button class="btn" @* @onclick="async () => await RemovePolicyAsync(policyEvent)" *@>Edit</button>
@@ -47,7 +47,7 @@ else {
         </tbody>
     </table>
     <details>
-        <summary>Invalid events</summary>
+        <summary>Redacted events</summary>
         <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
@@ -56,10 +56,11 @@ else {
             </tr>
             </thead>
             <tbody>
-            @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;
                 <tr>
                     <td>@policyEvent.StateKey</td>
-                    <td>@policyEvent.Content.ToJson(false, true)</td>
+                    <td>@policyEvent.RawContent.ToJson(false, true)</td>
                 </tr>
             }
             </tbody>
@@ -82,12 +83,13 @@ else {
         </tr>
         </thead>
         <tbody>
-        @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;
             <tr>
-                <td>Entity: @policyEvent.Content.Entity<br/>State: @policyEvent.StateKey</td>
-                <td>@policyEvent.Content.Reason</td>
+                <td>Entity: @policyData.Entity<br/>State: @policyEvent.StateKey</td>
+                <td>@policyData.Reason</td>
                 <td>
-                    @policyEvent.Content.ExpiryDateTime
+                    @policyData.ExpiryDateTime
                 </td>
                 <td>
                     <button class="btn btn-danger" @* @onclick="async () => await RemovePolicyAsync(policyEvent)" *@>Remove</button>
@@ -97,7 +99,7 @@ else {
         </tbody>
     </table>
     <details>
-        <summary>Invalid events</summary>
+        <summary>Redacted events</summary>
         <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
@@ -106,10 +108,10 @@ else {
             </tr>
             </thead>
             <tbody>
-            @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)) {
                 <tr>
                     <td>@policyEvent.StateKey</td>
-                    <td>@policyEvent.Content.ToJson(false, true)</td>
+                    <td>@policyEvent.RawContent.ToJson(false, true)</td>
                 </tr>
             }
             </tbody>
@@ -135,17 +137,18 @@ else {
         </tr>
         </thead>
         <tbody>
-        @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;
             <tr>
                 @if (_enableAvatars) {
                     <td scope="col">
-                        <img style="width: 48px; height: 48px; aspect-ratio: unset; border-radius: 50%;" src="@(avatars.ContainsKey(policyEvent.Content.Entity) ? avatars[policyEvent.Content.Entity] : "")"/>
+                        <img style="width: 48px; height: 48px; aspect-ratio: unset; border-radius: 50%;" src="@(avatars.ContainsKey(policyData.Entity) ? avatars[policyData.Entity] : "")"/>
                     </td>
                 }
-                <td style="word-wrap: anywhere;">Entity: @string.Join("", policyEvent.Content.Entity.Take(64))<br/>State: @string.Join("", policyEvent.StateKey.Take(64))</td>
-                <td>@policyEvent.Content.Reason</td>
+                <td style="word-wrap: anywhere;">Entity: @string.Join("", policyData.Entity.Take(64))<br/>State: @string.Join("", policyEvent.StateKey.Take(64))</td>
+                <td>@policyData.Reason</td>
                 <td>
-                    @policyEvent.Content.ExpiryDateTime
+                    @policyData.ExpiryDateTime
                 </td>
                 <td>
                     <button class="btn btn-danger" @* @onclick="async () => await RemovePolicyAsync(policyEvent)" *@>Remove</button>
@@ -155,7 +158,7 @@ else {
         </tbody>
     </table>
     <details>
-        <summary>Invalid events</summary>
+        <summary>Redacted events</summary>
         <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
@@ -164,10 +167,10 @@ else {
             </tr>
             </thead>
             <tbody>
-            @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)) {
                 <tr>
                     <td>@policyEvent.StateKey</td>
-                    <td>@policyEvent.Content.ToJson(false, true)</td>
+                    <td>@policyEvent.RawContent.ToJson(false, true)</td>
                 </tr>
             }
             </tbody>
@@ -191,36 +194,32 @@ else {
     static readonly Dictionary<string, string?> avatars = new();
     static readonly Dictionary<string, RemoteHomeServer> servers = new();
 
-    public static List<StateEventResponse<PolicyRuleStateEventData>> PolicyEvents { get; set; } = new();
+    public static List<StateEventResponse> 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<object>(Content).ToJson());
-    // var stateEvents = JsonSerializer.Deserialize<List<StateEventResponse>>(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<List<StateEventResponse>>();
-        PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule"))
-            .Select(x => JsonSerializer.Deserialize<StateEventResponse<PolicyRuleStateEventData>>(JsonSerializer.Serialize(x))).ToList();
+        
+        
+        // var stateEventsQuery = await room.GetStateAsync("");
+        // var stateEvents = stateEventsQuery.Value.Deserialize<List<StateEventResponse>>();
+        // PolicyEvents = stateEvents.Where(x => x.Type.StartsWith("m.policy.rule"))
+            // .Select(x => JsonSerializer.Deserialize<StateEventResponse>(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
 <h3>Policy list editor - Room list</h3>
@@ -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<Task<PolicyRoomInfo?>>();
-        foreach (var room in xxxrooms) {
-            xxxtasks.Add(GetPolicyRoomInfo(room.RoomId, xxxsemaphore));
+        var semaphore = new SemaphoreSlim(8);
+        var tasks = new List<Task<PolicyRoomInfo?>>();
+        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<PolicyRoomInfo?> 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<MjolnirShortcodeEventData>("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; }