RoomStateViewerPage.razor (5403B)
1 @page "/RoomStateViewer/{RoomId}" 2 @using System.Net.Http.Headers 3 @using System.Text.Json 4 @using MatrixRoomUtils.Core.Extensions 5 @inject ILocalStorageService LocalStorage 6 @inject NavigationManager NavigationManager 7 <h3>Room state viewer - Viewing @RoomId</h3> 8 <hr/> 9 10 <p>@status</p> 11 12 <input type="checkbox" id="showAll" @bind="ShowMembershipEvents"/> Show member events 13 14 <table class="table table-striped table-hover" style="width: fit-Content;"> 15 <thead> 16 <tr> 17 <th scope="col">Type</th> 18 <th scope="col">Content</th> 19 </tr> 20 </thead> 21 <tbody> 22 @foreach (var stateEvent in FilteredEvents.Where(x => x.state_key == "").OrderBy(x => x.origin_server_ts)) 23 { 24 <tr> 25 <td>@stateEvent.type</td> 26 <td style="max-width: fit-Content;"> 27 <pre>@stateEvent.content</pre> 28 </td> 29 </tr> 30 } 31 </tbody> 32 </table> 33 34 @foreach (var group in FilteredEvents.GroupBy(x => x.state_key).OrderBy(x => x.Key).Where(x => x.Key != "")) 35 { 36 <details> 37 <summary>@group.Key</summary> 38 <table class="table table-striped table-hover" style="width: fit-Content;"> 39 <thead> 40 <tr> 41 <th scope="col">Type</th> 42 <th scope="col">Content</th> 43 </tr> 44 </thead> 45 <tbody> 46 @foreach (var stateEvent in group.OrderBy(x => x.origin_server_ts)) 47 { 48 <tr> 49 <td>@stateEvent.type</td> 50 <td style="max-width: fit-Content;"> 51 <pre>@stateEvent.content</pre> 52 </td> 53 </tr> 54 } 55 </tbody> 56 </table> 57 </details> 58 } 59 60 <LogView></LogView> 61 62 @code { 63 //get room list 64 // - sync withroom list filter 65 // Type = support.feline.msc3784 66 //support.feline.policy.lists.msc.v1 67 68 [Parameter] 69 public string? RoomId { get; set; } 70 71 public List<PreRenderedStateEvent> FilteredEvents { get; set; } = new(); 72 public List<PreRenderedStateEvent> Events { get; set; } = new(); 73 public string status = ""; 74 75 protected override async Task OnInitializedAsync() 76 { 77 await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); 78 await base.OnInitializedAsync(); 79 if (RuntimeCache.CurrentHomeServer == null) 80 { 81 NavigationManager.NavigateTo("/Login"); 82 return; 83 } 84 RoomId = RoomId.Replace('~', '.'); 85 await LoadStatesAsync(); 86 Console.WriteLine("Policy list editor initialized!"); 87 } 88 private DateTime _lastUpdate = DateTime.Now; 89 90 private async Task LoadStatesAsync() 91 { 92 int StateLoaded = 0; 93 //TODO: can we improve this? 94 using var client = new HttpClient(); 95 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.CurrentHomeServer.AccessToken); 96 var response = await client.GetAsync($"{RuntimeCache.CurrentHomeServer.FullHomeServerDomain}/_matrix/client/r0/rooms/{RoomId}/state"); 97 // var response = await client.GetAsync($"http://localhost:5117/matrix-hq-state.json"); 98 //var _events = await response.Content.ReadFromJsonAsync<Queue<StateEventStruct>>(); 99 var _data = await response.Content.ReadAsStreamAsync(); 100 var __events = JsonSerializer.DeserializeAsyncEnumerable<StateEventStruct>(_data); 101 await foreach (var _ev in __events) 102 { 103 var e = new PreRenderedStateEvent() 104 { 105 type = _ev.type, 106 state_key = _ev.state_key, 107 origin_server_ts = _ev.origin_server_ts, 108 content = _ev.content.ToJson(indent: true, ignoreNull: true), 109 }; 110 Events.Add(e); 111 if (string.IsNullOrEmpty(e.state_key)) 112 { 113 FilteredEvents.Add(e); 114 } 115 StateLoaded++; 116 if ((DateTime.Now - _lastUpdate).TotalMilliseconds > 100) 117 { 118 _lastUpdate = DateTime.Now; 119 status = $"Loaded {StateLoaded} state events"; 120 StateHasChanged(); 121 await Task.Delay(0); 122 } 123 124 } 125 126 StateHasChanged(); 127 } 128 129 private async Task RebuildFilteredData() 130 { 131 status = "Rebuilding filtered data..."; 132 StateHasChanged(); 133 await Task.Delay(1); 134 var _FilteredEvents = Events; 135 if (!ShowMembershipEvents) 136 _FilteredEvents = _FilteredEvents.Where(x => x.type != "m.room.member").ToList(); 137 138 status = "Done, rerendering!"; 139 StateHasChanged(); 140 await Task.Delay(1); 141 FilteredEvents = _FilteredEvents; 142 StateHasChanged(); 143 } 144 145 146 public struct PreRenderedStateEvent 147 { 148 public string content { get; set; } 149 public long origin_server_ts { get; set; } 150 public string state_key { get; set; } 151 public string type { get; set; } 152 // public string Sender { get; set; } 153 // public string EventId { get; set; } 154 // public string UserId { get; set; } 155 // public string ReplacesState { get; set; } 156 } 157 158 public bool ShowMembershipEvents 159 { 160 get => _showMembershipEvents; 161 set 162 { 163 _showMembershipEvents = value; 164 RebuildFilteredData(); 165 } 166 } 167 168 private bool _showMembershipEvents; 169 }