diff options
Diffstat (limited to 'MatrixRoomUtils.Core/Helpers/SyncHelper.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Helpers/SyncHelper.cs | 88 |
1 files changed, 73 insertions, 15 deletions
diff --git a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs index edbb646..04c31cd 100644 --- a/MatrixRoomUtils.Core/Helpers/SyncHelper.cs +++ b/MatrixRoomUtils.Core/Helpers/SyncHelper.cs @@ -1,5 +1,7 @@ +using System.Diagnostics.CodeAnalysis; using System.Net.Http.Json; using System.Text.Json.Serialization; +using MatrixRoomUtils.Core.Interfaces; using MatrixRoomUtils.Core.Responses; using MatrixRoomUtils.Core.Services; using MatrixRoomUtils.Core.StateEventTypes; @@ -15,7 +17,7 @@ public class SyncHelper { _storageService = storageService; } - public async Task<SyncResult?> Sync(string? since = null) { + public async Task<SyncResult?> Sync(string? since = null, CancellationToken? cancellationToken = null) { var outFileName = "sync-" + (await _storageService.CacheStorageProvider.GetAllKeys()).Count(x => x.StartsWith("sync")) + ".json"; @@ -23,12 +25,66 @@ public class SyncHelper { if (!string.IsNullOrWhiteSpace(since)) url += $"&since={since}"; else url += "&full_state=true"; Console.WriteLine("Calling: " + url); - var res = await _homeServer._httpClient.GetFromJsonAsync<SyncResult>(url); - await _storageService.CacheStorageProvider.SaveObject(outFileName, res); - return res; + try { + var res = await _homeServer._httpClient.GetFromJsonAsync<SyncResult>(url, + cancellationToken: cancellationToken ?? CancellationToken.None); + await _storageService.CacheStorageProvider.SaveObject(outFileName, res); + Console.WriteLine($"Wrote file: {outFileName}"); + return res; + } + catch (TaskCanceledException) { + Console.WriteLine("Sync cancelled!"); + } + catch (Exception e) { + Console.WriteLine(e); + } + return null; + } + + [SuppressMessage("ReSharper", "FunctionNeverReturns")] + public async Task RunSyncLoop(CancellationToken? cancellationToken = null, bool skipInitialSyncEvents = true) { + SyncResult? sync = null; + while (cancellationToken is null || !cancellationToken.Value.IsCancellationRequested) { + sync = await Sync(sync?.NextBatch, cancellationToken); + Console.WriteLine($"Got sync, next batch: {sync?.NextBatch}!"); + if (sync == null) continue; + if (sync.Rooms is { Invite.Count: > 0 }) { + foreach (var roomInvite in sync.Rooms.Invite) { + Console.WriteLine(roomInvite.Value.GetType().Name); + InviteReceived?.Invoke(this, roomInvite); + } + } + + if (sync.AccountData is { Events: { Count: > 0 } }) { + foreach (var accountDataEvent in sync.AccountData.Events) { + AccountDataReceived?.Invoke(this, accountDataEvent); + } + } + + // 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) { + foreach (var stateEventResponse in updatedRoom.Value.Timeline.Events) { + stateEventResponse.RoomId = updatedRoom.Key; + TimelineEventReceived?.Invoke(this, stateEventResponse); + } + } + } + } } - - public event EventHandler<SyncResult>? ; + + /// <summary> + /// Event fired when a room invite is received + /// </summary> + public event EventHandler<KeyValuePair<string, SyncResult.RoomsDataStructure.InvitedRoomDataStructure>>? InviteReceived; + + public event EventHandler<StateEventResponse>? TimelineEventReceived; + public event EventHandler<StateEventResponse>? AccountDataReceived; } public class SyncResult { @@ -36,30 +92,30 @@ public class SyncResult { public string NextBatch { get; set; } [JsonPropertyName("account_data")] - public EventList AccountData { get; set; } + public EventList? AccountData { get; set; } [JsonPropertyName("presence")] - public PresenceDataStructure Presence { get; set; } + 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; } + public RoomsDataStructure? Rooms { get; set; } // supporting classes public class PresenceDataStructure { [JsonPropertyName("events")] - public List<StateEventResponse<PresenceStateEventData>> Events { get; set; } + public List<StateEventResponse> Events { get; set; } } public class RoomsDataStructure { [JsonPropertyName("join")] - public Dictionary<string, JoinedRoomDataStructure> Join { get; set; } + public Dictionary<string, JoinedRoomDataStructure>? Join { get; set; } [JsonPropertyName("invite")] - public Dictionary<string, InvitedRoomDataStructure> Invite { get; set; } - + public Dictionary<string, InvitedRoomDataStructure>? Invite { get; set; } + public class JoinedRoomDataStructure { [JsonPropertyName("timeline")] public TimelineDataStructure Timeline { get; set; } @@ -75,7 +131,7 @@ public class SyncResult { [JsonPropertyName("unread_notifications")] public UnreadNotificationsDataStructure UnreadNotifications { get; set; } - + [JsonPropertyName("summary")] public SummaryDataStructure Summary { get; set; } @@ -97,12 +153,14 @@ public class SyncResult { [JsonPropertyName("highlight_count")] public int HighlightCount { get; set; } } - + public class SummaryDataStructure { [JsonPropertyName("m.heroes")] public List<string> Heroes { get; set; } + [JsonPropertyName("m.invited_member_count")] public int InvitedMemberCount { get; set; } + [JsonPropertyName("m.joined_member_count")] public int JoinedMemberCount { get; set; } } |