From 59016736ceb4aab3975fe429e226da38c239abf9 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 31 May 2024 13:41:40 +0200 Subject: Event serialisation fix --- ExampleBots/ModerationBot | 2 +- .../Events/RoomMembershipEventContent.cs | 6 +- .../Events/RoomMessageEventContent.cs | 57 +++++++++ LibMatrix.EventTypes/LibMatrix.EventTypes.csproj | 5 + LibMatrix.EventTypes/MatrixEventCollection.cs | 142 ++++++++++----------- LibMatrix.EventTypes/MatrixEventContent.cs | 48 ++++++- LibMatrix.EventTypes/temp/Program.cs | 30 +++++ LibMatrix.EventTypes/temp/Temp.cs | 17 --- LibMatrix/Utilities/CommonSyncFilters.cs | 4 +- 9 files changed, 211 insertions(+), 100 deletions(-) create mode 100644 LibMatrix.EventTypes/Events/RoomMessageEventContent.cs create mode 100644 LibMatrix.EventTypes/temp/Program.cs delete mode 100644 LibMatrix.EventTypes/temp/Temp.cs diff --git a/ExampleBots/ModerationBot b/ExampleBots/ModerationBot index b5db509..c137f94 160000 --- a/ExampleBots/ModerationBot +++ b/ExampleBots/ModerationBot @@ -1 +1 @@ -Subproject commit b5db50961eeedf24f0c969799e6f24830c973b0c +Subproject commit c137f94aeb122c636629fb9361dd73626594f690 diff --git a/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs b/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs index dac7094..fe50a2e 100644 --- a/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs +++ b/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs @@ -2,9 +2,11 @@ using System.Text.Json.Serialization; namespace LibMatrix.EventTypes.Events; +[MatrixEvent("m.room.member")] +[JsonConverter(typeof(MatrixEventContentConverter))] public class RoomMembershipEventContent : MatrixEventContent { public string Membership { - get => _json["membership"]!.GetValue(); - set => Console.WriteLine(value); + get => InternalJson["membership"]!.GetValue(); + set => InternalJson["membership"] = value; } } \ No newline at end of file diff --git a/LibMatrix.EventTypes/Events/RoomMessageEventContent.cs b/LibMatrix.EventTypes/Events/RoomMessageEventContent.cs new file mode 100644 index 0000000..55c2b6c --- /dev/null +++ b/LibMatrix.EventTypes/Events/RoomMessageEventContent.cs @@ -0,0 +1,57 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes; + +namespace LibMatrix.LegacyEvents.EventTypes.Spec; + +[MatrixEvent(EventId)] +public class RoomMessageEventContent : MatrixEventContent { + public const string EventId = "m.room.message"; + + public RoomMessageEventContent(string messageType = "m.notice", string? body = null) { + MessageType = messageType; + Body = body ?? ""; + } + + [JsonPropertyName("body")] + public string Body { get; set; } + + [JsonPropertyName("msgtype")] + public string MessageType { get; set; } = "m.notice"; + + [JsonPropertyName("formatted_body")] + public string? FormattedBody { get; set; } + + [JsonPropertyName("format")] + public string? Format { get; set; } + + /// + /// Media URI for this message, if any + /// + [JsonPropertyName("url")] + public string? Url { get; set; } + + public string? FileName { get; set; } + + [JsonPropertyName("info")] + public FileInfoStruct? FileInfo { get; set; } + + [JsonIgnore] + public string BodyWithoutReplyFallback => Body.Split('\n').SkipWhile(x => x.StartsWith(">")).SkipWhile(x=>x.Trim().Length == 0).Aggregate((x, y) => $"{x}\n{y}"); + + public class FileInfoStruct { + [JsonPropertyName("mimetype")] + public string? MimeType { get; set; } + + [JsonPropertyName("size")] + public long Size { get; set; } + + [JsonPropertyName("thumbnail_url")] + public string? ThumbnailUrl { get; set; } + + [JsonPropertyName("w")] + public int? Width { get; set; } + + [JsonPropertyName("h")] + public int? Height { get; set; } + } +} \ No newline at end of file diff --git a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj index 3a63532..bd33993 100644 --- a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj +++ b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj @@ -4,6 +4,11 @@ net8.0 enable enable + Exe + + + + diff --git a/LibMatrix.EventTypes/MatrixEventCollection.cs b/LibMatrix.EventTypes/MatrixEventCollection.cs index 35afd2b..78886d9 100644 --- a/LibMatrix.EventTypes/MatrixEventCollection.cs +++ b/LibMatrix.EventTypes/MatrixEventCollection.cs @@ -1,71 +1,71 @@ -using System.Collections; - -namespace LibMatrix.EventTypes; - -public interface IMatrixEventCollection : IEnumerable> where T : MatrixEventContent { - -} -public class MatrixEventCollection : IMatrixEventCollection, IList { - private IList> _listImplementation; - public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); - - public void Add(MatrixEvent item) => _listImplementation.Add(item); - - public void Clear() => _listImplementation.Clear(); - - public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); - - public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); - - public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); - - public int Count => _listImplementation.Count; - - public bool IsReadOnly => _listImplementation.IsReadOnly; - - public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); - - public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); - - public void RemoveAt(int index) => _listImplementation.RemoveAt(index); - - public MatrixEvent this[int index] { - get => _listImplementation[index]; - set => _listImplementation[index] = value; - } -} -public class MatrixEventCollection : IMatrixEventCollection, IList> where T : MatrixEventContent { - //TODO: implement - - private IList> _listImplementation = new List>(); - public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); - - public void Add(MatrixEvent item) => _listImplementation.Add(item); - - public void Clear() => _listImplementation.Clear(); - - public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); - - public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); - - public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); - - public int Count => _listImplementation.Count; - - public bool IsReadOnly => _listImplementation.IsReadOnly; - - public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); - - public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); - - public void RemoveAt(int index) => _listImplementation.RemoveAt(index); - - public MatrixEvent this[int index] { - get => _listImplementation[index]; - set => _listImplementation[index] = value; - } -} \ No newline at end of file +// using System.Collections; +// +// namespace LibMatrix.EventTypes; +// +// public interface IMatrixEventCollection : IEnumerable> where T : MatrixEventContent { +// +// } +// public class MatrixEventCollection : IMatrixEventCollection, IList { +// private IList> _listImplementation; +// public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); +// +// IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); +// +// public void Add(MatrixEvent item) => _listImplementation.Add(item); +// +// public void Clear() => _listImplementation.Clear(); +// +// public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); +// +// public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); +// +// public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); +// +// public int Count => _listImplementation.Count; +// +// public bool IsReadOnly => _listImplementation.IsReadOnly; +// +// public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); +// +// public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); +// +// public void RemoveAt(int index) => _listImplementation.RemoveAt(index); +// +// public MatrixEvent this[int index] { +// get => _listImplementation[index]; +// set => _listImplementation[index] = value; +// } +// } +// public class MatrixEventCollection : IMatrixEventCollection, IList> where T : MatrixEventContent { +// //TODO: implement +// +// private IList> _listImplementation = new List>(); +// public IEnumerator> GetEnumerator() => _listImplementation.GetEnumerator(); +// +// IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator(); +// +// public void Add(MatrixEvent item) => _listImplementation.Add(item); +// +// public void Clear() => _listImplementation.Clear(); +// +// public bool Contains(MatrixEvent item) => _listImplementation.Contains(item); +// +// public void CopyTo(MatrixEvent[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex); +// +// public bool Remove(MatrixEvent item) => _listImplementation.Remove(item); +// +// public int Count => _listImplementation.Count; +// +// public bool IsReadOnly => _listImplementation.IsReadOnly; +// +// public int IndexOf(MatrixEvent item) => _listImplementation.IndexOf(item); +// +// public void Insert(int index, MatrixEvent item) => _listImplementation.Insert(index, item); +// +// public void RemoveAt(int index) => _listImplementation.RemoveAt(index); +// +// public MatrixEvent this[int index] { +// get => _listImplementation[index]; +// set => _listImplementation[index] = value; +// } +// }a \ No newline at end of file diff --git a/LibMatrix.EventTypes/MatrixEventContent.cs b/LibMatrix.EventTypes/MatrixEventContent.cs index c30ebb0..81b8c52 100644 --- a/LibMatrix.EventTypes/MatrixEventContent.cs +++ b/LibMatrix.EventTypes/MatrixEventContent.cs @@ -1,26 +1,60 @@ +using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Serialization; +using ArcaneLibs.Extensions; namespace LibMatrix.EventTypes; +// : MatrixEventContent where T : MatrixEventContent, new() { /// /// Extensible Event Content, aims to provide an API similar to JsonNode/JsonObject /// /// /// +[JsonConverter(typeof(MatrixEventContentConverter))] public class MatrixEventContent { - // : MatrixEventContent where T : MatrixEventContent, new() { - internal JsonNode _json = new JsonObject(); - public static implicit operator MatrixEventContent(JsonNode json) => new(json); + [JsonExtensionData, JsonInclude] + public JsonObject InternalJson { get; set; } = new(); + + + + public MatrixEventContent() { } - [JsonConstructor] public MatrixEventContent(JsonNode json) { - _json = json; + InternalJson = json.AsObject(); } - public MatrixEventContent() { } + public static implicit operator MatrixEventContent(JsonNode json) => new(json); + + // public static implicit operator JsonNode(MatrixEventContent content) => content.InternalJson; + + [JsonIgnore] + public IEnumerable EventTypes => this.GetType().GetCustomAttributes().Select(x => x.EventType); + + [JsonIgnore] + public string EventType => EventTypes.First(); + + public JsonNode? this[string key] => InternalJson[key]; + + public string ToJson() => InternalJson.ToJson(); + + + public class MatrixEventContentConverter : JsonConverter where T : MatrixEventContent, new() { + public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { + // read entire object into a JsonObject + var json = JsonNode.Parse(ref reader); + return new T { InternalJson = json.AsObject() }; + } + + public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) { + value.InternalJson.WriteTo(writer); + } + } +} - public JsonNode? this[string key] => _json[key]; +public class MatrixEventAttribute(string eventType, bool deprecated = false) : Attribute { + public string EventType { get; } = eventType; + public bool Deprecated { get; } = deprecated; } \ No newline at end of file diff --git a/LibMatrix.EventTypes/temp/Program.cs b/LibMatrix.EventTypes/temp/Program.cs new file mode 100644 index 0000000..22a65d4 --- /dev/null +++ b/LibMatrix.EventTypes/temp/Program.cs @@ -0,0 +1,30 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Events; + +namespace LibMatrix.EventTypes.temp; + +public class Program { + // public MatrixEventCollection Members = [ + // new MatrixEvent() { + // Content = new() { + // Membership = "join" + // } + // } + // ]; + + public static void Main(string[] args) { + var evt = new RoomMembershipEventContent() { + Membership = "join" + }; + Console.WriteLine(evt.ToJson()); + + var eventJson = File.ReadAllText("test-event.json"); + var evt2 = JsonSerializer.Deserialize>(eventJson); + evt2.Content.Membership = "meow"; + Console.WriteLine(evt2.Content.ToJson()); + Console.WriteLine(ObjectExtensions.ToJson(evt2)); + + } +} \ No newline at end of file diff --git a/LibMatrix.EventTypes/temp/Temp.cs b/LibMatrix.EventTypes/temp/Temp.cs deleted file mode 100644 index 3fbb401..0000000 --- a/LibMatrix.EventTypes/temp/Temp.cs +++ /dev/null @@ -1,17 +0,0 @@ -using LibMatrix.EventTypes.Events; - -namespace LibMatrix.EventTypes.temp; - -// public class Temp { -// public MatrixEventCollection Members = [ -// new MatrixEvent() { -// Content = new() { -// Membership = "join" -// } -// } -// ]; -// -// public void a() { -// -// } -// } \ No newline at end of file diff --git a/LibMatrix/Utilities/CommonSyncFilters.cs b/LibMatrix/Utilities/CommonSyncFilters.cs index 68836f5..04672c5 100644 --- a/LibMatrix/Utilities/CommonSyncFilters.cs +++ b/LibMatrix/Utilities/CommonSyncFilters.cs @@ -36,13 +36,13 @@ public static class CommonSyncFilters { AccountData = new MatrixFilter.RoomFilter.StateFilter(rooms: []), Ephemeral = new MatrixFilter.RoomFilter.StateFilter(rooms: []), State = new MatrixFilter.RoomFilter.StateFilter { - Types = new List { + Types = [ RoomCreateLegacyEventContent.EventId, RoomNameLegacyEventContent.EventId, RoomAvatarLegacyEventContent.EventId, MjolnirShortcodeLegacyEventContent.EventId, RoomPowerLevelLegacyEventContent.EventId - }, + ], LazyLoadMembers = true, IncludeRedundantMembers = false }, Timeline = new MatrixFilter.RoomFilter.StateFilter(rooms: []) -- cgit 1.4.1