From a129b321998614b20e4ebb8a7c1632553ebee981 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Sat, 1 Jun 2024 19:02:28 +0200 Subject: Split event abstractions --- .../Events/RoomMembershipEventContent.cs | 12 ---- .../Events/RoomMessageEventContent.cs | 57 ----------------- LibMatrix.EventTypes/LibMatrix.EventTypes.csproj | 14 ----- LibMatrix.EventTypes/MatrixEvent.cs | 9 --- LibMatrix.EventTypes/MatrixEventCollection.cs | 71 ---------------------- LibMatrix.EventTypes/MatrixEventContent.cs | 60 ------------------ LibMatrix.EventTypes/temp/Program.cs | 30 --------- 7 files changed, 253 deletions(-) delete mode 100644 LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs delete mode 100644 LibMatrix.EventTypes/Events/RoomMessageEventContent.cs delete mode 100644 LibMatrix.EventTypes/LibMatrix.EventTypes.csproj delete mode 100644 LibMatrix.EventTypes/MatrixEvent.cs delete mode 100644 LibMatrix.EventTypes/MatrixEventCollection.cs delete mode 100644 LibMatrix.EventTypes/MatrixEventContent.cs delete mode 100644 LibMatrix.EventTypes/temp/Program.cs (limited to 'LibMatrix.EventTypes') diff --git a/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs b/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs deleted file mode 100644 index fe50a2e..0000000 --- a/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Text.Json.Serialization; - -namespace LibMatrix.EventTypes.Events; - -[MatrixEvent("m.room.member")] -[JsonConverter(typeof(MatrixEventContentConverter))] -public class RoomMembershipEventContent : MatrixEventContent { - public string Membership { - 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 deleted file mode 100644 index 55c2b6c..0000000 --- a/LibMatrix.EventTypes/Events/RoomMessageEventContent.cs +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index bd33993..0000000 --- a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net8.0 - enable - enable - Exe - - - - - - - diff --git a/LibMatrix.EventTypes/MatrixEvent.cs b/LibMatrix.EventTypes/MatrixEvent.cs deleted file mode 100644 index 63f1e75..0000000 --- a/LibMatrix.EventTypes/MatrixEvent.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Text.Json.Serialization; - -namespace LibMatrix.EventTypes; - -public interface IMatrixEvent where T : MatrixEventContent; -public class MatrixEvent : IMatrixEvent where T : MatrixEventContent { - [JsonPropertyName("content")] - public T? Content { get; set; } -} \ No newline at end of file diff --git a/LibMatrix.EventTypes/MatrixEventCollection.cs b/LibMatrix.EventTypes/MatrixEventCollection.cs deleted file mode 100644 index 78886d9..0000000 --- a/LibMatrix.EventTypes/MatrixEventCollection.cs +++ /dev/null @@ -1,71 +0,0 @@ -// 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 deleted file mode 100644 index 81b8c52..0000000 --- a/LibMatrix.EventTypes/MatrixEventContent.cs +++ /dev/null @@ -1,60 +0,0 @@ -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 { - - [JsonExtensionData, JsonInclude] - public JsonObject InternalJson { get; set; } = new(); - - - - public MatrixEventContent() { } - - public MatrixEventContent(JsonNode json) { - InternalJson = json.AsObject(); - } - - 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 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 deleted file mode 100644 index 22a65d4..0000000 --- a/LibMatrix.EventTypes/temp/Program.cs +++ /dev/null @@ -1,30 +0,0 @@ -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 -- cgit 1.4.1