MatrixRoomUtils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

StateEvent.cs (1491B)


      1 using System.Text.Json;
      2 using System.Text.Json.Nodes;
      3 using System.Text.Json.Serialization;
      4 
      5 namespace MatrixRoomUtils.Core;
      6 
      7 public class StateEvent
      8 {
      9     [JsonPropertyName("content")] public dynamic Content { get; set; } = new { };
     10 
     11     [JsonPropertyName("state_key")] public string StateKey { get; set; } = "";
     12     [JsonPropertyName("type")] public string Type { get; set; }
     13     [JsonPropertyName("replaces_state")] public string? ReplacesState { get; set; }
     14 
     15     //extra properties
     16     [JsonIgnore]
     17     public JsonNode ContentAsJsonNode
     18     {
     19         get => JsonSerializer.SerializeToNode(Content);
     20         set => Content = value;
     21     }
     22 
     23     public StateEvent<T> As<T>() where T : class
     24     {
     25         return (StateEvent<T>)this;
     26     }
     27 
     28     public string dtype
     29     {
     30         get
     31         {
     32             string res = GetType().Name switch
     33             {
     34                 "StateEvent`1" => $"StateEvent<{Content.GetType().Name}>",
     35                 _ => GetType().Name
     36             };
     37             return res;
     38         }
     39     }
     40 }
     41 
     42 public class StateEvent<T> : StateEvent where T : class
     43 {
     44     public StateEvent()
     45     {
     46         //import base content if not an empty object
     47         if (base.Content.GetType() == typeof(T))
     48         {
     49             Console.WriteLine($"StateEvent<{typeof(T)}> created with base content of type {base.Content.GetType()}. Importing base content.");
     50             Content = base.Content;
     51         }
     52     }
     53     [JsonPropertyName("content")]
     54     public new T Content { get; set; }
     55 }