about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-12-07 07:26:02 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-12-07 07:26:02 +0100
commit83f9a4df147ef58c884f43092527f5cb6fa2f0a9 (patch)
tree336f0cc008723f8dba11a480249de7c76ad3e9ea /LibMatrix
parentCleanup, move ArcaneLibs to submodule instead of parent submodule (diff)
downloadLibMatrix-83f9a4df147ef58c884f43092527f5cb6fa2f0a9.tar.xz
Temp state
Diffstat (limited to '')
-rw-r--r--LibMatrix.sln16
-rw-r--r--LibMatrix.sln.DotSettings.user1
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs28
-rw-r--r--LibMatrix/Helpers/SyncStateResolver.cs2
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs3
-rw-r--r--LibMatrix/StateEvent.cs42
6 files changed, 63 insertions, 29 deletions
diff --git a/LibMatrix.sln b/LibMatrix.sln
index 87398f3..f94131b 100644
--- a/LibMatrix.sln
+++ b/LibMatrix.sln
@@ -27,6 +27,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.Tests", "Tests\Li
 EndProject

 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestDataGenerator", "Tests\TestDataGenerator\TestDataGenerator.csproj", "{0B9B34D1-9362-45A9-9C21-816FD6959110}"

 EndProject

+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LibMatrix.JsonSerializerContextGenerator", "Utilities\LibMatrix.JsonSerializerContextGenerator\LibMatrix.JsonSerializerContextGenerator.csproj", "{4D9B5227-48DC-4A30-9263-AFB51DC01ABB}"

+EndProject

+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ArcaneLibs", "ArcaneLibs", "{01A126FE-9D50-40F2-817B-E55F4065EA76}"

+EndProject

+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArcaneLibs", "ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj", "{13A797D1-7E13-4789-A167-8628B1641AC0}"

+EndProject

 Global

 	GlobalSection(SolutionConfigurationPlatforms) = preSolution

 		Debug|Any CPU = Debug|Any CPU

@@ -72,6 +78,14 @@ Global
 		{0B9B34D1-9362-45A9-9C21-816FD6959110}.Debug|Any CPU.Build.0 = Debug|Any CPU

 		{0B9B34D1-9362-45A9-9C21-816FD6959110}.Release|Any CPU.ActiveCfg = Release|Any CPU

 		{0B9B34D1-9362-45A9-9C21-816FD6959110}.Release|Any CPU.Build.0 = Release|Any CPU

