@page "/Rooms/Create" @using System.Text.Json @using System.Reflection @using ArcaneLibs.Extensions @using LibMatrix @using LibMatrix.Extensions @using LibMatrix.Helpers @using LibMatrix.Homeservers @using LibMatrix.Responses @using LibMatrix.StateEventTypes.Spec @using MatrixRoomUtils.Web.Classes.RoomCreationTemplates @* @* ReSharper disable once RedundantUsingDirective - Must not remove this, Rider marks this as "unused" when it's not */ *@

Room Manager - Create Room

@*
@JsonString
*@ @if (creationEvent is not null) {
@creationEvent.PowerLevelContentOverride.Users.Count members @foreach (var user in creationEvent.PowerLevelContentOverride.Events.Keys) { var _event = user;
} @foreach (var user in creationEvent.PowerLevelContentOverride.Users.Keys) { var _user = user; } @* Initial states, should remain at bottom *@ }
Preset: @if (Presets is null) {

Presets is null!

} else { @foreach (var createRoomRequest in Presets) { } }
Room name: @if (creationEvent.Name is null) {

creationEvent.Name is null!

} else {

(#:@HomeServer.WhoAmI.UserId.Split(':').Last())

}
Room type: @if (creationEvent._creationContentBaseType is null) {

creationEvent._creationContentBaseType is null!

} else { }
History visibility:
Guest access: @(guestAccessEvent.IsGuestAccessEnabled ? "Guests can join" : "Guests cannot join") (@guestAccessEvent.GuestAccess)
Room icon:

Permissions:
:
:
Server ACLs: @if (serverAcl?.Allow is null) {

No allow rules exist!

} else {
@((creationEvent["m.room.server_acls"].TypedContent as ServerACLEventContent).Allow.Count) allow rules @* *@
} @if (serverAcl?.Deny is null) {

No deny rules exist!

} else {
@((creationEvent["m.room.server_acls"].TypedContent as ServerACLEventContent).Deny.Count) deny rules @* *@
}
Invited members:
@creationEvent.InitialState.Count(x => x.Type == "m.room.member") members @* *@ @foreach (var member in creationEvent.InitialState.Where(x => x.Type == "m.room.member" && x.StateKey != HomeServer.UserId)) { }
Initial states:
@code { private static readonly string[] ImplementedStates = { "m.room.avatar", "m.room.history_visibility", "m.room.guest_access", "m.room.server_acl" }; } @creationEvent.InitialState.Count(x => !ImplementedStates.Contains(x.Type)) custom states @foreach (var initialState in creationEvent.InitialState.Where(x => !ImplementedStates.Contains(x.Type))) { }
@(initialState.Type): @if (!string.IsNullOrEmpty(initialState.StateKey)) {
(@initialState.StateKey) }
@JsonSerializer.Serialize(initialState.RawContent, new JsonSerializerOptions { WriteIndented = true })
@creationEvent.InitialState.Count initial states @foreach (var initialState in creationEvent.InitialState) { var _state = initialState; }
@(_state.Type):
@JsonSerializer.Serialize(_state.RawContent, new JsonSerializerOptions { WriteIndented = true })

        @creationEvent.ToJson(ignoreNull: true)
    
        @creationEvent.ToJson()
    
@if (_matrixException is not null) {
            @_matrixException.Message
        
} @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? Presets { get; set; } = new(); private AuthenticatedHomeserverGeneric? HomeServer { get; set; } private MatrixException? _matrixException { get; set; } private HistoryVisibilityEventContent? historyVisibility => creationEvent?["m.room.history_visibility"].TypedContent as HistoryVisibilityEventContent; private GuestAccessEventContent? guestAccessEvent => creationEvent?["m.room.guest_access"].TypedContent as GuestAccessEventContent; private ServerACLEventContent? serverAcl => creationEvent?["m.room.server_acls"].TypedContent as ServerACLEventContent; private RoomAvatarEventContent? roomAvatarEvent => creationEvent?["m.room.avatar"].TypedContent as RoomAvatarEventContent; protected override async Task OnInitializedAsync() { HomeServer = await MRUStorage.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&::MatrixRoomUtils (https://mru.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 }; }