about summary refs log tree commit diff
path: root/LibMatrix/StateEvent.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-08-14 04:09:13 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-08-14 04:09:13 +0200
commit0d0511e35d9965fc0ea5190ae3347c3d77c3334c (patch)
treeb4e0c336cbccc37bd14952b447868a577ea15540 /LibMatrix/StateEvent.cs
downloadLibMatrix-0d0511e35d9965fc0ea5190ae3347c3d77c3334c.tar.xz
Split LibMatrix into separate repo
Diffstat (limited to 'LibMatrix/StateEvent.cs')
-rw-r--r--LibMatrix/StateEvent.cs117
1 files changed, 117 insertions, 0 deletions
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
new file mode 100644
index 0000000..5efeaf5
--- /dev/null
+++ b/LibMatrix/StateEvent.cs
@@ -0,0 +1,117 @@
+using System.Reflection;
+using System.Text.Json;
+using System.Text.Json.Nodes;
+using System.Text.Json.Serialization;
+using LibMatrix.Extensions;
+using LibMatrix.Interfaces;
+
+namespace LibMatrix;
+
+public class StateEvent {
+    public static List<Type> KnownStateEventTypes =
+        new ClassCollector<IStateEventType>().ResolveFromAllAccessibleAssemblies();
+
+    public static Type GetStateEventType(string type) {
+        if (type == "m.receipt") {
+            return typeof(Dictionary<string, JsonObject>);
+        }
+
+        var eventType = KnownStateEventTypes.FirstOrDefault(x =>
+            x.GetCustomAttributes<MatrixEventAttribute>()?.Any(y => y.EventName == type) ?? false);
+
+        return eventType ?? typeof(object);
+    }
+
+    public object TypedContent {
+        get {
+            try {
+                return RawContent.Deserialize(GetType)!;
+            }
+            catch (JsonException e) {
+                Console.WriteLine(e);
+                Console.WriteLine("Content:\n" + ObjectExtensions.ToJson(RawContent));
+            }
+
+            return null;
+        }
+        set => RawContent = JsonSerializer.Deserialize<JsonObject>(JsonSerializer.Serialize(value));
+    }
+
+    [JsonPropertyName("state_key")]
+    public string StateKey { get; set; } = "";
+
+    private string _type;
+
+    [JsonPropertyName("type")]
+    public string Type {
+        get => _type;
+        set {
+            _type = value;
+            // if (RawContent is not null && this is StateEventResponse stateEventResponse) {
+            //     if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return;
+            //     var x = GetType.Name;
+            // }
+        }
+    }
+
+    [JsonPropertyName("replaces_state")]
+    public string? ReplacesState { get; set; }
+
+    private JsonObject? _rawContent;
+
+    [JsonPropertyName("content")]
+    public JsonObject? RawContent {
+        get => _rawContent;
+        set {
+            _rawContent = value;
+            // if (Type is not null && this is StateEventResponse stateEventResponse) {
+            //     if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return;
+            //     var x = GetType.Name;
+            // }
+        }
+    }
+
+    [JsonIgnore]
+    public Type GetType {
+        get {
+            var type = GetStateEventType(Type);
+
+            //special handling for some types
+            // if (type == typeof(RoomEmotesEventData)) {
+            //     RawContent["emote"] = RawContent["emote"]?.AsObject() ?? new JsonObject();
+            // }
+            //
+            // if (this is StateEventResponse stateEventResponse) {
+            //     if (type == null || type == typeof(object)) {
+            //         Console.WriteLine($"Warning: unknown event type '{Type}'!");
+            //         Console.WriteLine(RawContent.ToJson());
+            //         Directory.CreateDirectory($"unknown_state_events/{Type}");
+            //         File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json",
+            //             RawContent.ToJson());
+            //         Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json");
+            //     }
+            //     else if (RawContent is not null && RawContent.FindExtraJsonObjectFields(type)) {
+            //         Directory.CreateDirectory($"unknown_state_events/{Type}");
+            //         File.WriteAllText($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json",
+            //             RawContent.ToJson());
+            //         Console.WriteLine($"Saved to unknown_state_events/{Type}/{stateEventResponse.EventId}.json");
+            //     }
+            // }
+
+            return type;
+        }
+    }
+
+    //debug
+    public string dtype {
+        get {
+            var res = GetType().Name switch {
+                "StateEvent`1" => $"StateEvent",
+                _ => GetType().Name
+            };
+            return res;
+        }
+    }
+
+    public string cdtype => TypedContent.GetType().Name;
+}