about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/RoomManager/RoomManager.razor
blob: a8b8fd4f990dc13ba4c0476022428ea89bdc6907 (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
@page "/RoomManager"
@inject ILocalStorageService LocalStorage
@inject NavigationManager NavigationManager
<h3>Room manager</h3>
<hr/>
@if (Rooms.Count == 0)
{
    <p>You are not in any rooms!</p>
    @* <p>Loading progress: @checkedRoomCount/@totalRoomCount</p> *@
}
else
{
    <p>You are in @Rooms.Count rooms and @Spaces.Count spaces</p>
    <p>
        <a href="/RoomManagerCreateRoom">Create room</a>
    </p>

    <details open>
        <summary>Space List</summary>
        @foreach (var room in Spaces)
        {
            <a style="color: unset; text-decoration: unset;" href="/RoomManager/Space/@room.RoomId.Replace('.', '~')">
                <RoomListItem Room="@room" ShowOwnProfile="false"></RoomListItem>
            </a>
        }
    </details>
    <details open>
        <summary>Room List</summary>
        @foreach (var room in Rooms)
        {
            <a style="color: unset; text-decoration: unset;" href="/RoomManager/Room/@room.RoomId.Replace('.', '~')">
                <RoomListItem Room="@room" ShowOwnProfile="true"></RoomListItem>
            </a>
        }
    </details>
}

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

@code {
    public List<Room> Rooms { get; set; } = new();
    public List<Room> Spaces { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        Console.WriteLine("Initializing room manager");
        await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage);
        Console.WriteLine("Loaded from local storage");
        await base.OnInitializedAsync();
        Console.WriteLine("Initialized base");
        if (RuntimeCache.CurrentHomeServer == null)
        {
            NavigationManager.NavigateTo("/Login");
            return;
        }
        Console.WriteLine("Fetching joined rooms");
        var _rooms = await RuntimeCache.CurrentHomeServer.GetJoinedRooms();
        StateHasChanged();
        Console.WriteLine($"Got {_rooms.Count} rooms");
        var semaphore = new SemaphoreSlim(10);
        var tasks = new List<Task<Room?>>();
        foreach (var room in _rooms)
        {
            tasks.Add(CheckIfSpace(room, semaphore));
        }
        await Task.WhenAll(tasks);

        Console.WriteLine("Fetched joined rooms!");
    }

    private async Task<Room?> CheckIfSpace(Room room, SemaphoreSlim semaphore)
    {
        await semaphore.WaitAsync();
        // Console.WriteLine($"Checking if {room.RoomId} is a space");
        try
        {
            var state = await room.GetStateAsync<CreateEvent>("m.room.create");
            if (state != null)
            {
    //Console.WriteLine(state.Value.ToJson());
                if (state.Type != null)
                {
                    if (state.Type == "m.space")
                    {
                        Console.WriteLine($"Room {room.RoomId} is a space!");
                        Spaces.Add(room);
                        StateHasChanged();
                        return room;
                    }
                    else
                    {
                        Console.WriteLine($"Encountered unknown room type {state.Type}");
                    }
                }
                else
                {
                    Rooms.Add(room);
    //this is fine, apprently...
    // Console.WriteLine($"Room {room.RoomId} has no Content.type in m.room.create!");
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return null;
        }
        finally
        {
            semaphore.Release();
        }
        return null;
    }

}