about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/RoomList.razor
blob: ac2cbb3f6363dbc1ec6fb5270e239ba04c362c94 (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
@using MatrixRoomUtils.Web.Shared.RoomListComponents;
@using MatrixRoomUtils.Core.StateEventTypes
<p>@Rooms.Count rooms total, @RoomsWithTypes.Sum(x=>x.Value.Count) fetched so far...</p>
@foreach (var category in RoomsWithTypes.OrderBy(x => x.Value.Count)) {
    <RoomListCategory Category="@category"></RoomListCategory>
}

@code {

    [Parameter]
    public List<GenericRoom> Rooms { get; set; }

    Dictionary<string, List<GenericRoom>> RoomsWithTypes = new();

    protected override async Task OnInitializedAsync() {
        if (RoomsWithTypes.Any()) return;

        var tasks = Rooms.Select(AddRoom);
        await Task.WhenAll(tasks);

        await base.OnInitializedAsync();
    }

    private string GetRoomTypeName(string? roomType) => roomType switch {
        "m.space" => "Space",
        "msc3588.stories.stories-room" => "Story room",
        null => "Room",
        _ => roomType
        };

    
    private static SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(8, 8);
    private async Task AddRoom(GenericRoom room) {
        await _semaphoreSlim.WaitAsync();
        var roomType = GetRoomTypeName((await room.GetCreateEventAsync()).Type);

        if (roomType == "Room") {
            var shortcodeState = await room.GetStateAsync<MjolnirShortcodeEventData>("org.matrix.mjolnir.shortcode");
            if (shortcodeState is not null) roomType = "Legacy policy room";
        }
        
        if (!RoomsWithTypes.ContainsKey(roomType)) {
            RoomsWithTypes.Add(roomType, new List<GenericRoom>());
        }
        RoomsWithTypes[roomType].Add(room);

    // if (RoomsWithTypes.Count % 10 == 0)
        StateHasChanged();
        await Task.Delay(100);
        _semaphoreSlim.Release();
    }

    private bool _isSpaceChildrenOpen = false;

}