about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-12 00:18:56 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-07-12 00:18:56 +0200
commit0e43e947ca8be0e529f2505a66143cb8b1fcbb8e (patch)
tree6fc7af3f8b7387ff5e10401f521946316f1fe5f5 /MatrixRoomUtils.Core
parentLocal changes (diff)
downloadMatrixUtils-0e43e947ca8be0e529f2505a66143cb8b1fcbb8e.tar.xz
Changes
Diffstat (limited to 'MatrixRoomUtils.Core')
-rw-r--r--MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs22
1 files changed, 19 insertions, 3 deletions
diff --git a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
index 334c05c..be78a97 100644
--- a/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
+++ b/MatrixRoomUtils.Core/Responses/CreateRoomRequest.cs
@@ -1,6 +1,8 @@
+using System.Reflection;
 using System.Text.Json.Nodes;
 using System.Text.Json.Serialization;
 using System.Text.RegularExpressions;
+using MatrixRoomUtils.Core.Extensions;
 using MatrixRoomUtils.Core.Interfaces;
 using MatrixRoomUtils.Core.StateEventTypes;
 using MatrixRoomUtils.Core.StateEventTypes.Spec;
@@ -21,7 +23,7 @@ public class CreateRoomRequest {
     //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!;
 
@@ -39,7 +41,21 @@ public class CreateRoomRequest {
     /// </summary>
 
     public StateEvent this[string event_type, string event_key = ""] {
-        get => InitialState.First(x => x.Type == event_type && x.StateKey == event_key);
+        get {
+            var stateEvent = InitialState.FirstOrDefault(x => x.Type == event_type && x.StateKey == event_key);
+            if (stateEvent == null) {
+                InitialState.Add(stateEvent = new StateEvent {
+                    Type = event_type,
+                    StateKey = event_key,
+                    TypedContent = Activator.CreateInstance(
+                        StateEvent.KnownStateEventTypes.FirstOrDefault(x =>
+                            x.GetCustomAttributes<MatrixEventAttribute>()?
+                                .Any(y => y.EventName == event_type) ?? false) ?? typeof(object)
+                        )
+                });
+            }
+            return stateEvent;
+        }
         set {
             var stateEvent = InitialState.FirstOrDefault(x => x.Type == event_type && x.StateKey == event_key);
             if (stateEvent == null)
@@ -57,4 +73,4 @@ public class CreateRoomRequest {
 
         return errors;
     }
-}
\ No newline at end of file
+}