about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/User/DMManager.razor
blob: f753f182b8736af584714bc04f34ee3370bb61c8 (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
@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) {
                var roomInfo = new RoomInfo() { Room = Homeserver.GetRoom(room) };
                roomList.Add(roomInfo);
                roomInfo.StateEvents.Add(new() {
                    Type = RoomNameEventContent.EventId,
                    TypedContent = new RoomNameEventContent() {
                        Name = await Homeserver.GetRoom(room).GetNameOrFallbackAsync(4)
                    }
                });
            }
            StateHasChanged();
        }

        StateHasChanged();
        Status = null;

        await base.OnInitializedAsync();
    }

}