about summary refs log tree commit diff
path: root/LibMatrix/Helpers
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2024-01-31 12:09:28 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2024-01-31 12:09:28 +0100
commit9f8d0c85c54b4715974994aea52562072d6f1751 (patch)
treece5eaf47b02fb82bc99236b926eb9948322745f7 /LibMatrix/Helpers
parentGet full state event (diff)
downloadLibMatrix-9f8d0c85c54b4715974994aea52562072d6f1751.tar.xz
Better sync filter support, named filters, error handling
Diffstat (limited to '')
-rw-r--r--LibMatrix/Helpers/HomeserverWeightEstimation.cs2
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs56
2 files changed, 52 insertions, 6 deletions
diff --git a/LibMatrix/Helpers/HomeserverWeightEstimation.cs b/LibMatrix/Helpers/HomeserverWeightEstimation.cs
index 8f1bf3a..02f9185 100644
--- a/LibMatrix/Helpers/HomeserverWeightEstimation.cs
+++ b/LibMatrix/Helpers/HomeserverWeightEstimation.cs
@@ -2,7 +2,7 @@ namespace LibMatrix.Helpers;
 
 public class HomeserverWeightEstimation {
     public static Dictionary<string, int> EstimatedSize = new() {
-        { "matrix.org", 84387 },
+        { "matrix.org", 843870 },
         { "anontier.nl", 44809 },
         { "nixos.org", 8195 },
         { "the-apothecary.club", 6983 },
diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs
index 691b964..636cfdd 100644
--- a/LibMatrix/Helpers/SyncHelper.cs
+++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -11,16 +11,59 @@ using Microsoft.Extensions.Logging;
 namespace LibMatrix.Helpers;
 
 public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logger = null) {
+    private SyncFilter? _filter;
+    private string? _namedFilterName;
+    private bool _filterIsDirty = false;
+    private string? _filterId = null;
+
     public string? Since { get; set; }
     public int Timeout { get; set; } = 30000;
     public string? SetPresence { get; set; } = "online";
-    public SyncFilter? Filter { get; set; }
+
+    public string? FilterId {
+        get => _filterId;
+        set {
+            _filterId = value;
+            _namedFilterName = null;
+            _filter = null;
+        }
+    }
+    public string? NamedFilterName {
+        get => _namedFilterName;
+        set {
+            _namedFilterName = value;
+            _filterIsDirty = true;
+            _filterId = null;
+        }
+    }
+
+    public SyncFilter? Filter {
+        get => _filter;
+        set {
+            _filter = value;
+            _filterIsDirty = true;
+            _filterId = null;
+        }
+    }
+
     public bool FullState { get; set; }
 
     public bool IsInitialSync { get; set; } = true;
 
     public TimeSpan MinimumDelay { get; set; } = new(0);
 
+    private async Task updateFilterAsync() {
+        if (!string.IsNullOrWhiteSpace(NamedFilterName)) {
+            _filterId = await homeserver.GetNamedFilterIdOrNullAsync(NamedFilterName);
+            if (_filterId is null)
+                if (logger is null) Console.WriteLine($"Failed to get filter ID for named filter {NamedFilterName}");
+                else logger.LogWarning("Failed to get filter ID for named filter {NamedFilterName}", NamedFilterName);
+        }
+        else if (Filter is not null)
+            _filterId = (await homeserver.UploadFilterAsync(Filter)).FilterId;
+        else _filterId = null;
+    }
+
     public async Task<SyncResponse?> SyncAsync(CancellationToken? cancellationToken = null) {
         if (homeserver is null) {
             Console.WriteLine("Null passed as homeserver for SyncHelper!");
@@ -33,12 +76,14 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
         }
 
         var sw = Stopwatch.StartNew();
+        if (_filterIsDirty) await updateFilterAsync();
 
         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);
+        if (_filterId is not null) url += $"&filter={_filterId}";
+        
         logger?.LogInformation("SyncHelper: Calling: {}", url);
+        
         try {
             var httpResp = await homeserver.ClientHttpClient.GetAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None);
             if (httpResp is null) throw new NullReferenceException("Failed to send HTTP request");
@@ -99,14 +144,15 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg
                     },
                     ToDevice: null or {
                         Events: null or { Count: 0 }
-                    } 
+                    }
                 }) {
                 emptyInitialSyncCount++;
                 if (emptyInitialSyncCount >= 2) {
                     IsInitialSync = false;
                     Timeout = oldTimeout;
                 }
-            } else if (syncCount > 15) 
+            }
+            else if (syncCount > 15)
                 Console.WriteLine(sync.ToJson(ignoreNull: true, indent: true));
 
             await RunSyncLoopCallbacksAsync(sync, IsInitialSync && skipInitialSyncEvents);