about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/User/DMManager.razor
blob: 04ff6e563a20cb1875538a0aade73b74a19222a7 (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
@page "/User/DirectMessages"
@using LibMatrix.Homeservers
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Responses
<h3>Direct Messages</h3>
<hr/>

@foreach (var (targetUser, rooms) in DMRooms) {
    <div>
        <InlineUserItem User="targetUser"></InlineUserItem>
        @foreach (var room in rooms) {
            <RoomListItem RoomInfo="room" LoadData="true"></RoomListItem>
        }
    </div>
}

@code {
    private string? _status;
    private AuthenticatedHomeserverGeneric? HomeServer { get; set; }
    private Dictionary<UserProfileResponse, List<RoomInfo>> DMRooms { get; set; } = new();

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

    protected override async Task OnInitializedAsync() {
        HomeServer = await MRUStorage.GetCurrentSessionOrNavigate();
        if (HomeServer is null) return;
        Status = "Loading global profile...";
        if (HomeServer.WhoAmI?.UserId is null) return;

        Status = "Loading DM list from account data...";
        var dms = await HomeServer.GetAccountDataAsync<Dictionary<string, List<string>>>("m.direct");
        DMRooms.Clear();
        foreach (var (userId, rooms) in dms) {
            var roomList = new List<RoomInfo>();
            DMRooms.Add(await HomeServer.GetProfileAsync(userId), roomList);
            foreach (var room in rooms) {
                roomList.Add(new RoomInfo() { Room = HomeServer.GetRoom(room) });
            }
            StateHasChanged();
        }

        StateHasChanged();
        Status = null;

        await base.OnInitializedAsync();
    }



}