about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Responses
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-27 00:39:49 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-05-27 00:39:49 +0200
commita67276252c8bfcd6b6c5344e70debc6d67d917a9 (patch)
treeaf2806e9729f1013a5db6fbda80a7ad1cd7126fa /MatrixRoomUtils.Core/Responses
parentidk (diff)
downloadMatrixUtils-a67276252c8bfcd6b6c5344e70debc6d67d917a9.tar.xz
Been a while since I last committed
Diffstat (limited to 'MatrixRoomUtils.Core/Responses')
-rw-r--r--MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs217
-rw-r--r--MatrixRoomUtils.Core/Responses/StateEventResponse.cs34
2 files changed, 251 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
new file mode 100644
index 0000000..6949b1a
--- /dev/null
+++ b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
@@ -0,0 +1,217 @@
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using System.Text.Json.Serialization;
+using System.Text.RegularExpressions;
+
+namespace MatrixRoomUtils.Core.Responses;
+
+public class CreateRoomRequest
+{
+    [JsonPropertyName("name")] public string Name { get; set; } = null!;
+
+    [JsonPropertyName("room_alias_name")] public string RoomAliasName { get; set; } = null!;
+
+    //we dont want to use this, we want more control
+    // [JsonPropertyName("preset")]
+    // public string Preset { get; set; } = null!;
+    [JsonPropertyName("initial_state")] public List<StateEvent> InitialState { get; set; } = null!;
+    [JsonPropertyName("visibility")] public string Visibility { get; set; } = null!;
+
+    [JsonPropertyName("power_level_content_override")]
+    public PowerLevelEvent PowerLevelContentOverride { get; set; } = null!;
+
+    [JsonPropertyName("creation_content")] public JsonObject CreationContent { get; set; } = new();
+
+    /// <summary>
+    /// For use only when you can't use the CreationContent property
+    /// </summary>
+
+
+    //extra properties
+    [JsonIgnore]
+    public string HistoryVisibility
+    {
+        get
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.history_visibility",
+                    Content = new JsonObject()
+                    {
+                        ["history_visibility"] = "shared"
+                    }
+                });
+                return "shared";
+            }
+
+            return stateEvent.ContentAsJsonNode["history_visibility"].GetValue<string>();
+        }
+        set
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.history_visibility");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.history_visibility",
+                    Content = new JsonObject()
+                    {
+                        ["history_visibility"] = value
+                    }
+                });
+            }
+            else
+            {
+                var v = stateEvent.ContentAsJsonNode;
+                v["history_visibility"] = value;
+                stateEvent.ContentAsJsonNode = v;
+            }
+        }
+    }
+
+    [JsonIgnore]
+    public string RoomIcon
+    {
+        get
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.avatar",
+                    Content = new JsonObject()
+                    {
+                        ["url"] = ""
+                    }
+                });
+                return "";
+            }
+
+            return stateEvent.ContentAsJsonNode["url"].GetValue<string>();
+        }
+        set
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.avatar");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.avatar",
+                    Content = new JsonObject()
+                    {
+                        ["url"] = value
+                    }
+                });
+            }
+            else
+            {
+                var v = stateEvent.ContentAsJsonNode;
+                v["url"] = value;
+                stateEvent.ContentAsJsonNode = v;
+            }
+        }
+    }
+
+    [JsonIgnore]
+    public string GuestAccess
+    {
+        get
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.guest_access",
+                    Content = new JsonObject()
+                    {
+                        ["guest_access"] = "can_join"
+                    }
+                });
+                return "can_join";
+            }
+
+            return stateEvent.ContentAsJsonNode["guest_access"].GetValue<string>();
+        }
+        set
+        {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == "m.room.guest_access");
+            if (stateEvent == null)
+            {
+                InitialState.Add(new StateEvent()
+                {
+                    Type = "m.room.guest_access",
+                    Content = new JsonObject()
+                    {
+                        ["guest_access"] = value
+                    }
+                });
+            }
+            else
+            {
+                var v = stateEvent.ContentAsJsonNode;
+                v["guest_access"] = value;
+                stateEvent.ContentAsJsonNode = v;
+            }
+        }
+    }
+
+
+        [JsonIgnore] public CreationContentBaseType _creationContentBaseType;
+
+    public CreateRoomRequest() => _creationContentBaseType = new(this);
+
+
+    public Dictionary<string, string> Validate()
+    {
+        Dictionary<string, string> errors = new();
+        if (!Regex.IsMatch(RoomAliasName, @"[a-zA-Z0-9_\-]+$"))
+            errors.Add("room_alias_name", "Room alias name must only contain letters, numbers, underscores, and hyphens.");
+
+        return errors;
+    }
+}
+
+public class CreationContentBaseType
+{
+    private readonly CreateRoomRequest createRoomRequest;
+
+    public CreationContentBaseType(CreateRoomRequest createRoomRequest)
+    {
+        this.createRoomRequest = createRoomRequest;
+    }
+
+    [JsonPropertyName("type")]
+    public string Type
+    {
+        get => (string)createRoomRequest.CreationContent["type"];
+        set
+        {
+            if (value is "null" or "") createRoomRequest.CreationContent.Remove("type");
+            else createRoomRequest.CreationContent["type"] = value;
+        }
+    }
+}
+
+public class PowerLevelEvent
+{
+    [JsonPropertyName("ban")] public int Ban { get; set; } // = 50;
+    [JsonPropertyName("events_default")] public int EventsDefault { get; set; } // = 0;
+    [JsonPropertyName("events")] public Dictionary<string, int> Events { get; set; } // = null!;
+    [JsonPropertyName("invite")] public int Invite { get; set; } // = 50;
+    [JsonPropertyName("kick")] public int Kick { get; set; } // = 50;
+    [JsonPropertyName("notifications")] public NotificationsPL NotificationsPl { get; set; } // = null!;
+    [JsonPropertyName("redact")] public int Redact { get; set; } // = 50;
+    [JsonPropertyName("state_default")] public int StateDefault { get; set; } // = 50;
+    [JsonPropertyName("users")] public Dictionary<string, int> Users { get; set; } // = null!;
+    [JsonPropertyName("users_default")] public int UsersDefault { get; set; } // = 0;
+}
+
+public class NotificationsPL
+{
+    [JsonPropertyName("room")] public int Room { get; set; } = 50;
+}
\ No newline at end of file
diff --git a/MatrixRoomUtils.Core/Responses/StateEventResponse.cs b/MatrixRoomUtils.Core/Responses/StateEventResponse.cs
new file mode 100644
index 0000000..d86f546
--- /dev/null
+++ b/MatrixRoomUtils.Core/Responses/StateEventResponse.cs
@@ -0,0 +1,34 @@
+using System.Text.Json.Serialization;
+
+namespace MatrixRoomUtils.Core;
+
+public class StateEventResponse
+{
+    [JsonPropertyName("Content")]
+    public dynamic Content { get; set; }
+    [JsonPropertyName("origin_server_ts")]
+    public long OriginServerTs { get; set; }
+    [JsonPropertyName("RoomId")]
+    public string RoomId { get; set; }
+    [JsonPropertyName("Sender")]
+    public string Sender { get; set; }
+    [JsonPropertyName("StateKey")]
+    public string StateKey { get; set; }
+    [JsonPropertyName("Type")]
+    public string Type { get; set; }
+    [JsonPropertyName("Unsigned")]
+    public dynamic Unsigned { get; set; }
+    [JsonPropertyName("EventId")]
+    public string EventId { get; set; }
+    [JsonPropertyName("UserId")]
+    public string UserId { get; set; }
+    [JsonPropertyName("ReplacesState")]
+    public string ReplacesState { get; set; }
+    [JsonPropertyName("PrevContent")]
+    public dynamic PrevContent { get; set; }
+}
+
+public class StateEventResponse<T> : StateEventResponse where T : class
+{
+    public T content { get; set; }
+}
\ No newline at end of file