about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LibMatrix.EventTypes/Events/RoomMembershipEventContent.cs6
-rw-r--r--LibMatrix.EventTypes/Events/RoomMessageEventContent.cs57
-rw-r--r--LibMatrix.EventTypes/LibMatrix.EventTypes.csproj5
-rw-r--r--LibMatrix.EventTypes/MatrixEventCollection.cs142
-rw-r--r--LibMatrix.EventTypes/MatrixEventContent.cs48
-rw-r--r--LibMatrix.EventTypes/temp/Program.cs30
-rw-r--r--LibMatrix.EventTypes/temp/Temp.cs17
-rw-r--r--LibMatrix/Utilities/CommonSyncFilters.cs4
8 files changed, 210 insertions, 99 deletions
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<RoomMembershipEventContent>))]
 public class RoomMembershipEventContent : MatrixEventContent {
     public string Membership {
-        get => _json["membership"]!.GetValue<string>();
-        set => Console.WriteLine(value);
+        get => InternalJson["membership"]!.GetValue<string>();
+        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; }
+
+    /// <summary>
+    /// Media URI for this message, if any
+    /// </summary>
+    [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 @@
         <TargetFramework>net8.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
+        <OutputType>Exe</OutputType>
     </PropertyGroup>
 
+    <ItemGroup>
+      <ProjectReference Include="..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj" />
+    </ItemGroup>
+
 </Project>
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<out T> : IEnumerable<IMatrixEvent<T>> where T : MatrixEventContent {
-    
-}
-public class MatrixEventCollection : IMatrixEventCollection<MatrixEventContent>, IList<MatrixEvent<MatrixEventContent> {
-    private IList<MatrixEvent<MatrixEventContent>> _listImplementation;
-    public IEnumerator<MatrixEvent<MatrixEventContent>> GetEnumerator() => _listImplementation.GetEnumerator();
-
-    IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator();
-
-    public void Add(MatrixEvent<MatrixEventContent> item) => _listImplementation.Add(item);
-
-    public void Clear() => _listImplementation.Clear();
-
-    public bool Contains(MatrixEvent<MatrixEventContent> item) => _listImplementation.Contains(item);
-
-    public void CopyTo(MatrixEvent<MatrixEventContent>[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex);
-
-    public bool Remove(MatrixEvent<MatrixEventContent> item) => _listImplementation.Remove(item);
-
-    public int Count => _listImplementation.Count;
-
-    public bool IsReadOnly => _listImplementation.IsReadOnly;
-
-    public int IndexOf(MatrixEvent<MatrixEventContent> item) => _listImplementation.IndexOf(item);
-
-    public void Insert(int index, MatrixEvent<MatrixEventContent> item) => _listImplementation.Insert(index, item);
-
-    public void RemoveAt(int index) => _listImplementation.RemoveAt(index);
-
-    public MatrixEvent<MatrixEventContent> this[int index] {
-        get => _listImplementation[index];
-        set => _listImplementation[index] = value;
-    }
-}
-public class MatrixEventCollection<T> : IMatrixEventCollection<T>, IList<MatrixEvent<T>> where T : MatrixEventContent {
-    //TODO: implement
-    
-    private IList<MatrixEvent<T>> _listImplementation = new List<MatrixEvent<T>>();
-    public IEnumerator<MatrixEvent<T>> GetEnumerator() => _listImplementation.GetEnumerator();
-
-    IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator();
-
-    public void Add(MatrixEvent<T> item) => _listImplementation.Add(item);
-
-    public void Clear() => _listImplementation.Clear();
-
-    public bool Contains(MatrixEvent<T> item) => _listImplementation.Contains(item);
-
-    public void CopyTo(MatrixEvent<T>[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex);
-
-    public bool Remove(MatrixEvent<T> item) => _listImplementation.Remove(item);
-
-    public int Count => _listImplementation.Count;
-
-    public bool IsReadOnly => _listImplementation.IsReadOnly;
-
-    public int IndexOf(MatrixEvent<T> item) => _listImplementation.IndexOf(item);
-
-    public void Insert(int index, MatrixEvent<T> item) => _listImplementation.Insert(index, item);
-
-    public void RemoveAt(int index) => _listImplementation.RemoveAt(index);
-
-    public MatrixEvent<T> 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<out T> : IEnumerable<IMatrixEvent<T>> where T : MatrixEventContent {
+//     
+// }
+// public class MatrixEventCollection : IMatrixEventCollection<MatrixEventContent>, IList<MatrixEvent<MatrixEventContent> {
+//     private IList<MatrixEvent<MatrixEventContent>> _listImplementation;
+//     public IEnumerator<MatrixEvent<MatrixEventContent>> GetEnumerator() => _listImplementation.GetEnumerator();
+//
+//     IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator();
+//
+//     public void Add(MatrixEvent<MatrixEventContent> item) => _listImplementation.Add(item);
+//
+//     public void Clear() => _listImplementation.Clear();
+//
+//     public bool Contains(MatrixEvent<MatrixEventContent> item) => _listImplementation.Contains(item);
+//
+//     public void CopyTo(MatrixEvent<MatrixEventContent>[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex);
+//
+//     public bool Remove(MatrixEvent<MatrixEventContent> item) => _listImplementation.Remove(item);
+//
+//     public int Count => _listImplementation.Count;
+//
+//     public bool IsReadOnly => _listImplementation.IsReadOnly;
+//
+//     public int IndexOf(MatrixEvent<MatrixEventContent> item) => _listImplementation.IndexOf(item);
+//
+//     public void Insert(int index, MatrixEvent<MatrixEventContent> item) => _listImplementation.Insert(index, item);
+//
+//     public void RemoveAt(int index) => _listImplementation.RemoveAt(index);
+//
+//     public MatrixEvent<MatrixEventContent> this[int index] {
+//         get => _listImplementation[index];
+//         set => _listImplementation[index] = value;
+//     }
+// }
+// public class MatrixEventCollection<T> : IMatrixEventCollection<T>, IList<MatrixEvent<T>> where T : MatrixEventContent {
+//     //TODO: implement
+//     
+//     private IList<MatrixEvent<T>> _listImplementation = new List<MatrixEvent<T>>();
+//     public IEnumerator<MatrixEvent<T>> GetEnumerator() => _listImplementation.GetEnumerator();
+//
+//     IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_listImplementation).GetEnumerator();
+//
+//     public void Add(MatrixEvent<T> item) => _listImplementation.Add(item);
+//
+//     public void Clear() => _listImplementation.Clear();
+//
+//     public bool Contains(MatrixEvent<T> item) => _listImplementation.Contains(item);
+//
+//     public void CopyTo(MatrixEvent<T>[] array, int arrayIndex) => _listImplementation.CopyTo(array, arrayIndex);
+//
+//     public bool Remove(MatrixEvent<T> item) => _listImplementation.Remove(item);
+//
+//     public int Count => _listImplementation.Count;
+//
+//     public bool IsReadOnly => _listImplementation.IsReadOnly;
+//
+//     public int IndexOf(MatrixEvent<T> item) => _listImplementation.IndexOf(item);
+//
+//     public void Insert(int index, MatrixEvent<T> item) => _listImplementation.Insert(index, item);
+//
+//     public void RemoveAt(int index) => _listImplementation.RemoveAt(index);
+//
+//     public MatrixEvent<T> 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;
 
+// <T> : MatrixEventContent where T : MatrixEventContent<T>, new() {
 /// <summary>
 ///     Extensible Event Content, aims to provide an API similar to JsonNode/JsonObject
 ///     <seealso cref="System.Text.Json.Nodes.JsonNode"/>
 ///     <seealso cref="System.Text.Json.Nodes.JsonObject"/>
 /// </summary>
+[JsonConverter(typeof(MatrixEventContentConverter<MatrixEventContent>))]
 public class MatrixEventContent {
-    // <T> : MatrixEventContent where T : MatrixEventContent<T>, 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<string> EventTypes => this.GetType().GetCustomAttributes<MatrixEventAttribute>().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<T> : JsonConverter<T> 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<MatrixEventContent> Members = [
+    //     new MatrixEvent<RoomMembershipEventContent>() {
+    //         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<MatrixEvent<RoomMembershipEventContent>>(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<MatrixEventContent> Members = [
-//         new MatrixEvent<RoomMembershipEventContent>() {
-//             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<string> {
+                Types = [
                     RoomCreateLegacyEventContent.EventId,
                     RoomNameLegacyEventContent.EventId,
                     RoomAvatarLegacyEventContent.EventId,
                     MjolnirShortcodeLegacyEventContent.EventId,
                     RoomPowerLevelLegacyEventContent.EventId
-                },
+                ],
                 LazyLoadMembers = true, IncludeRedundantMembers = false
             },
             Timeline = new MatrixFilter.RoomFilter.StateFilter(rooms: [])