diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-11 15:18:02 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-10-11 15:18:02 +0200 |
commit | b475ba5cf146114a820b464e901b5129b2abfe84 (patch) | |
tree | a7855482df8f6cb602834bfbac256b3443d0fae5 /LibMatrix | |
parent | Set timeout for command listener (diff) | |
download | LibMatrix-bak-b475ba5cf146114a820b464e901b5129b2abfe84.tar.xz |
Fix bug with event sending, make initial sync detection deterministic
Diffstat (limited to 'LibMatrix')
-rw-r--r-- | LibMatrix/Extensions/HttpClientExtensions.cs | 4 | ||||
-rw-r--r-- | LibMatrix/Helpers/SyncHelper.cs | 18 | ||||
-rw-r--r-- | LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 3 | ||||
-rw-r--r-- | LibMatrix/RoomTypes/GenericRoom.cs | 5 | ||||
-rw-r--r-- | LibMatrix/WhoAmIResponse.cs | 2 |
5 files changed, 25 insertions, 7 deletions
diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs index 2fe99b6..6f1eab6 100644 --- a/LibMatrix/Extensions/HttpClientExtensions.cs +++ b/LibMatrix/Extensions/HttpClientExtensions.cs @@ -4,6 +4,7 @@ using System.Net.Http.Headers; using System.Reflection; using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; using ArcaneLibs.Extensions; namespace LibMatrix.Extensions; @@ -93,7 +94,8 @@ public class MatrixHttpClient : HttpClient { CancellationToken cancellationToken = default) { var request = new HttpRequestMessage(HttpMethod.Put, requestUri); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - request.Content = new StringContent(JsonSerializer.Serialize(value, value.GetType()), Encoding.UTF8, "application/json"); + request.Content = new StringContent(JsonSerializer.Serialize(value, value.GetType(), options ?? new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }), + Encoding.UTF8, "application/json"); return await SendAsync(request, cancellationToken); } } diff --git a/LibMatrix/Helpers/SyncHelper.cs b/LibMatrix/Helpers/SyncHelper.cs index a793d76..6bdf5ad 100644 --- a/LibMatrix/Helpers/SyncHelper.cs +++ b/LibMatrix/Helpers/SyncHelper.cs @@ -14,7 +14,6 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public SyncFilter? Filter { get; set; } public bool FullState { get; set; } = false; - 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}"; @@ -37,7 +36,7 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg } public async IAsyncEnumerable<SyncResponse> EnumerateSyncAsync(CancellationToken? cancellationToken = null) { - while(!cancellationToken?.IsCancellationRequested ?? true) { + while (!cancellationToken?.IsCancellationRequested ?? true) { var sync = await SyncAsync(cancellationToken); if (sync is null) continue; Since = sync.NextBatch ?? Since; @@ -47,14 +46,25 @@ public class SyncHelper(AuthenticatedHomeserverGeneric homeserver, ILogger? logg public async Task RunSyncLoopAsync(bool skipInitialSyncEvents = true, CancellationToken? cancellationToken = null) { var sw = Stopwatch.StartNew(); + bool isInitialSync = true; + int emptyInitialSyncCount = 0; + var oldTimeout = Timeout; + Timeout = 0; 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); + if (sync?.ToJson(ignoreNull: true, indent: false).Length < 250) { + emptyInitialSyncCount++; + if (emptyInitialSyncCount > 5) { + isInitialSync = false; + Timeout = oldTimeout; + } + } + + await RunSyncLoopCallbacksAsync(sync, isInitialSync && skipInitialSyncEvents); } } private async Task RunSyncLoopCallbacksAsync(SyncResponse syncResponse, bool isInitialSync) { - var tasks = SyncReceivedHandlers.Select(x => x(syncResponse)).ToList(); await Task.WhenAll(tasks); diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index f6c530d..c78b799 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -39,7 +39,8 @@ public class AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) public WhoAmIResponse? WhoAmI { get; set; } - public string UserId => WhoAmI.UserId; + public string? UserId => WhoAmI?.UserId; + public string? UserLocalpart => UserId?.Split(":")[0][1..]; // public virtual async Task<WhoAmIResponse> WhoAmI() { // if (_whoAmI is not null) return _whoAmI; diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index ae41f1e..37006af 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -264,6 +264,11 @@ public class GenericRoom { } #endregion + + public async Task InviteUsersAsync(IEnumerable<string> users) { + var tasks = users.Select(x=>InviteUserAsync(x)).ToList(); + await Task.WhenAll(tasks); + } } public class RoomIdResponse { diff --git a/LibMatrix/WhoAmIResponse.cs b/LibMatrix/WhoAmIResponse.cs index f461c9b..3884b1d 100644 --- a/LibMatrix/WhoAmIResponse.cs +++ b/LibMatrix/WhoAmIResponse.cs @@ -4,7 +4,7 @@ namespace LibMatrix; public class WhoAmIResponse { [JsonPropertyName("user_id")] - public string UserId { get; set; } = null!; + public string? UserId { get; set; } = null!; [JsonPropertyName("device_id")] public string? DeviceId { get; set; } |