about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/Tools/SpaceDebug.razor
blob: 5b4815dfa72779d01c11f79c409eb10209c82e09 (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
@page "/SpaceDebug"
@using LibMatrix.Filters
@using LibMatrix.Helpers
<h3>SpaceDebug</h3>
<hr/>

<p>@Status</p>

<b>Has parent:</b>
<br/>

@foreach (var (roomId, parents) in SpaceParents) {
    <p>@roomId's parents</p>
    <ul>
        @foreach (var parent in parents) {
            <li>@parent</li>
        }
    </ul>
}

<b>Space children:</b>

@foreach (var (roomId, children) in SpaceChildren) {
    <p>@roomId's children</p>
    <ul>
        @foreach (var child in children) {
            <li>@child</li>
        }
    </ul>
}

@code {
    private string _status = "Loading...";

    public string Status {
        get => _status;
        set {
            _status = value;
            StateHasChanged();
        }
    }

    public Dictionary<string, List<string>> SpaceChildren { get; set; } = new();
    public Dictionary<string, List<string>> SpaceParents { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        Status = "Getting homeserver...";
        var hs = await MRUStorage.GetCurrentSessionOrNavigate();
        if (hs is null) return;

        var syncHelper = new SyncHelper(hs) {
            Filter = new SyncFilter() {
                Presence = new(0),
                Room = new() {
                    AccountData = new(limit: 0),
                    Ephemeral = new(limit: 0),
                    State = new(limit: 1000, types: new() { "m.space.child", "m.space.parent" }),
                    Timeline = new(limit: 0)
                },
                AccountData = new(limit: 0)
            }
        };
        
        Status = "Syncing...";

        var syncs = syncHelper.EnumerateSyncAsync();
        await foreach (var sync in syncs) {
            if (sync is null) {
                Status = "Sync failed";
                continue;
            }

            if (sync.Rooms is null) {
                Status = "No rooms in sync...";
                break;
            }

            if (sync.Rooms.Join is null) {
                Status = "No joined rooms in sync...";
                break;
            }

            if (sync.Rooms.Join.Count == 0) {
                Status = "Joined rooms list was empty...";
                break;
            }

            // nextBatch = sync.NextBatch;
            foreach (var (roomId, data) in sync.Rooms!.Join!) {
                data.State?.Events?.ForEach(e => {
                    if (e.Type == "m.space.child") {
                        if (!SpaceChildren.ContainsKey(roomId)) SpaceChildren[roomId] = new();
                        if (e.RawContent is null) e.StateKey += " (null)";
                        else if (e.RawContent.Count == 0) e.StateKey += " (empty)";
                        SpaceChildren[roomId].Add(e.StateKey);
                    }
                    if (e.Type == "m.space.parent") {
                        if (!SpaceParents.ContainsKey(roomId)) SpaceParents[roomId] = new();
                        if (e.RawContent is null) e.StateKey += " (null)";
                        else if (e.RawContent.Count == 0) e.StateKey += " (empty)";
                        SpaceParents[roomId].Add(e.StateKey);
                    }
                });
            }
            Status = $"Synced {sync.Rooms.Join.Count} rooms, found {SpaceChildren.Count} spaces, {SpaceParents.Count} parents";
        }
        Status = $"Synced: found {SpaceChildren.Count}->{SpaceChildren.Sum(x => x.Value.Count)} spaces, {SpaceParents.Count}->{SpaceParents.Sum(x => x.Value.Count)} parents!";

        await base.OnInitializedAsync();
    }


}