about summary refs log tree commit diff
path: root/LibMatrix/Helpers/SyncHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/Helpers/SyncHelper.cs')
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs259
1 files changed, 73 insertions, 186 deletions
diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs
index 74972a1..06ae3fe 100644
--- a/LibMatrix/Helpers/SyncHelper.cs
+++ b/LibMatrix/Helpers/SyncHelper.cs
@@ -1,228 +1,115 @@
+using System.Diagnostics;
 using System.Diagnostics.CodeAnalysis;
 using System.Net.Http.Json;
-using System.Text.Json.Serialization;
 using ArcaneLibs.Extensions;
 using LibMatrix.Filters;
 using LibMatrix.Homeservers;
 using LibMatrix.Responses;
 using LibMatrix.Services;
+using Microsoft.Extensions.Logging;
 
 namespace LibMatrix.Helpers;
 
-public class SyncHelper(AuthenticatedHomeserverGeneric homeserver) {
-    public async Task<SyncResult?> Sync(
-        string? since = null,
-        int? timeout = 30000,
-        string? setPresence = "online",
-        SyncFilter? filter = null,
-        CancellationToken? cancellationToken = null) {
-        var url = $"/_matrix/client/v3/sync?timeout={timeout}&set_presence={setPresence}";
-        if (!string.IsNullOrWhiteSpace(since)) url += $"&since={since}";
-        if (filter is not null) url += $"&filter={filter.ToJson(ignoreNull: true, indent: false)}";
-        // else url += "&full_state=true";
-        Console.WriteLine("Calling: " + url);
-        try {
-            var req = await homeserver._httpClient.GetAsync(url, cancellationToken: cancellationToken ?? CancellationToken.None);
+public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logger = null) {
+    public string? Since { get; set; }
+    public int Timeout { get; set; } = 30000;
+    public string? SetPresence { get; set; } = "online";
+    public SyncFilter? Filter { get; set; }
+    public bool FullState { get; set; } = false;
 
-#if DEBUG && false
-            try {
-                await homeserver._httpClient.PostAsync(
-                    "http://localhost:5116/validate/" + typeof(SyncResult).AssemblyQualifiedName,
-                    new StreamContent(await req.Content.ReadAsStreamAsync()));
-            }
 
-            catch (Exception e) {
-                Console.WriteLine("[!!] Checking sync response failed: " + e);
-            }
-            var res = await req.Content.ReadFromJsonAsync<SyncResult>();
-            return res;
-#else
-            return await req.Content.ReadFromJsonAsync<SyncResult>();
-#endif
+    public async Task<SyncResponse?> SyncAsync(CancellationToken? cancellationToken = null) {
+        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._httpClient.GetFromJsonAsync<SyncResponse>(url, cancellationToken: cancellationToken ?? CancellationToken.None);
         }
         catch (TaskCanceledException) {
             Console.WriteLine("Sync cancelled!");
+            logger?.LogWarning("Sync cancelled due to TaskCanceledException!");
         }
         catch (Exception e) {
             Console.WriteLine(e);
+            logger?.LogError(e, "Failed to sync!\n{}", e.ToString());
         }
 
         return null;
     }
 
-    [SuppressMessage("ReSharper", "FunctionNeverReturns")]
-    public async Task RunSyncLoop(
-        bool skipInitialSyncEvents = true,
-        string? since = null,
-        int? timeout = 30000,
-        string? setPresence = "online",
-        SyncFilter? filter = null,
-        CancellationToken? cancellationToken = null
-    ) {
-        // await Task.WhenAll((await storageService.CacheStorageProvider.GetAllKeysAsync())
-        //     .Where(x => x.StartsWith("sync"))
-        //     .ToList()
-        //     .Select(x => storageService.CacheStorageProvider.DeleteObjectAsync(x)));
-        var nextBatch = since;
-        while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) {
-            var sync = await Sync(since: nextBatch, timeout: timeout, setPresence: setPresence, filter: filter,
-                cancellationToken: cancellationToken);
-            nextBatch = sync?.NextBatch ?? nextBatch;
+    public async IAsyncEnumerable<SyncResponse> EnumerateSyncAsync(CancellationToken? cancellationToken = null) {
+        while(!cancellationToken?.IsCancellationRequested ?? true) {
+            var sync = await SyncAsync(cancellationToken);
             if (sync is null) continue;
-            Console.WriteLine($"Got sync, next batch: {nextBatch}!");
-
-            if (sync.Rooms is { Invite.Count: > 0 }) {
-                foreach (var roomInvite in sync.Rooms.Invite) {
-                    var tasks = InviteReceivedHandlers.Select(x => x(roomInvite)).ToList();
-                    await Task.WhenAll(tasks);
-                }
-            }
-
-            if (sync.AccountData is { Events: { Count: > 0 } }) {
-                foreach (var accountDataEvent in sync.AccountData.Events) {
-                    var tasks = AccountDataReceivedHandlers.Select(x => x(accountDataEvent)).ToList();
-                    await Task.WhenAll(tasks);
-                }
-            }
-
-            // Things that are skipped on the first sync
-            if (skipInitialSyncEvents) {
-                skipInitialSyncEvents = false;
-                continue;
-            }
-
-            if (sync.Rooms is { Join.Count: > 0 }) {
-                foreach (var updatedRoom in sync.Rooms.Join) {
-                    if(updatedRoom.Value.Timeline is null) continue;
-                    foreach (var stateEventResponse in updatedRoom.Value.Timeline.Events) {
-                        stateEventResponse.RoomId = updatedRoom.Key;
-                        var tasks = TimelineEventHandlers.Select(x => {
-                            try {
-                                return x(stateEventResponse);
-                            }
-                            catch (Exception e) {
-                                Console.WriteLine(e);
-                                return Task.CompletedTask;
-                            }
-                        }).ToList();
-                        await Task.WhenAll(tasks);
-                    }
-                }
-            }
+            Since = sync.NextBatch ?? Since;
+            yield return sync;
         }
     }
 
-    /// <summary>
-    /// Event fired when a room invite is received
-    /// </summary>
-    public List<Func<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>, Task>>
-        InviteReceivedHandlers { get; } = new();
-
-    public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; } = new();
-    public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new();
-}
-
-public class SyncResult {
-    [JsonPropertyName("next_batch")]
-    public string NextBatch { get; set; }
-
-    [JsonPropertyName("account_data")]
-    public EventList? AccountData { get; set; }
-
-    [JsonPropertyName("presence")]
-    public PresenceDataStructure? Presence { get; set; }
-
-    [JsonPropertyName("device_one_time_keys_count")]
-    public Dictionary<string, int> DeviceOneTimeKeysCount { get; set; }
-
-    [JsonPropertyName("rooms")]
-    public RoomsDataStructure? Rooms { get; set; }
-
-    [JsonPropertyName("to_device")]
-    public EventList? ToDevice { get; set; }
-
-    [JsonPropertyName("device_lists")]
-    public DeviceListsDataStructure? DeviceLists { get; set; }
-
-    public class DeviceListsDataStructure {
-        [JsonPropertyName("changed")]
-        public List<string>? Changed { get; set; }
-
-        [JsonPropertyName("left")]
-        public List<string>? Left { get; set; }
-    }
-
-    // supporting classes
-    public class PresenceDataStructure {
-        [JsonPropertyName("events")]
-        public List<StateEventResponse> Events { get; set; }
+    public async Task RunSyncLoopAsync(bool skipInitialSyncEvents = true, CancellationToken? cancellationToken = null) {
+        var sw = Stopwatch.StartNew();
+        await foreach (var sync in EnumerateSyncAsync(cancellationToken)) {
+            logger?.LogInformation("Got sync response: {} bytes, {} elapsed", sync?.ToJson(ignoreNull: true, indent: false).Length ?? -1, sw.Elapsed);
+            await RunSyncLoopCallbacksAsync(sync, Since is null && skipInitialSyncEvents);
+        }
     }
 
-    public class RoomsDataStructure {
-        [JsonPropertyName("join")]
-        public Dictionary<string, JoinedRoomDataStructure>? Join { get; set; }
-
-        [JsonPropertyName("invite")]
-        public Dictionary<string, InvitedRoomDataStructure>? Invite { get; set; }
-
-        public class JoinedRoomDataStructure {
-            [JsonPropertyName("timeline")]
-            public TimelineDataStructure? Timeline { get; set; }
-
-            [JsonPropertyName("state")]
-            public EventList State { get; set; }
-
-            [JsonPropertyName("account_data")]
-            public EventList AccountData { get; set; }
-
-            [JsonPropertyName("ephemeral")]
-            public EventList Ephemeral { get; set; }
-
-            [JsonPropertyName("unread_notifications")]
-            public UnreadNotificationsDataStructure UnreadNotifications { get; set; }
-
-            [JsonPropertyName("summary")]
-            public SummaryDataStructure Summary { get; set; }
+    private async Task RunSyncLoopCallbacksAsync(SyncResponse syncResponse, bool isInitialSync) {
 
-            public class TimelineDataStructure {
-                [JsonPropertyName("events")]
-                public List<StateEventResponse> Events { get; set; }
+        var tasks = SyncReceivedHandlers.Select(x => x(syncResponse)).ToList();
+        await Task.WhenAll(tasks);
 
-                [JsonPropertyName("prev_batch")]
-                public string PrevBatch { get; set; }
-
-                [JsonPropertyName("limited")]
-                public bool Limited { get; set; }
+        if (syncResponse.AccountData is { Events: { Count: > 0 } }) {
+            foreach (var accountDataEvent in syncResponse.AccountData.Events) {
+                tasks = AccountDataReceivedHandlers.Select(x => x(accountDataEvent)).ToList();
+                await Task.WhenAll(tasks);
             }
+        }
 
-            public class UnreadNotificationsDataStructure {
-                [JsonPropertyName("notification_count")]
-                public int NotificationCount { get; set; }
+        await RunSyncLoopRoomCallbacksAsync(syncResponse, isInitialSync);
+    }
 
-                [JsonPropertyName("highlight_count")]
-                public int HighlightCount { get; set; }
+    private async Task RunSyncLoopRoomCallbacksAsync(SyncResponse syncResponse, bool isInitialSync) {
+        if (syncResponse.Rooms is { Invite.Count: > 0 }) {
+            foreach (var roomInvite in syncResponse.Rooms.Invite) {
+                var tasks = InviteReceivedHandlers.Select(x => x(roomInvite)).ToList();
+                await Task.WhenAll(tasks);
             }
+        }
 
-            public class SummaryDataStructure {
-                [JsonPropertyName("m.heroes")]
-                public List<string> Heroes { get; set; }
-
-                [JsonPropertyName("m.invited_member_count")]
-                public int InvitedMemberCount { get; set; }
+        if (isInitialSync) return;
 
-                [JsonPropertyName("m.joined_member_count")]
-                public int JoinedMemberCount { get; set; }
+        if (syncResponse.Rooms is { Join.Count: > 0 }) {
+            foreach (var updatedRoom in syncResponse.Rooms.Join) {
+                if (updatedRoom.Value.Timeline is null) continue;
+                foreach (var stateEventResponse in updatedRoom.Value.Timeline.Events) {
+                    stateEventResponse.RoomId = updatedRoom.Key;
+                    var tasks = TimelineEventHandlers.Select(x => x(stateEventResponse)).ToList();
+                    await Task.WhenAll(tasks);
+                }
             }
         }
-
-        public class InvitedRoomDataStructure {
-            [JsonPropertyName("invite_state")]
-            public EventList InviteState { get; set; }
-        }
     }
-}
 
-public class EventList {
-    [JsonPropertyName("events")]
-    public List<StateEventResponse> Events { get; set; }
+    /// <summary>
+    /// Event fired when a sync response is received
+    /// </summary>
+    public List<Func<SyncResponse, Task>> SyncReceivedHandlers { get; } = new();
+
+    /// <summary>
+    /// Event fired when a room invite is received
+    /// </summary>
+    public List<Func<KeyValuePair<string, SyncResponse.RoomsDataStructure.InvitedRoomDataStructure>, Task>> InviteReceivedHandlers { get; } = new();
+
+    /// <summary>
+    /// Event fired when a timeline event is received
+    /// </summary>
+    public List<Func<StateEventResponse, Task>> TimelineEventHandlers { get; } = new();
+
+    /// <summary>
+    /// Event fired when an account data event is received
+    /// </summary>
+    public List<Func<StateEventResponse, Task>> AccountDataReceivedHandlers { get; } = new();
 }