about summary refs log tree commit diff
path: root/LibMatrix/StateEvent.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-09-15 09:50:45 +0200
commit6bd02248ccfbcb46960a6f39eaad23888d190eb5 (patch)
tree110578f31b6f9f70a7a1edab32fb3a34d6ad4f1a /LibMatrix/StateEvent.cs
parentMedia moderator PoC works, abstract command handling to library (diff)
downloadLibMatrix-6bd02248ccfbcb46960a6f39eaad23888d190eb5.tar.xz
Some refactoring
Diffstat (limited to 'LibMatrix/StateEvent.cs')
-rw-r--r--LibMatrix/StateEvent.cs42
1 files changed, 22 insertions, 20 deletions
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
index 175d706..9ca9141 100644
--- a/LibMatrix/StateEvent.cs
+++ b/LibMatrix/StateEvent.cs
@@ -6,28 +6,41 @@ using ArcaneLibs;
 using ArcaneLibs.Extensions;
 using LibMatrix.Helpers;
 using LibMatrix.Interfaces;
+using LibMatrix.StateEventTypes;
 
 namespace LibMatrix;
 
 public class StateEvent {
-    public static List<Type> KnownStateEventTypes =
-        new ClassCollector<IStateEventType>().ResolveFromAllAccessibleAssemblies();
+    public static readonly List<Type> KnownStateEventTypes =
+        new ClassCollector<EventContent>().ResolveFromAllAccessibleAssemblies();
+
+    public static readonly Dictionary<string, Type> KnownStateEventTypesByName = KnownStateEventTypes.Aggregate(
+        new Dictionary<string, Type>(),
+        (dict, type) => {
+            var attrs = type.GetCustomAttributes<MatrixEventAttribute>();
+            foreach (var attr in attrs) {
+                dict[attr.EventName] = type;
+            }
+
+            return dict;
+        });
 
     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);
+        // var eventType = KnownStateEventTypes.FirstOrDefault(x =>
+        // x.GetCustomAttributes<MatrixEventAttribute>()?.Any(y => y.EventName == type) ?? false);
+        var eventType = KnownStateEventTypesByName.GetValueOrDefault(type);
 
-        return eventType ?? typeof(object);
+        return eventType ?? typeof(UnknownEventContent);
     }
 
-    public object TypedContent {
+    public EventContent TypedContent {
         get {
             try {
-                return RawContent.Deserialize(GetType)!;
+                return (EventContent) RawContent.Deserialize(GetType)!;
             }
             catch (JsonException e) {
                 Console.WriteLine(e);
@@ -42,19 +55,8 @@ public class StateEvent {
     [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;
-            // }
-        }
-    }
+    public string Type { get; set; }
 
     [JsonPropertyName("replaces_state")]
     public string? ReplacesState { get; set; }
@@ -79,7 +81,7 @@ public class StateEvent {
             var type = GetStateEventType(Type);
 
             //special handling for some types
-            // if (type == typeof(RoomEmotesEventData)) {
+            // if (type == typeof(RoomEmotesEventContent)) {
             //     RawContent["emote"] = RawContent["emote"]?.AsObject() ?? new JsonObject();
             // }
             //