@page "/RoomStateViewer/{RoomId}"
@using System.Net.Http.Headers
@using System.Text.Json
@using MatrixRoomUtils.Extensions
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager
Room state viewer
Room ID: @RoomId
@status
Show member events
Type |
Content |
@foreach (var stateEvent in FilteredEvents.Where(x => x.state_key == "").OrderBy(x => x.origin_server_ts))
{
@stateEvent.type |
@stateEvent.content
|
}
@foreach (var group in FilteredEvents.GroupBy(x => x.state_key).OrderBy(x => x.Key).Where(x => x.Key != ""))
{
@group.Key
Type |
Content |
@foreach (var stateEvent in group.OrderBy(x => x.origin_server_ts))
{
@stateEvent.type |
@stateEvent.content
|
}
}
@code {
//get room list
// - sync withroom list filter
// type = support.feline.msc3784
//support.feline.policy.lists.msc.v1
[Parameter]
public string? RoomId { get; set; }
public List FilteredEvents { get; set; } = new();
public List Events { get; set; } = new();
public string status = "";
protected override async Task OnInitializedAsync()
{
if (!RuntimeCache.WasLoaded) await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
await base.OnInitializedAsync();
if (RuntimeCache.AccessToken == null || RuntimeCache.CurrentHomeserver == null)
{
NavigationManager.NavigateTo("/Login");
return;
}
RoomId = RoomId.Replace('~', '.');
await LoadStatesAsync();
Console.WriteLine("Policy list editor initialized!");
}
private DateTime _lastUpdate = DateTime.Now;
private async Task LoadStatesAsync()
{
int StateLoaded = 0;
using var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", RuntimeCache.AccessToken);
var response = await client.GetAsync($"{RuntimeCache.CurrentHomeserver}/_matrix/client/r0/rooms/{RoomId}/state");
// var response = await client.GetAsync($"http://localhost:5117/matrix-hq-state.json");
//var _events = await response.Content.ReadFromJsonAsync>();
var _data = await response.Content.ReadAsStreamAsync();
var __events = JsonSerializer.DeserializeAsyncEnumerable(_data);
await foreach (var _ev in __events)
{
var e = new PreRenderedStateEvent()
{
type = _ev.type,
state_key = _ev.state_key,
origin_server_ts = _ev.origin_server_ts,
content = _ev.content.ToJson(indent: true, ignoreNull: true),
};
Events.Add(e);
if (string.IsNullOrEmpty(e.state_key))
{
FilteredEvents.Add(e);
}
StateLoaded++;
if ((DateTime.Now - _lastUpdate).TotalMilliseconds > 100)
{
_lastUpdate = DateTime.Now;
status = $"Loaded {StateLoaded} state events";
StateHasChanged();
await Task.Delay(0);
}
}
StateHasChanged();
}
private async Task RebuildFilteredData()
{
status = "Rebuilding filtered data...";
StateHasChanged();
await Task.Delay(1);
var _FilteredEvents = Events;
if (!ShowMembershipEvents)
_FilteredEvents = _FilteredEvents.Where(x => x.type != "m.room.member").ToList();
status = "Done, rerendering!";
StateHasChanged();
await Task.Delay(1);
FilteredEvents = _FilteredEvents;
StateHasChanged();
}
public struct PreRenderedStateEvent
{
public string content { get; set; }
public long origin_server_ts { get; set; }
public string state_key { get; set; }
public string type { get; set; }
// public string sender { get; set; }
// public string event_id { get; set; }
// public string user_id { get; set; }
// public string replaces_state { get; set; }
}
public bool ShowMembershipEvents
{
get => _showMembershipEvents;
set
{
_showMembershipEvents = value;
RebuildFilteredData();
}
}
private bool _showMembershipEvents;
}