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