about summary refs log tree commit diff
path: root/MatrixUtils.Web/Pages/Rooms/Create.razor
blob: 3527bf5e90c2202f3a7ec22e3d753e53f2bcbcdc (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
@page "/Rooms/Create"
@using System.Text.Json
@using System.Reflection
@using ArcaneLibs.Extensions
@using LibMatrix
@using LibMatrix.EventTypes.Spec.State
@using LibMatrix.EventTypes.Spec.State.RoomInfo
@using LibMatrix.Responses
@using MatrixUtils.Web.Classes.RoomCreationTemplates
@using Microsoft.AspNetCore.Components.Forms
@* @* ReSharper disable once RedundantUsingDirective - Must not remove this, Rider marks this as "unused" when it's not */ *@

<h3>Room Manager - Create Room</h3>

@* <pre Contenteditable="true" @onkeypress="@JsonChanged" content="JsonString">@JsonString</pre> *@
<style>
     table.table-top-first-tr tr td:first-child {
         vertical-align: top;
     }
 </style>
<table class="table-top-first-tr">
    <tr style="padding-bottom: 16px;">
        <td>Preset:</td>
        <td>
            @if (Presets is null) {
                <p style="color: red;">Presets is null!</p>
            }
            else {
                <InputSelect @bind-Value="@RoomPreset">
                    @foreach (var createRoomRequest in Presets) {
                        <option value="@createRoomRequest.Key">@createRoomRequest.Key</option>
                    }
                </InputSelect>
            }
        </td>
    </tr>
    @if (creationEvent is not null) {
        <tr>
            <td>Room name:</td>
            <td>
                @if (creationEvent.Name is null) {
                    <p style="color: red;">creationEvent.Name is null!</p>
                }
                else {
                    <FancyTextBox @bind-Value="@creationEvent.Name"></FancyTextBox>
                    <p>(#<FancyTextBox @bind-Value="@creationEvent.RoomAliasName"></FancyTextBox>:@Homeserver.WhoAmI.UserId.Split(':').Last())</p>
                }
            </td>
        </tr>
        <tr>
            <td>Room type:</td>
            <td>
                @if (creationEvent.CreationContentBaseType is null) {
                    <p style="color: red;">creationEvent._creationContentBaseType is null!</p>
                }
                else {
                    <InputSelect @bind-Value="@creationEvent.CreationContentBaseType.Type">
                        <option value="">Room</option>
                        <option value="m.space">Space</option>
                    </InputSelect>
                    <FancyTextBox @bind-Value="@creationEvent.CreationContentBaseType.Type"></FancyTextBox>
                }
            </td>
        </tr>
        <tr>
            <td style="padding-top: 16px;">History visibility:</td>
            <td style="padding-top: 16px;">
                <InputSelect @bind-Value="@historyVisibility.HistoryVisibility">
                    <option value="invited">Invited</option>
                    <option value="joined">Joined</option>
                    <option value="shared">Shared</option>
                    <option value="world_readable">World readable</option>
                </InputSelect>
            </td>
        </tr>
        <tr>
            <td>Guest access:</td>
            <td>
                <ToggleSlider @bind-Value="guestAccessEvent.IsGuestAccessEnabled">
                    @(guestAccessEvent.IsGuestAccessEnabled ? "Guests can join" : "Guests cannot join") (@guestAccessEvent.GuestAccess)
                </ToggleSlider>
                <InputSelect @bind-Value="@guestAccessEvent.GuestAccess">
                    <option value="can_join">Can join</option>
                    <option value="forbidden">Forbidden</option>
                </InputSelect>
            </td>
        </tr>

        <tr>
            <td>Room icon:</td>
            <td>
                @* <img src="@Homeserver.ResolveMediaUri(roomAvatarEvent.Url)" style="width: 128px; height: 128px; border-radius: 50%;"/> *@
                <div style="display: inline-block; vertical-align: middle;">
                    <FancyTextBox @bind-Value="@roomAvatarEvent.Url"></FancyTextBox><br/>
                    <InputFile OnChange="RoomIconFilePicked"></InputFile>
                </div>
            </td>
        </tr>
        <tr>
            <td>Permissions:</td>
            <details>
                <summary>@creationEvent.PowerLevelContentOverride.Users.Count members</summary>
                @foreach (var user in creationEvent.PowerLevelContentOverride.Events.Keys) {
                    var _event = user;
                    <tr>
                        <td>
                            <FancyTextBox Formatter="@GetPermissionFriendlyName"
                                          Value="@_event"
                                          ValueChanged="val => { creationEvent.PowerLevelContentOverride.Events.ChangeKey(_event, val); }">
                            </FancyTextBox>:
                        </td>
                        <td>
                            <input type="number" value="@creationEvent.PowerLevelContentOverride.Events[_event]" @oninput="val => { creationEvent.PowerLevelContentOverride.Events[_event] = int.Parse(val.Value.ToString()); }" @onfocusout="() => { creationEvent.PowerLevelContentOverride.Events = creationEvent.PowerLevelContentOverride.Events.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value); }"/>
                        </td>
                    </tr>
                }
                @foreach (var user in creationEvent.PowerLevelContentOverride.Users.Keys) {
                    var _user = user;
                    <tr>
                        <td><FancyTextBox Value="@_user" ValueChanged="val => { creationEvent.PowerLevelContentOverride.Users.ChangeKey(_user, val); creationEvent.PowerLevelContentOverride.Users = creationEvent.PowerLevelContentOverride.Users.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value); }"></FancyTextBox>:</td>
                        <td>
                            <input type="number" value="@creationEvent.PowerLevelContentOverride.Users[_user]" @oninput="val => { creationEvent.PowerLevelContentOverride.Users[_user] = int.Parse(val.Value.ToString()); }"/>
                        </td>
                    </tr>
                }
            </details>
        </tr>
        <tr>
            <td>Server ACLs:</td>
            <td>
                @if (serverAcl?.Allow is null) {
                    <p>No allow rules exist!</p>
                    <button @onclick="@(() => { serverAcl.Allow = new List<string> { "*" }; })">Create sane defaults</button>
                }
                else {
                    <details>
                        <summary>@((creationEvent["m.room.server_acls"].TypedContent as RoomServerACLEventContent).Allow.Count) allow rules</summary>
                        @* <StringListEditor @bind-Items="@serverAcl.Allow"></StringListEditor> *@
                    </details>
                }
                @if (serverAcl?.Deny is null) {
                    <p>No deny rules exist!</p>
                    <button @onclick="@(() => { serverAcl.Allow = new List<string>(); })">Create sane defaults</button>
                }
                else {
                    <details>
                        <summary>@((creationEvent["m.room.server_acls"].TypedContent as RoomServerACLEventContent).Deny.Count) deny rules</summary>
                        @* <StringListEditor @bind-Items="@serverAcl.Allow"></StringListEditor> *@
                    </details>
                }
            </td>
        </tr>

        <tr>
            <td>Invited members:</td>
            <td>
                <details>
                    <summary>@creationEvent.InitialState.Count(x => x.Type == "m.room.member") members</summary>
                    @* <button @onclick="() => { RuntimeCache.LoginSessions.Select(x => x.Value.LoginResponse.UserId).ToList().ForEach(InviteMember); }">Invite all logged in accounts</button> *@
                    @foreach (var member in creationEvent.InitialState.Where(x => x.Type == "m.room.member" && x.StateKey != Homeserver.UserId)) {
                        <UserListItem UserId="@member.StateKey"></UserListItem>
                    }
                </details>
            </td>
        </tr>
        @* Initial states, should remain at bottom *@
        <tr>
            <td style="vertical-align: top;">Initial states:</td>
            <td>
                <details>

                    @code
                    {
                        private static readonly string[] ImplementedStates = { "m.room.avatar", "m.room.history_visibility", "m.room.guest_access", "m.room.server_acl" };
                    }

                    <summary> @creationEvent.InitialState.Count(x => !ImplementedStates.Contains(x.Type)) custom states</summary>
                    <table>
                        @foreach (var initialState in creationEvent.InitialState.Where(x => !ImplementedStates.Contains(x.Type))) {
                            <tr>
                                <td style="vertical-align: top;">
                                    @(initialState.Type):
                                    @if (!string.IsNullOrEmpty(initialState.StateKey)) {
                                        <br/>
                                        <span>(@initialState.StateKey)</span>
                                    }

                                </td>
                                <td>
                                    <pre>@JsonSerializer.Serialize(initialState.RawContent, new JsonSerializerOptions { WriteIndented = true })</pre>
                                </td>
                            </tr>
                        }
                    </table>
                </details>
                <details>
                    <summary> @creationEvent.InitialState.Count initial states</summary>
                    <table>
                        @foreach (var initialState in creationEvent.InitialState) {
                            var _state = initialState;
                            <tr>
                                <td style="vertical-align: top;">
                                    <span>@(_state.Type):</span><br/>
                                    <button @onclick="() => { creationEvent.InitialState.Remove(_state); StateHasChanged(); }">Remove</button>
                                </td>

                                <td>
                                    <pre>@JsonSerializer.Serialize(_state.RawContent, new JsonSerializerOptions { WriteIndented = true })</pre>
                                </td>
                            </tr>
                        }
                    </table>
                </details>
            </td>
        </tr>
    }
</table>
<button @onclick="CreateRoom">Create room</button>
<br/>
<ModalWindow Title="Creation JSON">
    <pre>
        @creationEvent.ToJson(ignoreNull: true)
    </pre>
</ModalWindow>
<ModalWindow Title="Creation JSON (with null values)">
    <pre>
        @creationEvent.ToJson()
    </pre>
</ModalWindow>

@if (_matrixException is not null) {
    <ModalWindow Title="@("Matrix exception: " + _matrixException.ErrorCode)">
        <pre>
            @_matrixException.Message
        </pre>
    </ModalWindow>
}

@code {

    private string RoomPreset {
        get => Presets.ContainsValue(creationEvent) ? Presets.First(x => x.Value == creationEvent).Key : "Not a preset";
        set {
            creationEvent = Presets[value];
            JsonChanged();
            StateHasChanged();
        }
    }

    private CreateRoomRequest? creationEvent { get; set; }

    private Dictionary<string, CreateRoomRequest>? Presets { get; set; } = new();
    private AuthenticatedHomeserverGeneric? Homeserver { get; set; }

    private MatrixException? _matrixException { get; set; }

    private RoomHistoryVisibilityEventContent? historyVisibility => creationEvent?["m.room.history_visibility"].TypedContent as RoomHistoryVisibilityEventContent;
    private RoomGuestAccessEventContent? guestAccessEvent => creationEvent?["m.room.guest_access"].TypedContent as RoomGuestAccessEventContent;
    private RoomServerACLEventContent? serverAcl => creationEvent?["m.room.server_acls"].TypedContent as RoomServerACLEventContent;
    private RoomAvatarEventContent? roomAvatarEvent => creationEvent?["m.room.avatar"].TypedContent as RoomAvatarEventContent;

    protected override async Task OnInitializedAsync() {
        Homeserver = await RMUStorage.GetCurrentSessionOrNavigate();
        if (Homeserver is null) return;

        foreach (var x in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().Contains(typeof(IRoomCreationTemplate))).ToList()) {
            Console.WriteLine($"Found room creation template in class: {x.FullName}");
            var instance = (IRoomCreationTemplate)Activator.CreateInstance(x);
            Presets[instance.Name] = instance.CreateRoomRequest;
        }
        Presets = Presets.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);

        if (!Presets.ContainsKey("Default")) {
            Console.WriteLine($"No default room found in {Presets.Count} presets: {string.Join(", ", Presets.Keys)}");
        }
        else RoomPreset = "Default";

        await base.OnInitializedAsync();
    }

    private void JsonChanged() => Console.WriteLine(creationEvent.ToJson());

    private async Task RoomIconFilePicked(InputFileChangeEventArgs obj) {
        var res = await Homeserver.UploadFile(obj.File.Name, obj.File.OpenReadStream(), obj.File.ContentType);
        Console.WriteLine(res);
        (creationEvent["m.room.avatar"].TypedContent as RoomAvatarEventContent).Url = res;
        StateHasChanged();
    }

    private async Task CreateRoom() {
        Console.WriteLine("Create room");
        Console.WriteLine(creationEvent.ToJson());
        creationEvent.CreationContent.Add("rory.gay.created_using", "Rory&::MatrixUtils (https://rmu.rory.gay)");
        try {
            var id = await Homeserver.CreateRoom(creationEvent);
        }
        catch (MatrixException e) {
            _matrixException = e;
        }
    }

    private void InviteMember(string mxid) {
        if (!creationEvent.InitialState.Any(x => x.Type == "m.room.member" && x.StateKey == mxid) && Homeserver.UserId != mxid)
            creationEvent.InitialState.Add(new StateEvent {
                Type = "m.room.member",
                StateKey = mxid,
                TypedContent = new RoomMemberEventContent {
                    Membership = "invite",
                    Reason = "Automatically invited at room creation time."
                }
            });
    }

    private string GetStateFriendlyName(string key) => key switch {
        "m.room.history_visibility" => "History visibility",
        "m.room.guest_access" => "Guest access",
        "m.room.join_rules" => "Join rules",
        "m.room.server_acl" => "Server ACL",
        "m.room.avatar" => "Avatar",
        _ => key
        };

    private string GetPermissionFriendlyName(string key) => key switch {
        "m.reaction" => "Send reaction",
        "m.room.avatar" => "Change room icon",
        "m.room.canonical_alias" => "Change room alias",
        "m.room.encryption" => "Enable encryption",
        "m.room.history_visibility" => "Change history visibility",
        "m.room.name" => "Change room name",
        "m.room.power_levels" => "Change power levels",
        "m.room.tombstone" => "Upgrade room",
        "m.room.topic" => "Change room topic",
        "m.room.pinned_events" => "Pin events",
        "m.room.server_acl" => "Change server ACLs",
        _ => key
        };

}