about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor')
-rw-r--r--MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor95
1 files changed, 47 insertions, 48 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();
     }