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.razor122
1 files changed, 82 insertions, 40 deletions
diff --git a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor
index d0f9b87..08cdc2c 100644
--- a/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor
+++ b/MatrixRoomUtils.Web/Pages/PolicyList/PolicyListEditorPage.razor
@@ -8,13 +8,14 @@
 <hr/>
 
 <p>
-    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.
+    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.
 </p>
+<InputCheckbox @bind-Value="_enableAvatars" @oninput="GetAllAvatars"></InputCheckbox><label>Enable avatars (WILL EXPOSE YOUR IP TO TARGET HOMESERVERS!)</label>
 
 
-@if (!PolicyEvents.Any(x => x.type == "m.policy.rule.server"))
+@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.server"))
 {
     <p>No server policies</p>
 }
@@ -22,7 +23,7 @@ else
 {
     <h3>Server policies</h3>
     <hr/>
-    <table class="table table-striped table-hover" style="width: fit-content;">
+    <table class="table table-striped table-hover" style="width: fit-Content;">
         <thead>
         <tr>
             <th scope="col" style="max-width: 50vw;">Server</th>
@@ -32,10 +33,10 @@ 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.content.Entity != null))
         {
             <tr>
-                <td>Entity: @policyEvent.content.Entity<br/>State: @policyEvent.state_key</td>
+                <td>Entity: @policyEvent.content.Entity<br/>State: @policyEvent.StateKey</td>
                 <td>@policyEvent.content.Reason</td>
                 <td>
                     @policyEvent.content.ExpiryDateTime
@@ -50,18 +51,18 @@ else
     </table>
     <details>
         <summary>Invalid events</summary>
-        <table class="table table-striped table-hover" style="width: fit-content;">
+        <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
                 <th scope="col" style="max-width: 50vw;">State key</th>
-                <th scope="col">Serialised contents</th>
+                <th scope="col">Serialised Contents</th>
             </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.content.Entity == null))
             {
                 <tr>
-                    <td>@policyEvent.state_key</td>
+                    <td>@policyEvent.StateKey</td>
                     <td>@policyEvent.content.ToJson(indent: false, ignoreNull: true)</td>
                 </tr>
             }
@@ -69,7 +70,7 @@ else
         </table>
     </details>
 }
-@if (!PolicyEvents.Any(x => x.type == "m.policy.rule.room"))
+@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.room"))
 {
     <p>No room policies</p>
 }
@@ -77,7 +78,7 @@ else
 {
     <h3>Room policies</h3>
     <hr/>
-    <table class="table table-striped table-hover" style="width: fit-content;">
+    <table class="table table-striped table-hover" style="width: fit-Content;">
         <thead>
         <tr>
             <th scope="col" style="max-width: 50vw;">Room</th>
@@ -87,10 +88,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.content.Entity != null))
         {
             <tr>
-                <td>Entity: @policyEvent.content.Entity<br/>State: @policyEvent.state_key</td>
+                <td>Entity: @policyEvent.content.Entity<br/>State: @policyEvent.StateKey</td>
                 <td>@policyEvent.content.Reason</td>
                 <td>
                     @policyEvent.content.ExpiryDateTime
@@ -104,18 +105,18 @@ else
     </table>
     <details>
         <summary>Invalid events</summary>
-        <table class="table table-striped table-hover" style="width: fit-content;">
+        <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
                 <th scope="col" style="max-width: 50vw;">State key</th>
-                <th scope="col">Serialised contents</th>
+                <th scope="col">Serialised Contents</th>
             </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.content.Entity == null))
             {
                 <tr>
-                    <td>@policyEvent.state_key</td>
+                    <td>@policyEvent.StateKey</td>
                     <td>@policyEvent.content.ToJson(indent: false, ignoreNull: true)</td>
                 </tr>
             }
@@ -123,7 +124,7 @@ else
         </table>
     </details>
 }
-@if (!PolicyEvents.Any(x => x.type == "m.policy.rule.user"))
+@if (!PolicyEvents.Any(x => x.Type == "m.policy.rule.user"))
 {
     <p>No user policies</p>
 }
