From 3ed00f732a284b5a3e96e52d4e3a71869135869b Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 26 Jun 2023 02:43:54 +0200 Subject: Dependency injection stuff --- MatrixRoomUtils.Core/StateEvent.cs | 66 +++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 11 deletions(-) (limited to 'MatrixRoomUtils.Core/StateEvent.cs') diff --git a/MatrixRoomUtils.Core/StateEvent.cs b/MatrixRoomUtils.Core/StateEvent.cs index cb8f0b4..f2c8701 100644 --- a/MatrixRoomUtils.Core/StateEvent.cs +++ b/MatrixRoomUtils.Core/StateEvent.cs @@ -1,9 +1,12 @@ +using System.Diagnostics; using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; using MatrixRoomUtils.Core.Extensions; using MatrixRoomUtils.Core.Interfaces; +using MatrixRoomUtils.Core.Responses; +using MatrixRoomUtils.Core.StateEventTypes; namespace MatrixRoomUtils.Core; @@ -19,14 +22,36 @@ public class StateEvent { [JsonPropertyName("state_key")] public string StateKey { get; set; } = ""; + private string _type; + [JsonPropertyName("type")] - public string Type { get; set; } + public string Type { + get => _type; + set { + _type = value; + if (RawContent != 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; set; } + public JsonObject? RawContent { + get => _rawContent; + set { + _rawContent = value; + if (Type != null && this is StateEventResponse stateEventResponse) { + if (File.Exists($"unknown_state_events/{Type}/{stateEventResponse.EventId}.json")) return; + var x = GetType.Name; + } + } + } public T1 GetContent() where T1 : IStateEventType { return RawContent.Deserialize(); @@ -35,17 +60,36 @@ public class StateEvent { [JsonIgnore] public Type GetType { get { - var type = StateEvent.KnownStateEventTypes.FirstOrDefault(x => - x.GetCustomAttribute()?.EventName == Type); - if (type == null) { - Console.WriteLine($"Warning: unknown event type '{Type}'!"); - Console.WriteLine(RawContent.ToJson()); - return typeof(object); + if (Type == "m.receipt") { + return typeof(Dictionary); + } + + var type = KnownStateEventTypes.FirstOrDefault(x => + x.GetCustomAttributes()?.Any(y => y.EventName == Type) ?? false); + + //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.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"); + } } - RawContent.FindExtraJsonObjectFields(type); - - return type; + return type ?? typeof(object); } } -- cgit 1.5.1