Dependency injection stuff
1 files changed, 55 insertions, 11 deletions
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<T1>() where T1 : IStateEventType {
return RawContent.Deserialize<T1>();
@@ -35,17 +60,36 @@ public class StateEvent {
[JsonIgnore]
public Type GetType {
get {
- var type = StateEvent.KnownStateEventTypes.FirstOrDefault(x =>
- x.GetCustomAttribute<MatrixEventAttribute>()?.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<string, JsonObject>);
+ }
+
+ var type = KnownStateEventTypes.FirstOrDefault(x =>
+ x.GetCustomAttributes<MatrixEventAttribute>()?.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);
}
}
|