@@ -131,9 +132,13 @@ else
 {
     <h3>User policies</h3>
     <hr/>
-    <table class="table table-striped table-hover" style="width: fit-content;">
+    <table class="table table-striped table-hover" style="width: fit-Content;">
         <thead>
         <tr>
+            @if (_enableAvatars)
+            {
+                <th scope="col"></th>
+            }
             <th scope="col" style="max-width: 0.2vw; word-wrap: anywhere;">User</th>
             <th scope="col">Reason</th>
             <th scope="col">Expires</th>
@@ -141,10 +146,14 @@ 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.content.Entity != null))
         {
             <tr>
-                <td style="word-wrap: anywhere;">Entity: @string.Join("", policyEvent.content.Entity.Take(64))<br/>State: @string.Join("", policyEvent.state_key.Take(64))</td>
+                @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] : "")"/></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>
                     @policyEvent.content.ExpiryDateTime
@@ -158,18 +167,18 @@ else
     </table>
     <details>
         <summary>Invalid events</summary>
-        <table class="table table-striped table-hover" style="width: fit-content;">
+        <table class="table table-striped table-hover" style="width: fit-Content;">
             <thead>
             <tr>
                 <th scope="col">State key</th>
-                <th scope="col">Serialised contents</th>
+                <th scope="col">Serialised Contents</th>
             </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.content.Entity == null))
             {
                 <tr>
-                    <td>@policyEvent.state_key</td>
+                    <td>@policyEvent.StateKey</td>
                     <td>@policyEvent.content.ToJson(indent: false, ignoreNull: true)</td>
                 </tr>
             }
@@ -183,19 +192,24 @@ else
 @code {
     //get room list
     // - sync withroom list filter
-    // type = support.feline.msc3784
+    // Type = support.feline.msc3784
     //support.feline.policy.lists.msc.v1
 
     [Parameter]
     public string? RoomId { get; set; }
+    
+    private bool _enableAvatars = false;
+    
+    static Dictionary<string, string> avatars = new Dictionary<string, string>();
+    static Dictionary<string, RemoteHomeServer> servers = new Dictionary<string, RemoteHomeServer>();
 
-    public List<StateEvent<PolicyRuleStateEventData>> PolicyEvents { get; set; } = new();
+    public static List<StateEventResponse<PolicyRuleStateEventData>> PolicyEvents { get; set; } = new();
 
     protected override async Task OnInitializedAsync()
     {
-        if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
+        await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
         await base.OnInitializedAsync();
-    // if(RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null)
+        // if(RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null)
         if (RuntimeCache.CurrentHomeServer == null)
         {
             NavigationManager.NavigateTo("/Login");
@@ -208,21 +222,49 @@ else
 
     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<object>(content).ToJson());
-    // var stateEvents = JsonSerializer.Deserialize<List<StateEvent>>(content);
+        // 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<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 stateEvents = stateEventsQuery.Value.Deserialize<List<StateEvent>>();
-        PolicyEvents = stateEvents.Where(x => x.type.StartsWith("m.policy.rule"))
-            .Select(x => JsonSerializer.Deserialize<StateEvent<PolicyRuleStateEventData>>(JsonSerializer.Serialize(x))).ToList();
+        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();
+        StateHasChanged();
+    }
+    
+    private async Task GetAvatar(string userId)
+    {
+        try
+        {
+            if (avatars.ContainsKey(userId)) return;
+            var hs = userId.Split(':')[1];
+            RemoteHomeServer server = servers.ContainsKey(hs) ? servers[hs] : await new RemoteHomeServer(userId.Split(':')[1]).Configure();
+            if (!servers.ContainsKey(hs)) servers.Add(hs, server);
+            var profile = await server.GetProfile(userId);
+            avatars.Add(userId, server.ResolveMediaUri(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.content.Entity != null))
+        {
+            await GetAvatar(policyEvent.content.Entity);
+        }
         StateHasChanged();
     }