+		{4D9B5227-48DC-4A30-9263-AFB51DC01ABB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU

+		{4D9B5227-48DC-4A30-9263-AFB51DC01ABB}.Debug|Any CPU.Build.0 = Debug|Any CPU

+		{4D9B5227-48DC-4A30-9263-AFB51DC01ABB}.Release|Any CPU.ActiveCfg = Release|Any CPU

+		{4D9B5227-48DC-4A30-9263-AFB51DC01ABB}.Release|Any CPU.Build.0 = Release|Any CPU

+		{13A797D1-7E13-4789-A167-8628B1641AC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU

+		{13A797D1-7E13-4789-A167-8628B1641AC0}.Debug|Any CPU.Build.0 = Debug|Any CPU

+		{13A797D1-7E13-4789-A167-8628B1641AC0}.Release|Any CPU.ActiveCfg = Release|Any CPU

+		{13A797D1-7E13-4789-A167-8628B1641AC0}.Release|Any CPU.Build.0 = Release|Any CPU

 	EndGlobalSection

 	GlobalSection(NestedProjects) = preSolution

 		{1B1B2197-61FB-416F-B6C8-845F2E5A0442} = {840309F0-435B-43A7-8471-8C2BE643889D}

@@ -81,5 +95,7 @@ Global
 		{FB8FE4EB-B53B-464B-A5FD-9BF9D0F3EF9B} = {840309F0-435B-43A7-8471-8C2BE643889D}

 		{345934FF-CA81-4A4B-B137-9F198102C65F} = {BFE16D8E-EFC5-49F6-9854-DB001309B3B4}

 		{0B9B34D1-9362-45A9-9C21-816FD6959110} = {BFE16D8E-EFC5-49F6-9854-DB001309B3B4}

+		{4D9B5227-48DC-4A30-9263-AFB51DC01ABB} = {A6345ECE-4C5E-400F-9130-886E343BF314}

+		{13A797D1-7E13-4789-A167-8628B1641AC0} = {01A126FE-9D50-40F2-817B-E55F4065EA76}

 	EndGlobalSection

 EndGlobal

diff --git a/LibMatrix.sln.DotSettings.user b/LibMatrix.sln.DotSettings.user
index 822b0e8..f720bf5 100644
--- a/LibMatrix.sln.DotSettings.user
+++ b/LibMatrix.sln.DotSettings.user
@@ -1,4 +1,5 @@
 <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
+	<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=_002Fhome_002FRory_002Fgit_002Fmatrix_002FMatrixRoomUtils_002FLibMatrix_002FArcaneLibs_002FArcaneLibs_002Fbin_002FDebug_002Fnet8_002E0_002FArcaneLibs_002Edll/@EntryIndexedValue">True</s:Boolean>
 	<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;
   &lt;Assembly Path="/home/root@Rory/.cache/NuGetPackages/microsoft.extensions.hosting.abstractions/7.0.0/lib/net7.0/Microsoft.Extensions.Hosting.Abstractions.dll" /&gt;
 &lt;/AssemblyExplorer&gt;</s:String></wpf:ResourceDictionary>
\ No newline at end of file
diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs
index a05b915..334c288 100644
--- a/LibMatrix/Helpers/SyncHelper.cs
+++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -1,4 +1,5 @@
 using System.Diagnostics;
+using System.Net.Http.Json;
 using ArcaneLibs.Extensions;
 using LibMatrix.Filters;
 using LibMatrix.Homeservers;
@@ -12,23 +13,41 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
     public int Timeout { get; set; } = 30000;
     public string? SetPresence { get; set; } = "online";
     public SyncFilter? Filter { get; set; }
-    public bool FullState { get; set; } = false;
+    public bool FullState { get; set; }
 
     public bool IsInitialSync { get; set; } = true;
 
+    public TimeSpan MinimumDelay { get; set; } = new(0);
+
     public async Task<SyncResponse?> SyncAsync(CancellationToken? cancellationToken = null) {
         if (homeserver is null) {
             Console.WriteLine("Null passed as homeserver for SyncHelper!");
-            throw new ArgumentNullException("Null passed as homeserver for SyncHelper!");
+            throw new ArgumentNullException(nameof(homeserver), "Null passed as homeserver for SyncHelper!");
+        }
+        if (homeserver.ClientHttpClient is null) {
+            Console.WriteLine("Homeserver for SyncHelper is not properly configured!");
+            throw new ArgumentNullException(nameof(homeserver.ClientHttpClient), "Null passed as homeserver for SyncHelper!");
         }
 
+
+        var sw = Stopwatch.StartNew();
+
         var url = $"/_matrix/client/v3/sync?timeout={Timeout}&set_presence={SetPresence}&full_state={(FullState ? "true" : "false")}";
         if (!string.IsNullOrWhiteSpace(Since)) url += $"&since={Since}";
         if (Filter is not null) url += $"&filter={Filter.ToJson(ignoreNull: true, indent: false)}";
         // Console.WriteLine("Calling: " + url);
         logger?.LogInformation("SyncHelper: Calling: {}", url);
         try {
-            return await homeserver?.ClientHttpClient?.GetFromJsonAsync<SyncResponse>(url, cancellationToken: cancellationToken ?? CancellationToken.None)!;
+            var httpResp = await homeserver.ClientHttpClient.GetAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None)!;
+            if (httpResp is null) throw new NullReferenceException("Failed to send HTTP request");
+            logger?.LogInformation("Got sync response: {} bytes, {} elapsed", httpResp?.Content.Headers.ContentLength ?? -1, sw.Elapsed);
+            var deserializeSw = Stopwatch.StartNew();
+            var resp = await httpResp.Content.ReadFromJsonAsync<SyncResponse>(cancellationToken: cancellationToken ?? CancellationToken.None)!;
+            logger?.LogInformation("Deserialized sync response: {} bytes, {} elapsed, {} total", httpResp?.Content.Headers.ContentLength ?? -1, deserializeSw.Elapsed, sw.Elapsed);
+            var timeToWait = MinimumDelay.Subtract(sw.Elapsed);
+            if (timeToWait.TotalMilliseconds > 0)
+                await Task.Delay(timeToWait);
+            return resp;
         }
         catch (TaskCanceledException) {
             Console.WriteLine("Sync cancelled!");
@@ -57,7 +76,6 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
         var oldTimeout = Timeout;
         Timeout = 0;
         await foreach (var sync in EnumerateSyncAsync(cancellationToken)) {
-            logger?.LogInformation("Got sync response: {} bytes, {} elapsed", sync?.ToJson(ignoreNull: true, indent: false).Length ?? -1, sw.Elapsed);
             if (sync?.ToJson(ignoreNull: true, indent: false).Length < 250) {
                 emptyInitialSyncCount++;
                 if (emptyInitialSyncCount > 5) {
@@ -125,4 +143,4 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
     /// Event fired when an account data event is received
     /// </summary>
     public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new();
-}
+}
\ No newline at end of file
diff --git a/LibMatrix/Helpers/SyncStateResolver.cs b/LibMatrix/Helpers/SyncStateResolver.cs
index 3482be3..f40fa22 100644
--- a/LibMatrix/Helpers/SyncStateResolver.cs
+++ b/LibMatrix/Helpers/SyncStateResolver.cs
@@ -13,7 +13,7 @@ public class SyncStateResolver(AuthenticatedHomeserverGeneric homeserver, ILogge
     public SyncFilter? Filter { get; set; }
     public bool FullState { get; set; } = false;
 
-    public SyncResponse? MergedState { get; set; } = null!;
+    public SyncResponse? MergedState { get; set; }
 
     private SyncHelper _syncHelper = new SyncHelper(homeserver, logger);
 
diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs
index a64e167..9804e78 100644
--- a/LibMatrix/RoomTypes/GenericRoom.cs
+++ b/LibMatrix/RoomTypes/GenericRoom.cs
@@ -173,7 +173,8 @@ public class GenericRoom {
         var resText = await res.Content.ReadAsStringAsync();
         Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}");
         var result = await JsonSerializer.DeserializeAsync<ChunkedStateEventResponse>(await res.Content.ReadAsStreamAsync(), new JsonSerializerOptions() {
-            TypeInfoResolver = ChunkedStateEventResponseSerializerContext.Default
+            TypeInfoResolver = ChunkedStateEventResponseSerializerContext.Default,
+            
         });
         Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}");
         foreach (var resp in result.Chunk) {
diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs
index 6d69820..6ca82f4 100644
--- a/LibMatrix/StateEvent.cs
+++ b/LibMatrix/StateEvent.cs
@@ -1,3 +1,4 @@
+using System.Diagnostics.CodeAnalysis;
 using System.Reflection;
 using System.Text.Json;
 using System.Text.Json.Nodes;
@@ -29,26 +30,26 @@ public class StateEvent {
             return typeof(Dictionary<string, JsonObject>);
         }
 
-        // var eventType = KnownStateEventTypes.FirstOrDefault(x =>
-        // x.GetCustomAttributes<MatrixEventAttribute>()?.Any(y => y.EventName == type) ?? false);
-        var eventType = KnownStateEventTypesByName.GetValueOrDefault(type);
-
-        return eventType ?? typeof(UnknownEventContent);
+        return KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent);
     }
 
+    private static readonly JsonSerializerOptions TypedContentSerializerOptions = new JsonSerializerOptions() {
+        Converters = {
+            new JsonFloatStringConverter(),
+            new JsonDoubleStringConverter(),
+            new JsonDecimalStringConverter()
+        }
+    };
+
     [JsonIgnore]
-    public EventContent TypedContent {
+    [SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")]
+    public EventContent? TypedContent {
         get {
-            if (Type == "m.receipt") {
-                return null!;
-            }
+            // if (Type == "m.receipt") {
+                // return null;
+            // }
             try {
-                var jse = new JsonSerializerOptions();
-                jse ??= new JsonSerializerOptions();
-                jse.Converters.Add(new JsonFloatStringConverter());
-                jse.Converters.Add(new JsonDoubleStringConverter());
-                jse.Converters.Add(new JsonDecimalStringConverter());
-                return (EventContent)RawContent.Deserialize(GetType, jse)!;
+                return (EventContent)RawContent.Deserialize(GetType, TypedContentSerializerOptions)!;
             }
             catch (JsonException e) {
                 Console.WriteLine(e);
@@ -59,7 +60,7 @@ public class StateEvent {
         }
         set {
             if (value is null) {
-                RawContent = null;
+                RawContent?.Clear();
             }
             else RawContent = JsonSerializer.Deserialize<JsonObject>(JsonSerializer.Serialize(value, value.GetType()));
         }
@@ -69,7 +70,7 @@ public class StateEvent {
     public string StateKey { get; set; } = "";
 
     [JsonPropertyName("type")]
-    public string Type { get; set; }
+    public required string Type { get; set; }
 
     [JsonPropertyName("replaces_state")]
     public string? ReplacesState { get; set; }
@@ -121,7 +122,7 @@ public class StateEvent {
 
     //debug
     [JsonIgnore]
-    public string dtype {
+    public string InternalSelfTypeName {
         get {
             var res = GetType().Name switch {
                 "StateEvent`1" => "StateEvent",
@@ -132,7 +133,7 @@ public class StateEvent {
     }
 
     [JsonIgnore]
-    public string cdtype => TypedContent.GetType().Name;
+    public string InternalContentTypeName => TypedContent?.GetType().Name ?? "null";
 }
 
 
@@ -152,9 +153,6 @@ public class StateEventResponse : StateEvent {
     [JsonPropertyName("event_id")]
     public string EventId { get; set; }
 
-    // [JsonPropertyName("user_id")]
-    // public string UserId { get; set; }
-
     [JsonPropertyName("replaces_state")]
     public new string ReplacesState { get; set; }