about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/RoomStateViewerPage.razor
blob: ffd17d42b1b04d481244b14138507103c8fab398 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@page "/RoomStateViewer/{RoomId}"
@using MatrixRoomUtils.Authentication
@using MatrixRoomUtils.Web.Classes
@using Blazored.LocalStorage
@using System.Net.Http.Headers
@using System.Text.Json
@using System.Xml.Schema
@using MatrixRoomUtils.Extensions
@using MatrixRoomUtils.StateEventTypes
@using MatrixRoomUtils.Web.Shared.IndexComponents
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager
<h3>Room state viewer</h3>
<p>Room ID: @RoomId</p>

<p>@status</p>

<input type="checkbox" id="showAll" @bind="ShowMembershipEvents"/> Show member events

<table class="table table-striped table-hover" style="width: fit-content;">
    <thead>
    <tr>
        <th scope="col">Type</th>
        <th scope="col">Content</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var stateEvent in FilteredEvents.Where(x => x.state_key == "").OrderBy(x => x.origin_server_ts))
    {
        <tr>
            <td>@stateEvent.type</td>
            <td style="max-width: fit-content;">
                <pre>@stateEvent.content</pre>
            </td>
        </tr>
    }
    </tbody>
</table>

@foreach (var group in FilteredEvents.GroupBy(x => x.state_key).OrderBy(x => x.Key).Where(x => x.Key != ""))
{
    <details>
        <summary>@group.Key</summary>
        <table class="table table-striped table-hover" style="width: fit-content;">
            <thead>
            <tr>
                <th scope="col">Type</th>
                <th scope="col">Content</th>
            </tr>
            </thead>
            <tbody>
            @foreach (var stateEvent in group.OrderBy(x => x.origin_server_ts))
            {
                <tr>
                    <td>@stateEvent.type</td>
                    <td style="max-width: fit-content;">
                        <pre>@stateEvent.content</pre>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    </details>
}

<LogView></LogView>

@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<PreRenderedStateEvent> FilteredEvents { get; set; } = new();
    public List<PreRenderedStateEvent> Events { get; set; } = new();
    public string status = "";

    protected override async Task OnInitializedAsync()
    {
        if (!RuntimeStorage.WasLoaded) await RuntimeStorage.LoadFromLocalStorage(LocalStorage);
        await base.OnInitializedAsync();
        if (RuntimeStorage.AccessToken == null || RuntimeStorage.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", RuntimeStorage.AccessToken);
    var response = await client.GetAsync($"{RuntimeStorage.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<Queue<StateEventStruct>>();
        var _data = await response.Content.ReadAsStreamAsync();
        var __events = JsonSerializer.DeserializeAsyncEnumerable<StateEventStruct>(_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;
}