about summary refs log tree commit diff
path: root/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs')
-rw-r--r--LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs31
1 files changed, 31 insertions, 0 deletions
diff --git a/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs b/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs
new file mode 100644

index 0000000..4fd5c29 --- /dev/null +++ b/LibMatrix.EventTypes/Common/Msc2545EmoteRoomsAccountDataEventContent.cs
@@ -0,0 +1,31 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Spec; + +[MatrixEvent(EventName = EventId)] +public class Msc2545EmoteRoomsAccountDataEventContent : EventContent { + public const string EventId = "im.ponies.emote_rooms"; + + [JsonPropertyName("rooms")] + public Dictionary<string, Dictionary<string, EnabledEmotePackEntry>> Rooms { get; set; } = new(); + + // Dummy type to provide easy access to the by-spec empty content + public class EnabledEmotePackEntry { + [JsonExtensionData] + public Dictionary<string, object>? AdditionalData { get; set; } = []; + + public T? GetAdditionalData<T>(string key) where T : class { + if (AdditionalData == null || !AdditionalData.TryGetValue(key, out var value)) + return null; + + if (value is T tValue) + return tValue; + if (value is JsonElement jsonElement) + return jsonElement.Deserialize<T>(); + + throw new InvalidCastException($"Value for key '{key}' ({value.GetType()}) cannot be cast to type '{typeof(T)}'. Cannot continue."); + } + } +} \ No newline at end of file