about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Dev/DevUtilities.razor
blob: f6392a4827147a031398c16f13f2bd63aecc0f32 (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
@page "/Dev/Utilities"
@using ArcaneLibs.Extensions
@using LibMatrix.EventTypes.Spec.Ephemeral
@using LibMatrix.EventTypes.Spec.State.RoomInfo
@using LibMatrix.Helpers
@using MatrixUtils.Abstractions

<h3>Debug Tools</h3>
<hr/>
<LinkButton href="/Dev/WellKnownRes">Well known res tests</LinkButton>
@if (Rooms.Count == 0) {
    <p>You are not in any rooms!</p>
    @* <p>Loading progress: @checkedRoomCount/@totalRoomCount</p> *@
}
else {
    <details>
        <summary>Room List</summary>
        @foreach (var roomId in Rooms) {
            <a style="color: unset; text-decoration: unset;" href="/RoomStateViewer/@roomId.Replace('.', '~')">
                <RoomListItem Homeserver="hs" RoomInfo="@(new RoomInfo(hs.GetRoom(roomId)))" LoadData="true"></RoomListItem>
            </a>
        }
    </details>
}

<details open>
    <summary>Send GET request to URL</summary>
    <div class="input-group">
        <input type="text" class="form-control" @bind-value="GetRequestUrl" placeholder="URL">
        <button class="btn btn-outline-secondary" type="button" @onclick="SendGetRequest">Send</button>
    </div>
    <br/>
    <pre>@GetRequestResult</pre>
</details>

<div style="margin-bottom: 4em;"></div>
<LogView></LogView>

@code {
    public List<string> Rooms { get; set; } = new();
    public AuthenticatedHomeserverGeneric? hs { get; set; }

    protected override async Task OnInitializedAsync() {
        await base.OnInitializedAsync();
        hs = await sessionStore.GetCurrentHomeserver(navigateOnFailure: true);
        if (hs == null) return;
        Rooms = (await hs.GetJoinedRooms()).Select(x => x.RoomId).ToList();
        Console.WriteLine("Fetched joined rooms!");
    }

    //send req
    string GetRequestUrl { get; set; } = "";
    string GetRequestResult { get; set; } = "";

    private async Task SendGetRequest() {
        var httpClient = hs?.ClientHttpClient;
        try {
            var res = await httpClient.GetAsync(GetRequestUrl);
            if (res.IsSuccessStatusCode) {
                if (res.Content.Headers.ContentType.MediaType == "application/json")
                    GetRequestResult = (await res.Content.ReadFromJsonAsync<object>()).ToJson();
                else
                    GetRequestResult = await res.Content.ReadAsStringAsync();
                StateHasChanged();
                return;
            }

            if (res.Content.Headers.ContentType.MediaType == "application/json")
                GetRequestResult = $"Error: {res.StatusCode}\n" + (await res.Content.ReadFromJsonAsync<object>()).ToJson();
            else
                GetRequestResult = $"Error: {res.StatusCode}\n" + await res.Content.ReadAsStringAsync();
        }
        catch (Exception e) {
            GetRequestResult = $"Error: {e}";
        }

        StateHasChanged();
    }

    private async Task TestRoomBuilder() {
        var rb = new RoomBuilder() {
            HistoryVisibility = new RoomHistoryVisibilityEventContent() { HistoryVisibility = RoomHistoryVisibilityEventContent.HistoryVisibilityTypes.Shared },
            ImportantState = [
                new() {
                    RawContent = new() {
                        ["type"] = "m.room.name",
                        ["name"] = "Test Room"
                    }
                },
                new() {
                    Type = "test",
                    TypedContent = new PresenceEventContent() {
                        Presence = "online",
                        LastActiveAgo = 0,
                    }
                },

            ]
        };

        await rb.Create(hs);
    }

}