about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/User/DMSpaceStages/DMSpaceStage3.razor
blob: 865e9564af059842f821a26285d19189bb666283 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@using LibMatrix.Homeservers
@using LibMatrix.RoomTypes
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.Responses
@using MatrixUtils.LibDMSpace
@using MatrixUtils.LibDMSpace.StateEvents
@using ArcaneLibs.Extensions
@using System.Text.Json.Serialization
@using MatrixUtils.Abstractions

<b>
    <u>DM Space setup tool - stage 3: Preview space layout</u>
</b>
<p>This gives you a preview of how your settings would impact layout!</p>

@if (!string.IsNullOrWhiteSpace(Status)) {
    <p>@Status</p>
}

@if (SetupData is not null) {
    @if (SetupData.DMSpaceRoomInfo is not null) {
        <p>
            <InputCheckbox @bind-Value="SetupData.DmSpaceInfo.LayerByUser"></InputCheckbox>
            Create sub-spaces per user
        </p>
        @if (!SetupData.DmSpaceInfo.LayerByUser) {
            <RoomListItem RoomInfo="@SetupData.DMSpaceRoomInfo"></RoomListItem>
            @foreach (var (userId, room) in SetupData.DMRooms.OrderBy(x => x.Key.DisplayName)) {
                @foreach (var roomInfo in room) {
                    <div style="margin-left: 32px;">
                        <RoomListItem RoomInfo="@roomInfo"></RoomListItem>
                    </div>
                }
            }
        }
        else {
            <RoomListItem RoomInfo="@SetupData.DMSpaceRoomInfo"></RoomListItem>
            @foreach (var (user, room) in SetupData.DMRooms.OrderBy(x => x.Key.DisplayName)) {
                <div style="margin-left: 32px;">
                    @{
                        RoomInfo fakeRoom = new(SetupData.DMSpaceRoomInfo.Room) {
                            RoomName = user.DisplayName ?? user.Id,
                            RoomIcon = user.AvatarUrl
                        };
                    }
                    <RoomListItem RoomInfo="@fakeRoom"></RoomListItem>
                </div>
                @foreach (var roomInfo in room) {
                    <div style="margin-left: 64px;">
                        <RoomListItem RoomInfo="@roomInfo"></RoomListItem>
                    </div>
                }
            }
        }
    }
    else {
        <b>Error: SetupData.DMSpaceRoomInfo is null!</b>
    }
}
else {
    <b>Error: DMSpaceRootPageConfiguration is null!</b>
}

<br/>
<LinkButton OnClick="@Execute">Next</LinkButton>

@code {

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

    private string? _status;

    [CascadingParameter]
    public DMSpace.DMSpaceSetupData SetupData { get; set; }

    SemaphoreSlim _semaphore = new(1, 1);

    protected override async Task OnInitializedAsync() {
        if (SetupData is null)
            return;
        await _semaphore.WaitAsync();
        var hs = SetupData.Homeserver;
        // var dmSpaceRoom = new DMSpaceRoom(hs, SetupData.DmSpaceConfiguration.DMSpaceId);
        // SetupData.
        // dmSpaceRoomInfo = new() {
        // RoomName = await dmSpaceRoom.GetNameAsync(),
        // CreationEventContent = await dmSpaceRoom.GetCreateEventAsync(),
        // RoomIcon = "mxc://feline.support/uUxBwaboPkMGtbZcAGZaIzpK",
        // Room = dmSpaceRoom
        // };
        // dmSpaceInfo = await dmSpaceRoom.GetDMSpaceInfo();
        // Status = "Loading DM list from account data...";
        // var dms = await SetupData.Homeserver.GetAccountDataAsync<Dictionary<string, List<string>>>("m.direct");

        Status = "DM list optimised, fetching info...";
        // var results = dms.Select(async x => {
        //     var (userId, rooms) = x;
        //     UserProfileWithId userProfile;
        //     try {
        //         var profile = await SetupData.Homeserver.GetProfileAsync(userId);
        //         userProfile = new() {
        //             AvatarUrl = profile.AvatarUrl,
        //             Id = userId,
        //             DisplayName = profile.DisplayName
        //         };
        //     }
        //     catch {
        //         userProfile = new() {
        //             AvatarUrl = "mxc://feline.support/uUxBwaboPkMGtbZcAGZaIzpK",
        //             DisplayName = userId,
        //             Id = userId
        //         };
        //     }
        //     var roomList = new List<RoomInfo>();
        //     var tasks = rooms.Select(x => GetRoomInfo(hs.GetRoom(x))).ToAsyncEnumerable();
        //     await foreach (var result in tasks)
        //         roomList.Add(result);
        //     return (userProfile, roomList);
        // }).ToAsyncEnumerable();
        // await foreach (var res in results) {
        //     dmRooms.Add(new RoomInfo() {
        //         Room = dmSpaceRoom,
        //         RoomIcon = res.userProfile.AvatarUrl,
        //         RoomName = res.userProfile.DisplayName,
        //         CreationEventContent = await dmSpaceRoom.GetCreateEventAsync()
        //     }, res.roomList);
        // }
        await SetupData.DMSpaceRoomInfo!.FetchAllStateAsync();
        _semaphore.Release();
        Status = null;
        await base.OnParametersSetAsync();
    }

    private async Task Execute() {
        var hs = SetupData.Homeserver;
        var dmSpaceRoom = new DMSpaceRoom(hs, SetupData.DmSpaceConfiguration!.DMSpaceId!);
        await dmSpaceRoom.ImportNativeDMs();
        NavigationManager.NavigateTo("/User/DMSpace/Setup?stage=3");
    }

    private async Task<RoomInfo> GetRoomInfo(GenericRoom room) {
        var roomInfo = new RoomInfo(room);
        var roomMembers = new List<UserProfileWithId>();
        roomInfo.CreationEventContent = await room.GetCreateEventAsync();
        try {
            roomInfo.RoomName = await room.GetNameAsync();
        }
        catch { }

        var membersEnum = room.GetMembersEnumerableAsync(true);
        await foreach (var member in membersEnum)
            if (member.TypedContent is RoomMemberEventContent memberEvent)
                roomMembers.Add(new() { DisplayName = memberEvent.DisplayName, AvatarUrl = memberEvent.AvatarUrl, Id = member.StateKey });

        if (string.IsNullOrWhiteSpace(roomInfo.RoomName) || roomInfo.RoomName == room.RoomId) {
            List<string> displayNames = new List<string>();
            foreach (var member in roomMembers)
                if (!string.IsNullOrWhiteSpace(member.DisplayName))
                    displayNames.Add(member.DisplayName);
            roomInfo.RoomName = string.Join(", ", displayNames);
        }

        try {
            string? roomIcon = (await room.GetAvatarUrlAsync())?.Url;
            if (room is not null)
                roomInfo.RoomIcon = roomIcon;
        }
        catch { }

        return roomInfo;
    }

    private async Task<List<RoomInfo>> GetRoomInfoForRooms(List<GenericRoom> rooms) {
        var tasks = rooms.Select(GetRoomInfo).ToList();
        await Task.WhenAll(tasks);
        return tasks.Select(x => x.Result).ToList();
    }

    private class UserProfileWithId : UserProfileResponse {
        [JsonIgnore]
        public string Id { get; set; }
    }

}