diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs
index 9a5e3d9..ad78779 100644
--- a/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs
+++ b/Utilities/LibMatrix.Utilities.Bot/Services/InviteListenerHostedService.cs
@@ -2,7 +2,7 @@ using System.Diagnostics.CodeAnalysis;
using LibMatrix.Filters;
using LibMatrix.Helpers;
using LibMatrix.Homeservers;
-using LibMatrix.Responses;
+using LibMatrix.Utilities.Bot.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -13,9 +13,10 @@ public class InviteHandlerHostedService(
ILogger<InviteHandlerHostedService> logger,
AuthenticatedHomeserverGeneric hs,
InviteHandlerHostedService.InviteListenerSyncConfiguration listenerSyncConfiguration,
- Func<InviteHandlerHostedService.InviteEventArgs, Task> inviteHandler
+ Func<RoomInviteContext, Task> inviteHandler
) : IHostedService {
private Task? _listenerTask;
+ private CancellationTokenSource _cts = new();
private readonly SyncHelper _syncHelper = new(hs, logger) {
Timeout = listenerSyncConfiguration.Timeout ?? 30_000,
@@ -37,14 +38,15 @@ public class InviteHandlerHostedService(
_syncHelper.Filter = listenerSyncConfiguration.Filter;
}
else {
- _syncHelper.FilterId = await hs.NamedCaches.FilterCache.GetOrSetValueAsync("gay.rory.libmatrix.utilities.bot.invite_listener_syncfilter.dev", new SyncFilter() {
- AccountData = new SyncFilter.EventFilter(types: [], limit: 1),
- Presence = new SyncFilter.EventFilter(types: ["*"]),
+ _syncHelper.FilterId = await hs.NamedCaches.FilterCache.GetOrSetValueAsync("gay.rory.libmatrix.utilities.bot.invite_listener_syncfilter.dev0", new SyncFilter() {
+ AccountData = SyncFilter.EventFilter.Empty,
+ Presence = SyncFilter.EventFilter.Empty,
Room = new SyncFilter.RoomFilter {
- AccountData = new SyncFilter.RoomFilter.StateFilter(types: [], limit: 1),
- Ephemeral = new SyncFilter.RoomFilter.StateFilter(types: [], limit: 1),
- State = new SyncFilter.RoomFilter.StateFilter(types: []),
+ AccountData = SyncFilter.RoomFilter.StateFilter.Empty,
+ Ephemeral = SyncFilter.RoomFilter.StateFilter.Empty,
+ State = SyncFilter.RoomFilter.StateFilter.Empty,
Timeline = new SyncFilter.RoomFilter.StateFilter(types: [], notSenders: [hs.WhoAmI.UserId]),
+ IncludeLeave = false
}
});
}
@@ -55,7 +57,7 @@ public class InviteHandlerHostedService(
_syncHelper.InviteReceivedHandlers.Add(async invite => {
logger.LogInformation("Received invite to room {}", invite.Key);
- var inviteEventArgs = new InviteEventArgs() {
+ var inviteEventArgs = new RoomInviteContext() {
RoomId = invite.Key,
InviteData = invite.Value,
MemberEvent = invite.Value.InviteState?.Events?.First(x => x.Type == "m.room.member" && x.StateKey == hs.WhoAmI.UserId)
@@ -70,7 +72,7 @@ public class InviteHandlerHostedService(
if (!listenerSyncConfiguration.InitialSyncOnStartup)
_syncHelper.SyncReceivedHandlers.Add(sync => File.WriteAllTextAsync(nextBatchFile, sync.NextBatch, cancellationToken));
- await _syncHelper.RunSyncLoopAsync(cancellationToken: cancellationToken);
+ await _syncHelper.RunSyncLoopAsync(cancellationToken: _cts.Token);
}
/// <summary>Triggered when the application host is performing a graceful shutdown.</summary>
@@ -82,19 +84,10 @@ public class InviteHandlerHostedService(
return;
}
- await _listenerTask.WaitAsync(cancellationToken);
+ await _cts.CancelAsync();
}
- public class InviteEventArgs {
- public required string RoomId { get; init; }
- public required AuthenticatedHomeserverGeneric Homeserver { get; init; }
- public required StateEventResponse MemberEvent { get; init; }
- public required SyncResponse.RoomsDataStructure.InvitedRoomDataStructure InviteData { get; init; }
- }
- public interface IInviteHandler {
- public Task HandleInviteAsync(InviteEventArgs invite);
- }
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "Configuration")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global", Justification = "Configuration")]
|