about summary refs log tree commit diff
path: root/LibMatrix/Extensions
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2024-09-16 08:43:42 +0200
committerRory& <root@rory.gay>2024-09-16 08:43:42 +0200
commitb93c5e7e284c73c97192bf4ff7002c4b396ed2e9 (patch)
tree254ea85c256b7f8c582a6755e7918ba48585ae87 /LibMatrix/Extensions
parentmeow (diff)
downloadLibMatrix-b93c5e7e284c73c97192bf4ff7002c4b396ed2e9.tar.xz
Diffstat (limited to 'LibMatrix/Extensions')
-rw-r--r--LibMatrix/Extensions/EnumerableExtensions.cs90
-rw-r--r--LibMatrix/Extensions/MatrixHttpClient.Single.cs42
2 files changed, 101 insertions, 31 deletions
diff --git a/LibMatrix/Extensions/EnumerableExtensions.cs b/LibMatrix/Extensions/EnumerableExtensions.cs
index 42d9491..ace2c0c 100644
--- a/LibMatrix/Extensions/EnumerableExtensions.cs
+++ b/LibMatrix/Extensions/EnumerableExtensions.cs
@@ -1,29 +1,91 @@
+using System.Collections.Frozen;
+using System.Collections.Immutable;
+
 namespace LibMatrix.Extensions;
 
 public static class EnumerableExtensions {
+    public static int insertions = 0;
+    public static int replacements = 0;
+
     public static void MergeStateEventLists(this IList<StateEvent> oldState, IList<StateEvent> newState) {
-        foreach (var stateEvent in newState) {
-            var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
-            if (old is null) {
-                oldState.Add(stateEvent);
-                continue;
+        // foreach (var stateEvent in newState) {
+        //     var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
+        //     if (old is null) {
+        //         oldState.Add(stateEvent);
+        //         continue;
+        //     }
+        //
+        //     oldState.Remove(old);
+        //     oldState.Add(stateEvent);
+        // }
+
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    break;
             }
+        }
 
-            oldState.Remove(old);
-            oldState.Add(stateEvent);
+        int FindIndex(StateEvent needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
+            }
+
+            return -1;
         }
     }
 
     public static void MergeStateEventLists(this IList<StateEventResponse> oldState, IList<StateEventResponse> newState) {
-        foreach (var stateEvent in newState) {
-            var old = oldState.FirstOrDefault(x => x.Type == stateEvent.Type && x.StateKey == stateEvent.StateKey);
-            if (old is null) {
-                oldState.Add(stateEvent);
-                continue;
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    break;
+            }
+        }
+
+        int FindIndex(StateEventResponse needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
+            }
+
+            return -1;
+        }
+    }
+
+    public static void MergeStateEventLists(this List<StateEventResponse> oldState, List<StateEventResponse> newState) {
+        foreach (var e in newState) {
+            switch (FindIndex(e)) {
+                case -1:
+                    oldState.Add(e);
+                    insertions++;
+                    break;
+                case var index:
+                    oldState[index] = e;
+                    replacements++;
+                    break;
+            }
+        }
+
+        int FindIndex(StateEventResponse needle) {
+            for (int i = 0; i < oldState.Count; i++) {
+                var old = oldState[i];
+                if (old.Type == needle.Type && old.StateKey == needle.StateKey)
+                    return i;
             }
 
-            oldState.Remove(old);
-            oldState.Add(stateEvent);
+            return -1;
         }
     }
 }
\ No newline at end of file
diff --git a/LibMatrix/Extensions/MatrixHttpClient.Single.cs b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
index 3c8aea4..7548a2c 100644
--- a/LibMatrix/Extensions/MatrixHttpClient.Single.cs
+++ b/LibMatrix/Extensions/MatrixHttpClient.Single.cs
@@ -50,6 +50,7 @@ public class MatrixHttpClient {
     internal SemaphoreSlim _rateLimitSemaphore { get; } = new(1, 1);
 #endif
 
+    private const bool LogRequests = true;
     public Dictionary<string, string> AdditionalQueryParameters { get; set; } = new();
 
     public Uri? BaseAddress { get; set; }
@@ -71,20 +72,21 @@ public class MatrixHttpClient {
     public async Task<HttpResponseMessage> SendUnhandledAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
         if (request.RequestUri is null) throw new NullReferenceException("RequestUri is null");
         // if (!request.RequestUri.IsAbsoluteUri) 
-            request.RequestUri = request.RequestUri.EnsureAbsolute(BaseAddress!);
+        request.RequestUri = request.RequestUri.EnsureAbsolute(BaseAddress!);
         var swWait = Stopwatch.StartNew();
 #if SYNC_HTTPCLIENT
         await _rateLimitSemaphore.WaitAsync(cancellationToken);
 #endif
         swWait.Stop();
         var swExec = Stopwatch.StartNew();
-       
+
         foreach (var (key, value) in AdditionalQueryParameters) request.RequestUri = request.RequestUri.AddQuery(key, value);
         foreach (var (key, value) in DefaultRequestHeaders) request.Headers.Add(key, value);
         request.Options.Set(new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse"), true);
 
-        Console.WriteLine("Sending " + request.Summarise(includeHeaders:true, includeQuery: true, includeContentIfText: true));
-        
+        if (LogRequests)
+            Console.WriteLine("Sending " + request.Summarise(includeHeaders: true, includeQuery: true, includeContentIfText: true, hideHeaders: ["Accept"]));
+
         HttpResponseMessage? responseMessage;
         try {
             responseMessage = await Client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
@@ -101,19 +103,25 @@ public class MatrixHttpClient {
 #endif
 
         // Console.WriteLine($"Sending {request.Method} {request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)}) -> {(int)responseMessage.StatusCode} {responseMessage.StatusCode} ({Util.BytesToString(responseMessage.GetContentLength())}, WAIT={swWait.ElapsedMilliseconds}ms, EXEC={swExec.ElapsedMilliseconds}ms)");
-        Console.WriteLine("Received " + responseMessage.Summarise(includeHeaders: true, includeContentIfText: false, hideHeaders: [
-            "Server",
-            "Date",
-            "Transfer-Encoding",
-            "Connection",
-            "Vary",
-            "Content-Length",
-            "Access-Control-Allow-Origin",
-            "Access-Control-Allow-Methods",
-            "Access-Control-Allow-Headers",
-            "Access-Control-Expose-Headers",
-            "Cache-Control"    
-        ]));
+        if (LogRequests)
+            Console.WriteLine("Received " + responseMessage.Summarise(includeHeaders: true, includeContentIfText: false, hideHeaders: [
+                "Server",
+                "Date",
+                "Transfer-Encoding",
+                "Connection",
+                "Vary",
+                "Content-Length",
+                "Access-Control-Allow-Origin",
+                "Access-Control-Allow-Methods",
+                "Access-Control-Allow-Headers",
+                "Access-Control-Expose-Headers",
+                "Cache-Control",
+                "Cross-Origin-Resource-Policy",
+                "X-Content-Security-Policy",
+                "Referrer-Policy",
+                "X-Robots-Tag",
+                "Content-Security-Policy"
+            ]));
 
         return responseMessage;
     }