about summary refs log tree commit diff
path: root/MatrixRoomUtils.Web/Pages/User/DMSpaceStages/DMSpaceStage1.razor
blob: 5958fc588ebead4e0385c4343ad87ee45da442ad (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
@using LibMatrix.Homeservers
@using LibMatrix.RoomTypes
@using LibMatrix
@using LibMatrix.Responses
@using MatrixRoomUtils.LibDMSpace
@using MatrixRoomUtils.LibDMSpace.StateEvents
@using Microsoft.Extensions.Primitives
@using ArcaneLibs.Extensions
<b>
    <u>DM Space setup tool - stage 1: Configure space</u>
</b>
<p>You will need a space to use for DM rooms.</p>
@if (DmSpace is not null) {
    <p>
        Selected space:
        <InputSelect @bind-Value="DmSpace.DmSpaceConfiguration.DMSpaceId">
            @foreach (var (id, name) in spaces) {
                <option value="@id">@name</option>
            }
        </InputSelect>
    </p>
    <p>
        <InputCheckbox @bind-Value="DmSpaceInfo.LayerByUser"></InputCheckbox>
        Create sub-spaces per user
    </p>
}
else {
    <b>Error: DmSpaceConfiguration is null!</b>
}

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

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

@code {

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

    private Dictionary<string, string> spaces = new() { { "", "New space" } };
    private string? _status;

    [CascadingParameter]
    public DMSpace? DmSpace { get; set; }

    public DMSpaceInfo? DmSpaceInfo { get; set; } = new();

    protected override async Task OnInitializedAsync() {
        await base.OnInitializedAsync();
    }

    SemaphoreSlim _semaphoreSlim = new(1, 1);
    protected override async Task OnParametersSetAsync() {
        if (DmSpace is null)
            return;
        await _semaphoreSlim.WaitAsync();
        DmSpace.DmSpaceConfiguration ??= new();
        if (spaces.Count == 1) {
            Status = "Looking for spaces...";
            var userRoomsEnum = DmSpace.Homeserver.GetJoinedRoomsByType("m.space");
            List<GenericRoom> userRooms = new(); 
            await foreach (var room in userRoomsEnum) {
                userRooms.Add(room);
            }
            var roomChecks = userRooms.Select(GetFeasibleSpaces).ToAsyncEnumerable();
            await foreach(var room in roomChecks)
                if(room.HasValue)
                    spaces.TryAdd(room.Value.id, room.Value.name);
            
            Status = "Done!";
        }
        _semaphoreSlim.Release();
        await base.OnParametersSetAsync();
    }

    private async Task Execute() {
        if (string.IsNullOrWhiteSpace(DmSpace.DmSpaceConfiguration.DMSpaceId)) {
            var crr = CreateRoomRequest.CreatePrivate(DmSpace.Homeserver, "Direct Messages");
            crr.CreationContentBaseType.Type = "m.space";
            DmSpace.DmSpaceConfiguration.DMSpaceId = (await DmSpace.Homeserver.CreateRoom(crr)).RoomId;
        }
        await DmSpace.Homeserver!.SetAccountDataAsync(DMSpaceConfiguration.EventId, DmSpace.DmSpaceConfiguration);
        var space = DmSpace.Homeserver.GetRoom(DmSpace.DmSpaceConfiguration.DMSpaceId);
        await space.SendStateEventAsync(DMSpaceInfo.EventId, DmSpaceInfo);

        NavigationManager.NavigateTo("/User/DMSpace/Setup?stage=2");
    }

    public async Task<(string id, string name)?> GetFeasibleSpaces(GenericRoom room) {
        try {
            var pls = await room.GetPowerLevelsAsync();
            if (!pls.UserHasStatePermission(DmSpace.Homeserver.WhoAmI.UserId, "m.space.child")) {
                Console.WriteLine($"No permission to send m.space.child in {room.RoomId}...");
                return null;
            }
            var roomName = await room.GetNameAsync();
            Status = $"Found viable space: {roomName}";
            if (string.IsNullOrWhiteSpace(DmSpace.DmSpaceConfiguration.DMSpaceId)) {
                try {
                    var dsi = await DmSpace.Homeserver.GetRoom(room.RoomId).GetStateOrNullAsync<DMSpaceInfo>(DMSpaceInfo.EventId) ?? new DMSpaceInfo();
                    if (await room.GetStateOrNullAsync<DMSpaceInfo>(DMSpaceInfo.EventId) is not null && dsi is not null) {
                        DmSpace.DmSpaceConfiguration.DMSpaceId = room.RoomId;
                        DmSpaceInfo = dsi;
                    }
                }
                catch (MatrixException e) {
                    if (e.ErrorCode == "M_NOT_FOUND") Console.WriteLine($"{room.RoomId} is not a DM space.");
                    else throw;
                }
            }
            return (room.RoomId, roomName);
        }
        catch (MatrixException e) {
            if (e.ErrorCode == "M_NOT_FOUND") Console.WriteLine($"m.room.power_levels does not exist in {room.RoomId}!!!");
            else throw;
        }
        return null;
    }

}