From cf90f02e1f9c9f7d037976cace2b9c49119c741c Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 30 May 2024 19:07:09 +0000 Subject: Start implementing new event system --- .../Events/RoomMembershipEventContent.cs | 10 +++ LibMatrix.EventTypes/MatrixEvent.cs | 9 +++ LibMatrix.EventTypes/MatrixEventCollection.cs | 71 ++++++++++++++++++++++ LibMatrix.EventTypes/MatrixEventContent.cs | 26 ++++++++ LibMatrix.EventTypes/temp/Temp.cs | 17 ++++++ 5 files changed, 133 insertions(+) create mode 100644 LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs create mode 100644 LibMatrix.EventTypes/MatrixEvent.cs create mode 100644 LibMatrix.EventTypes/MatrixEventCollection.cs create mode 100644 LibMatrix.EventTypes/MatrixEventContent.cs create mode 100644 LibMatrix.EventTypes/temp/Temp.cs (limited to 'LibMatrix.EventTypes') diff --git a/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs b/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs new file mode 100644 index 0000000..dac7094 --- /dev/null +++ b/LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs @@ -0,0 +1,10 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes.Events; + +public class RoomMembershipEventContent : MatrixEventContent { + public string Membership { + get => _json["membership"]!.GetValue(); + set => Console.WriteLine(value); + } +} \ No newline at end of file diff --git a/LibMatrix.EventTypes/MatrixEvent.cs b/LibMatrix.EventTypes/MatrixEvent.cs new file mode 100644 index 0000000..63f1e75 --- /dev/null +++ b/LibMatrix.EventTypes/MatrixEvent.cs @@ -0,0 +1,9 @@ +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 new file mode 100644 index 0000000..35afd2b --- /dev/null +++ b/LibMatrix.EventTypes/MatrixEventCollection.cs @@ -0,0 +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 diff --git a/LibMatrix.EventTypes/MatrixEventContent.cs b/LibMatrix.EventTypes/MatrixEventContent.cs new file mode 100644 index 0000000..c30ebb0 --- /dev/null +++ b/LibMatrix.EventTypes/MatrixEventContent.cs @@ -0,0 +1,26 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; + +namespace LibMatrix.EventTypes; + +/// +/// Extensible Event Content, aims to provide an API similar to JsonNode/JsonObject +/// +/// +/// +public class MatrixEventContent { + // : MatrixEventContent where T : MatrixEventContent, new() { + internal JsonNode _json = new JsonObject(); + + public static implicit operator MatrixEventContent(JsonNode json) => new(json); + + [JsonConstructor] + public MatrixEventContent(JsonNode json) { + _json = json; + } + + public MatrixEventContent() { } + + public JsonNode? this[string key] => _json[key]; +} \ No newline at end of file diff --git a/LibMatrix.EventTypes/temp/Temp.cs b/LibMatrix.EventTypes/temp/Temp.cs new file mode 100644 index 0000000..3fbb401 --- /dev/null +++ b/LibMatrix.EventTypes/temp/Temp.cs @@ -0,0 +1,17 @@ +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 -- cgit 1.4.1