From a357bec1831611758a19bf23ff0fa5a5fe99ca52 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sun, 28 May 2023 11:30:53 +0200 Subject: Add changes --- MatrixRoomUtils.Web/Pages/LoginPage.razor | 41 +- .../Pages/RoomManager/RoomManagerCreateRoom.razor | 494 ++++++++++++--------- 2 files changed, 290 insertions(+), 245 deletions(-) (limited to 'MatrixRoomUtils.Web/Pages') diff --git a/MatrixRoomUtils.Web/Pages/LoginPage.razor b/MatrixRoomUtils.Web/Pages/LoginPage.razor index 9fcedd1..c46dc9a 100644 --- a/MatrixRoomUtils.Web/Pages/LoginPage.razor +++ b/MatrixRoomUtils.Web/Pages/LoginPage.razor @@ -1,41 +1,21 @@ @page "/Login" @using System.Text.Json @using MatrixRoomUtils.Core.Authentication +@using MatrixRoomUtils.Web.Shared.SimpleComponents @inject ILocalStorageService LocalStorage @inject IJSRuntime JsRuntime

Login


- - @if (inputVisible.username) - { - - } - else - { - @newRecordInput.username - } - - @if (inputVisible.homeserver) - { - - } - else - { - @newRecordInput.homeserver - } + @@: - @if (inputVisible.password) - { - - } - else - { - @string.Join("", newRecordInput.password.Select(x => '*')) - } +
@@ -63,7 +43,6 @@ @code { List<(string homeserver, string username, string password)> records = new(); (string homeserver, string username, string password) newRecordInput = ("", "", ""); - (bool homeserver, bool username, bool password) inputVisible = (false, false, false); async Task Login() { @@ -106,14 +85,6 @@ } } - - private ElementReference elementToFocus; - - protected override async Task OnAfterRenderAsync(bool firstRender) - { - await JsRuntime.InvokeVoidAsync("BlazorFocusElement", elementToFocus); - } - private void AddRecord() { records.Add(newRecordInput); diff --git a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerCreateRoom.razor b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerCreateRoom.razor index 8cbbca6..c43e276 100644 --- a/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerCreateRoom.razor +++ b/MatrixRoomUtils.Web/Pages/RoomManager/RoomManagerCreateRoom.razor @@ -2,12 +2,21 @@ @using System.Text.Json @using MatrixRoomUtils.Core.Extensions @using MatrixRoomUtils.Core.Responses +@using MatrixRoomUtils.Web.Shared.SimpleComponents +@using System.Reflection @using System.Runtime.Intrinsics.X86 +@using System.Text.Json.Serialization +@using MatrixRoomUtils.Web.Classes.RoomCreationTemplates

Room Manager - Create Room

@*
@JsonString
*@ - - + +
+ - - - - - - - - - - - - - - - - - - - - + @if (creationEvent != null) + { + + + + + + + + + + + + + + + + + + + + - - - - - + + + - - + + +
- @code{ - - private static readonly string[] ImplementedStates = { "m.room.avatar", "m.room.history_visibility", "m.room.guest_access", }; - + @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; + + + + } - @creationEvent.InitialState.Count(x => !ImplementedStates.Contains(x.Type)) custom states -
Preset: @@ -15,105 +24,184 @@ { } - @* *@ - @* *@ - @* *@ - -
Room name: - -
Room alias (localpart): - -
Room type: - - - - - -
History visibility: - - - - - - -
Guest access: - - -
Room name: + +
Room alias (localpart): + +
Room type: + + + + + +
History visibility: + + + + + + +
Guest access: +@code +{ + bool test { get; set; } = true; + GuestAccessContent a => creationEvent["m.room.guest_access"].As().Content; +} +@(a.IsGuestAccessEnabled ? "Guests can join" : "Guests cannot join") + @* *@ + @* *@ + @* *@ + @* *@ +
Room icon: - - @* *@ -
Room icon: + +
+
+ +
-
Initial states: +
Permissions:
: + +
: + +
- @foreach (var initialState in creationEvent.InitialState.Where(x => !ImplementedStates.Contains(x.Type))) - { - - - - - - } -
@(initialState.Type): -
@JsonSerializer.Serialize(initialState.Content, new JsonSerializerOptions { WriteIndented = true })
-
-
- @creationEvent.InitialState.Count initial states - - @foreach (var initialState in creationEvent.InitialState.Where(x => !new[] { "m.room.avatar", "m.room.history_visibility" }.Contains(x.Type))) + + + + + + + + + - + + } + + + + + @* Initial states, should remain at bottom? *@ + + + + - } -
Server ACLs: +
+ @(creationEvent.ServerACLs.Allow.Count) allow rules + +
+
+ @creationEvent.ServerACLs.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 != RuntimeCache.CurrentHomeServer.UserId)) { -
@(initialState.Type):
Initial states: +
+ + @code{ + + private static readonly string[] ImplementedStates = { "m.room.avatar", "m.room.history_visibility", "m.room.guest_access", "m.room.server_acl" }; -
-
@JsonSerializer.Serialize(initialState.Content, new JsonSerializerOptions { WriteIndented = true })
-
-
- - + + @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.Content, new JsonSerializerOptions { WriteIndented = true })
+
+ +
+ @creationEvent.InitialState.Count initial states + + @foreach (var initialState in creationEvent.InitialState) + { + var _state = initialState; + + + + + + } +
+ @(_state.Type):
+ +
+
@JsonSerializer.Serialize(_state.Content, new JsonSerializerOptions { WriteIndented = true })
+
+
+ + + } +
Creation JSON @@ -123,22 +211,14 @@
Creation JSON (with null values) - +
+    @creationEvent.ToJson()
+    
@code { - private string JsonString - { - get => creationEvent.ToJson(); - set - { - creationEvent = JsonSerializer.Deserialize(value); - JsonChanged(); - } - } - private string RoomPreset { get @@ -153,6 +233,8 @@ { creationEvent = Presets[value]; JsonChanged(); + OverwriteWrappedPropertiesFromEvent(); + creationEvent.PowerLevelContentOverride.Events = creationEvent.PowerLevelContentOverride.Events.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value); } } @@ -166,123 +248,87 @@ { await LocalStorageWrapper.LoadFromLocalStorage(LocalStorage); - creationEvent = Presets["Default room"] = new CreateRoomRequest + //creationEvent = Presets["Default room"] = + foreach (var x in Assembly.GetExecutingAssembly().GetTypes().Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().Contains(typeof(IRoomCreationTemplate))).ToList()) { - Name = "My new room", - RoomAliasName = "myroom", - InitialState = new() - { - new() - { - Type = "m.room.history_visibility", - Content = new - { - history_visibility = "world_readable" - } - }, - new() - { - Type = "m.room.guest_access", - Content = new - { - guest_access = "can_join" - } - }, - new() - { - Type = "m.room.join_rules", - Content = new - { - join_rule = "public" - } - }, - new() - { - Type = "m.room.server_acl", - Content = new - { - allow = new[] { "*" }, - deny = new[] - { - "midov.pl", - "qoto.org", - "matrix.kiwifarms.net", - "plan9.rocks", - "thisisjoes.site", - "konqi.work", - "austinhuang.lol", - "arcticfox.ems.host", - "*.thisisjoes.site", - "*.abuser.eu", - "*.austinhuang.lol" - }, - allow_ip_literals = false - } - }, - new() - { - Type = "m.room.avatar", - Content = new - { - url = "mxc://feline.support/UKNhEyrVsrAbYteVvZloZcFj" - } - } - }, - Visibility = "public", - PowerLevelContentOverride = new() - { - UsersDefault = 0, - EventsDefault = 100, - StateDefault = 50, - Invite = 0, - Redact = 50, - Kick = 50, - Ban = 50, - NotificationsPl = new() - { - Room = 50 - }, - Events = new() - { - { "im.vector.modular.widgets", 50 }, - { "io.element.voice_broadcast_info", 50 }, - { "m.reaction", 100 }, - { "m.room.avatar", 50 }, - { "m.room.canonical_alias", 50 }, - { "m.room.encryption", 100 }, - { "m.room.history_visibility", 100 }, - { "m.room.name", 50 }, - { "m.room.pinned_events", 50 }, - { "m.room.power_levels", 100 }, - { "m.room.redaction", 100 }, - { "m.room.server_acl", 100 }, - { "m.room.tombstone", 100 }, - { "m.room.topic", 50 }, - { "m.space.child", 50 }, - { "org.matrix.msc3401.call", 50 }, - { "org.matrix.msc3401.call.member", 50 } - }, - Users = new() - { - { "@alicia:rory.gay", 100 }, - { "@emma:rory.gay", 100 }, - { "@root:rory.gay", 100 }, - { "@rory:rory.gay", 100 } - }, - }, - CreationContent = new() - { - { "type", null } - } - }; + 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(JsonString); + Console.WriteLine(creationEvent.ToJson()); + } + + + //wrappers + private List ServerACLAllowRules { get; set; } = new(); + private List ServerACLDenyRules { get; set; } = new(); + + private void OverwriteWrappedPropertiesFromEvent() + { + Console.WriteLine("Overwriting wrapped properties from event"); + ServerACLAllowRules = creationEvent.ServerACLs.Allow; + ServerACLDenyRules = creationEvent.ServerACLs.Deny; + } + + private async Task OverwriteWrappedProperties() + { + Console.WriteLine("Overwriting wrapped properties"); + Console.WriteLine($"Allow: {ServerACLAllowRules.Count}: {string.Join(", ", ServerACLAllowRules)}"); + Console.WriteLine($"Deny: {ServerACLDenyRules.Count}: {string.Join(", ", ServerACLDenyRules)}"); + creationEvent.ServerACLs = new() + { + Allow = ServerACLAllowRules, + Deny = ServerACLDenyRules, + AllowIpLiterals = creationEvent.ServerACLs.AllowIpLiterals + }; + + StateHasChanged(); + } + + private async Task RoomIconFilePicked(InputFileChangeEventArgs obj) + { + var res = await RuntimeCache.CurrentHomeServer.UploadFile(obj.File.Name, obj.File.OpenReadStream(), obj.File.ContentType); + Console.WriteLine(res); + creationEvent.RoomIcon = 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)"); + //creationEvent.CreationContent.Add(); + var id = await RuntimeCache.CurrentHomeServer.CreateRoom(creationEvent); + // NavigationManager.NavigateTo($"/RoomManager/{id.RoomId.Replace('.','~')}"); + } + + private void InviteMember(string mxid) + { + if (!creationEvent.InitialState.Any(x => x.Type == "m.room.member" && x.StateKey == mxid) && RuntimeCache.CurrentHomeServer.UserId != mxid) + creationEvent.InitialState.Add(new() + { + Type = "m.room.member", + StateKey = mxid, + Content = new + { + membership = "invite", + reason = "Automatically invited at room creation time." + } + }); } @@ -295,4 +341,32 @@ _ => key }; -} \ No newline at end of file + 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 + }; + public class GuestAccessContent + { + [JsonPropertyName("guest_access")] + public string GuestAccess { get; set; } + + public bool IsGuestAccessEnabled + { + get => GuestAccess == "can_join"; + set => GuestAccess = value ? "can_join" : "forbidden"; + } + } + + } + + -- cgit 1.5.1