From f866946fbbe962c712049327ade9dcbd43900295 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sun, 25 Jun 2023 03:07:05 +0200 Subject: Working state, refactored Rory&::LibMatrix --- MatrixRoomUtils.Core/Helpers/SyncHelper.cs | 88 +++++++++++++++++++++++++----- 1 file changed, 73 insertions(+), 15 deletions(-) (limited to 'MatrixRoomUtils.Core/Helpers') 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 Sync(string? since = null) { + public async Task 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(url); - await _storageService.CacheStorageProvider.SaveObject(outFileName, res); - return res; + try { + var res = await _homeServer._httpClient.GetFromJsonAsync(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? ; + + /// + /// Event fired when a room invite is received + /// + public event EventHandler>? InviteReceived; + + public event EventHandler? TimelineEventReceived; + public event EventHandler? 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 DeviceOneTimeKeysCount { get; set; } [JsonPropertyName("rooms")] - public RoomsDataStructure Rooms { get; set; } + public RoomsDataStructure? Rooms { get; set; } // supporting classes public class PresenceDataStructure { [JsonPropertyName("events")] - public List> Events { get; set; } + public List Events { get; set; } } public class RoomsDataStructure { [JsonPropertyName("join")] - public Dictionary Join { get; set; } + public Dictionary? Join { get; set; } [JsonPropertyName("invite")] - public Dictionary Invite { get; set; } - + public Dictionary? 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 Heroes { get; set; } + [JsonPropertyName("m.invited_member_count")] public int InvitedMemberCount { get; set; } + [JsonPropertyName("m.joined_member_count")] public int JoinedMemberCount { get; set; } } -- cgit 1.5.1