about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix')
-rw-r--r--LibMatrix/Extensions/HttpClientExtensions.cs4
-rw-r--r--LibMatrix/Helpers/MessageBuilder.cs40
-rw-r--r--LibMatrix/Helpers/MessageFormatter.cs24
-rw-r--r--LibMatrix/LibMatrix.csproj1
-rw-r--r--LibMatrix/StateEvent.cs38
5 files changed, 97 insertions, 10 deletions
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs
index 71bb0e5..c0366fb 100644
--- a/LibMatrix/Extensions/HttpClientExtensions.cs
+++ b/LibMatrix/Extensions/HttpClientExtensions.cs
@@ -135,8 +135,8 @@ public class MatrixHttpClient : HttpClient {
         var request = new HttpRequestMessage(HttpMethod.Put, requestUri);
         request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         Console.WriteLine($"Sending PUT {requestUri}");
-        Console.WriteLine($"Content: {JsonSerializer.Serialize(value, value.GetType(), options)}");
-        Console.WriteLine($"Type: {value.GetType().FullName}");
+        // Console.WriteLine($"Content: {JsonSerializer.Serialize(value, value.GetType(), options)}");
+        // Console.WriteLine($"Type: {value.GetType().FullName}");
         request.Content = new StringContent(JsonSerializer.Serialize(value, value.GetType(), options),
             Encoding.UTF8, "application/json");
         return await SendAsync(request, cancellationToken);
diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs
new file mode 100644
index 0000000..7715462
--- /dev/null
+++ b/LibMatrix/Helpers/MessageBuilder.cs
@@ -0,0 +1,40 @@
+using ArcaneLibs;
+using LibMatrix.EventTypes.Spec;
+
+namespace LibMatrix.Helpers;
+
+public class MessageBuilder(string msgType = "m.text", string format = "org.matrix.custom.html") {
+    private RoomMessageEventContent Content { get; set; } = new() {
+        MessageType = msgType,
+        Format = format
+    };
+    
+    public RoomMessageEventContent Build() => Content;
+    
+    public MessageBuilder WithColoredBody(string color, string body) {
+        Content.Body += body;
+        Content.FormattedBody += $"<font color=\"{color}\">{body}</font>";
+        return this;
+    }
+    
+    public MessageBuilder WithColoredBody(string color, Action<MessageBuilder> bodyBuilder) {
+        Content.FormattedBody += $"<font color=\"{color}\">";
+        bodyBuilder(this);
+        Content.FormattedBody += "</font>";
+        return this;
+    }
+
+    public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) {
+        if (useLength) {
+            lengthFactor = text.Length;
+        }
+        RainbowEnumerator enumerator = new(skip, offset, lengthFactor);
+        for (int i = 0; i < text.Length; i++) {
+            var (r, g, b) = enumerator.Next();
+            Content.FormattedBody += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>";
+        }
+
+        return this;
+    }
+    
+}
\ No newline at end of file
diff --git a/LibMatrix/Helpers/MessageFormatter.cs b/LibMatrix/Helpers/MessageFormatter.cs
index b2dda61..b7c6975 100644
--- a/LibMatrix/Helpers/MessageFormatter.cs
+++ b/LibMatrix/Helpers/MessageFormatter.cs
@@ -13,7 +13,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatException(string error, Exception e) {
         return new RoomMessageEventContent(body: $"{error}: {e.Message}", messageType: "m.text") {
-            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre>{e.Message}</pre></font>",
+            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre><code>{e.Message}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -27,7 +27,7 @@ public static class MessageFormatter {
 
     public static RoomMessageEventContent FormatSuccessJson(string text, object data) {
         return new RoomMessageEventContent(body: text, messageType: "m.text") {
-            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre>{data.ToJson(ignoreNull: true)}</pre></font>",
+            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
             Format = "org.matrix.custom.html"
         };
     }
@@ -53,4 +53,24 @@ public static class MessageFormatter {
             Format = "org.matrix.custom.html"
         };
     }
+    
+    public static RoomMessageEventContent FormatWarningJson(string warning, object data) {
+        return new RoomMessageEventContent(body: warning, messageType: "m.text") {
+            FormattedBody = $"<font color=\"#FFFF00\">{warning}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>",
+            Format = "org.matrix.custom.html"
+        };
+    }
+    
+    public static RoomMessageEventContent Concat(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}{b.Body}", messageType: a.MessageType) {
+            FormattedBody = $"{a.FormattedBody}{b.FormattedBody}",
+            Format = a.Format
+        };
+    }
+    public static RoomMessageEventContent ConcatLine(this RoomMessageEventContent a, RoomMessageEventContent b) {
+        return new RoomMessageEventContent(body: $"{a.Body}\n{b.Body}", messageType: "m.text") {
+            FormattedBody = $"{a.FormattedBody}<br/>{b.FormattedBody}",
+            Format = "org.matrix.custom.html"
+        };
+    }
 }
diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj
index 57d194d..a2ee327 100644
--- a/LibMatrix/LibMatrix.csproj
+++ b/LibMatrix/LibMatrix.csproj
@@ -11,6 +11,7 @@
     </PropertyGroup>
 
     <ItemGroup>
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
         <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
     </ItemGroup>
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
index 4a0adbd..1a8df11 100644
--- a/LibMatrix/StateEvent.cs
+++ b/LibMatrix/StateEvent.cs
@@ -6,7 +6,9 @@ using System.Text.Json;
 using System.Text.Json.Nodes;
 using System.Text.Json.Serialization;
 using ArcaneLibs;
+using ArcaneLibs.Attributes;
 using ArcaneLibs.Extensions;
+using Castle.DynamicProxy;
 using LibMatrix.EventTypes;
 using LibMatrix.Extensions;
 
@@ -14,7 +16,7 @@ namespace LibMatrix;
 
 public class StateEvent {
     public static FrozenSet<Type> KnownStateEventTypes { get; } = new ClassCollector<EventContent>().ResolveFromAllAccessibleAssemblies().ToFrozenSet();
-
+    
     public static FrozenDictionary<string, Type> KnownStateEventTypesByName { get; } = KnownStateEventTypes.Aggregate(
         new Dictionary<string, Type>(),
         (dict, type) => {
@@ -22,14 +24,24 @@ public class StateEvent {
             foreach (var attr in attrs) {
                 dict[attr.EventName] = type;
             }
+
             return dict;
         }).ToFrozenDictionary();
 
     public static Type GetStateEventType(string type) => KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent);
-    
+
     [JsonIgnore]
     public Type MappedType => GetStateEventType(Type);
 
+    [JsonIgnore]
+    public bool IsLegacyType => MappedType.GetCustomAttributes<MatrixEventAttribute>().FirstOrDefault(x => x.EventName == Type)?.Legacy ?? false;
+
+    [JsonIgnore]
+    public string FriendlyTypeName => MappedType.GetFriendlyNameOrNull() ?? Type;
+
+    [JsonIgnore]
+    public string FriendlyTypeNamePlural => MappedType.GetFriendlyNamePluralOrNull() ?? Type;
+
     private static readonly JsonSerializerOptions TypedContentSerializerOptions = new() {
         Converters = {
             new JsonFloatStringConverter(),
@@ -38,15 +50,30 @@ public class StateEvent {
         }
     };
 
+    private class EventContentInterceptor : IInterceptor {
+        public void Intercept(IInvocation invocation) {
+            Console.WriteLine($"Intercepting {invocation.Method.Name}");
+            // if (invocation.Method.Name == "ToString") {
+            //     invocation.ReturnValue = "EventContent";
+            //     return;
+            // }
+
+            invocation.Proceed();
+        }
+    }
+    
     [JsonIgnore]
     [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
     public EventContent? TypedContent {
         get {
             // if (Type == "m.receipt") {
-                // return null;
+            // return null;
             // }
             try {
-                return (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                var c= (EventContent)RawContent.Deserialize(GetStateEventType(Type), TypedContentSerializerOptions)!;
+                // c = (EventContent)new ProxyGenerator().CreateClassProxyWithTarget(GetStateEventType(Type), c, new EventContentInterceptor());
+                // Console.WriteLine(c.GetType().Name + ": " + string.Join(", ", c.GetType().GetRuntimeProperties().Select(x=>x.Name)));
+                return c;
             }
             catch (JsonException e) {
                 Console.WriteLine(e);
@@ -127,7 +154,6 @@ public class StateEvent {
     public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null";
 }
 
-
 public class StateEventResponse : StateEvent {
     [JsonPropertyName("origin_server_ts")]
     public ulong? OriginServerTs { get; set; }
@@ -213,4 +239,4 @@ public class StateEventContentPolymorphicTypeInfoResolver : DefaultJsonTypeInfoR
 }
 */
 
-#endregion
+#endregion
\ No newline at end of file