about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Shared/RoomList.razor
blob: 91ebb0b62abe89df946a42691d5e4ddff7ff5166 (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
@using MatrixRoomUtils.Web.Shared.RoomListComponents;
@using LibMatrix
@using LibMatrix.Extensions
@using ArcaneLibs.Extensions
@using LibMatrix.EventTypes.Spec.State
@if(Rooms.Count != RoomsWithTypes.Sum(x=>x.Value.Count)) {
    <p>Fetching room details... @RoomsWithTypes.Sum(x=>x.Value.Count) out of @Rooms.Count done!</p>
    @foreach (var category in RoomsWithTypes.OrderBy(x => x.Value.Count)) {
        <p>@category.Key (@category.Value.Count)</p>
    }
}
else {
    @foreach (var category in RoomsWithTypes.OrderBy(x => x.Value.Count)) {
        <RoomListCategory Category="@category" GlobalProfile="@GlobalProfile"></RoomListCategory>
    }
}

@code {

    [Parameter]
    public List<RoomInfo> Rooms { get; set; }
    [Parameter]
    public ProfileResponseEventContent? GlobalProfile { get; set; }

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

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

        GlobalProfile ??= await hs.GetProfileAsync(hs.WhoAmI.UserId);
        if (RoomsWithTypes.Any()) return;

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

        await base.OnInitializedAsync();
    }

    private string GetRoomTypeName(string? roomType) => roomType switch {
        "m.space" => "Space",
        "msc3588.stories.stories-room" => "Story room",
        "support.feline.policy.lists.msc.v1" => "MSC3784 Policy list (v1)",
        null => "Room",
        _ => roomType
        };


    private static SemaphoreSlim _semaphoreSlim = new(8, 8);
    private async Task ProcessRoom(RoomInfo room) {
        await _semaphoreSlim.WaitAsync();
        string roomType;
        try {
            var createEvent = (await room.GetStateEvent("m.room.create")).TypedContent as RoomCreateEventContent;
            roomType = GetRoomTypeName(createEvent.Type);

            if (roomType == "Room") {
                var mjolnirData = await room.GetStateEvent("org.matrix.mjolnir.shortcode");
                if(mjolnirData?.RawContent?.ToJson(ignoreNull: true) is not null and not "{}")
                    roomType = "Legacy policy room";
            }
        }
        catch (MatrixException e) {
            roomType = $"Error: {e.ErrorCode}";
        }

        if (!RoomsWithTypes.ContainsKey(roomType)) {
            RoomsWithTypes.Add(roomType, new List<RoomInfo>());
        }
        RoomsWithTypes[roomType].Add(room);

            StateHasChanged();
        _semaphoreSlim.Release();
    }

}