about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--LibMatrix/Extensions/HttpClientExtensions.cs4
-rw-r--r--LibMatrix/Helpers/SyncHelper.cs18
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs3
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs5
-rw-r--r--LibMatrix/WhoAmIResponse.cs2
-rw-r--r--Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs13
6 files changed, 33 insertions, 12 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; }
diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
index f56f9c1..6d2cba2 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs
@@ -37,7 +37,7 @@ public class CommandListenerHostedService : IHostedService {
 
     private async Task? Run(CancellationToken cancellationToken) {
         _logger.LogInformation("Starting command listener!");
-        var syncHelper = new SyncHelper(_hs);
+        var syncHelper = new SyncHelper(_hs, _logger);
         syncHelper.TimelineEventHandlers.Add(async @event => {
             try {
                 var room = _hs.GetRoom(@event.RoomId);
@@ -81,14 +81,17 @@ public class CommandListenerHostedService : IHostedService {
                 _logger.LogError(e, "Error in command listener!");
             }
         });
-        await new SyncHelper(_hs){Timeout = 2500}.RunSyncLoopAsync(cancellationToken: cancellationToken);
+        await syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken);
     }
 
     /// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
     /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
-    public Task StopAsync(CancellationToken cancellationToken) {
+    public async Task StopAsync(CancellationToken cancellationToken) {
         _logger.LogInformation("Shutting down command listener!");
-        _listenerTask.Wait(cancellationToken);
-        return Task.CompletedTask;
+        if (_listenerTask is null) {
+            _logger.LogError("Could not shut down command listener task because it was null!");
+            return;
+        }
+        await _listenerTask.WaitAsync(cancellationToken);
     }
 }