about summary refs log tree commit diff
path: root/LibMatrix/StateEvent.cs
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-01-11 07:31:09 +0100
commit8dadf547033d71480fd7756809992c0f32549f59 (patch)
treea23631cc840047e3ad9e32dda9c043511af1a583 /LibMatrix/StateEvent.cs
parentTry-create factory for RemoteHomeserver, more reliable RemoteHomeserver creation (diff)
downloadLibMatrix-8dadf547033d71480fd7756809992c0f32549f59.tar.xz
Cleanup, more message formatters, messagebuilder start
Diffstat (limited to 'LibMatrix/StateEvent.cs')
-rw-r--r--LibMatrix/StateEvent.cs38
1 files changed, 32 insertions, 6 deletions
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
index 4a0adbd..1a8df11 100644
--- a/LibMatrix/StateEvent.cs
+++ b/LibMatrix/StateEvent.cs
@@ -6,7 +6,9 @@ using System.Text.Json;
 using System.Text.Json.Nodes;
 using System.Text.Json.Serialization;
 using ArcaneLibs;
+using ArcaneLibs.Attributes;
 using ArcaneLibs.Extensions;
+using Castle.DynamicProxy;
 using LibMatrix.EventTypes;
 using LibMatrix.Extensions;
 
@@ -14,7 +16,7 @@ namespace LibMatrix;
 
 public class StateEvent {
     public static FrozenSet<Type> KnownStateEventTypes { get; } = new ClassCollector<EventContent>().ResolveFromAllAccessibleAssemblies().ToFrozenSet();
-
+    
     public static FrozenDictionary<string, Type> KnownStateEventTypesByName { get; } = KnownStateEventTypes.Aggregate(
         new Dictionary<string, Type>(),
         (dict, type) => {
@@ -22,14 +24,24 @@ public class StateEvent {
             foreach (var attr in attrs) {
                 dict[attr.EventName] = type;
             }
+
             return dict;
         }).ToFrozenDictionary();
 
     public static Type GetStateEventType(string type) => KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent);
-    
+
     [JsonIgnore]
     public Type MappedType => GetStateEventType(Type);
 
+    [JsonIgnore]
+    public bool IsLegacyType => MappedType.GetCustomAttributes<MatrixEventAttribute>().FirstOrDefault(x => x.EventName == Type)?.Legacy ?? false;
+
+    [JsonIgnore]
+    public string FriendlyTypeName => MappedType.GetFriendlyNameOrNull() ?? Type;
+
+    [JsonIgnore]
+    public string FriendlyTypeNamePlural => MappedType.GetFriendlyNamePluralOrNull() ?? Type;
+
     private static readonly JsonSerializerOptions TypedContentSerializerOptions = new() {
         Converters = {
             new JsonFloatStringConverter(),
@@ -38,15 +50,30 @@ public class StateEvent {
         }
     };
 
+    private class EventContentInterceptor : IInterceptor {
+        public void Intercept(IInvocation invocation) {
+            Console.WriteLine($"Intercepting {invocation.Method.Name}");
+            // if (invocation.Method.Name == "ToString") {
+            //     invocation.ReturnValue = "EventContent";
+            //     return;
+            // }
+
+            invocation.Proceed();
+        }
+    }
+    
     [JsonIgnore]
     [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
     public EventContent? TypedContent {
         get {
             // if (Type == "m.receipt") {
-                // return null;
+            // return null;
             // }
             try {
-                return (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                var c= (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                // c = (EventContent)new ProxyGenerator().CreateClassProxyWithTarget(GetStateEventType(Type), c, new EventContentInterceptor());
+                // Console.WriteLine(c.GetType().Name + ": " + string.Join(", ", c.GetType().GetRuntimeProperties().Select(x=>x.Name)));
+                return c;
             }
             catch (JsonException e) {
                 Console.WriteLine(e);
@@ -127,7 +154,6 @@ public class StateEvent {
     public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null";
 }
 
-
 public class StateEventResponse : StateEvent {
     [JsonPropertyName("origin_server_ts")]
     public ulong? OriginServerTs { get; set; }
@@ -213,4 +239,4 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR
 }
 */
 
-#endregion
+#endregion
\ No newline at end of file