about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-08 23:45:53 +0200
committerRory& <root@rory.gay>2025-05-08 23:45:53 +0200
commit4f6f25b093ac231b8b099d19fc0d26fe8dd69049 (patch)
tree268ec6575a0be0067e4ff0280aff11a3146c9f26
parentFix install-user-units.sh (diff)
downloadMatrixAntiDmSpam-4f6f25b093ac231b8b099d19fc0d26fe8dd69049.tar.xz
Switch to msc4222
m---------LibMatrix0
-rw-r--r--MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs7
-rw-r--r--MatrixAntiDmSpam.Core/PolicyExecutor.cs8
-rw-r--r--MatrixAntiDmSpam.Core/PolicyListFetcher.cs29
4 files changed, 23 insertions, 21 deletions
diff --git a/LibMatrix b/LibMatrix
-Subproject 2fde2d5f961eabf3167280ba55786cdb6b38f2c
+Subproject f8f1feff2bcbfb2bd5fdb498b2e8572ecba37b2
diff --git a/MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs b/MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs

index bd06db3..1ce901e 100644 --- a/MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs +++ b/MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs
@@ -5,9 +5,10 @@ namespace MatrixAntiDmSpam.Core; public class AntiDmSpamConfiguration { public AntiDmSpamConfiguration(IConfiguration config) => config.GetRequiredSection("AntiDmSpam").Bind(this); public string? LogRoom { get; set; } - public bool LogInviteDataAsFile { get; set; } - public bool IgnoreBannedUsers { get; set; } - public bool ReportBlockedInvites { get; set; } + public bool LogInviteDataAsFile { get; set; } = true; + public bool IgnoreBannedUsers { get; set; } = false; + public bool ReportBlockedInvites { get; set; } = false; + public TimeSpan MinimumSyncTime { get; set; } = TimeSpan.Zero; public required List<PolicyRoomReference> PolicyLists { get; set; } diff --git a/MatrixAntiDmSpam.Core/PolicyExecutor.cs b/MatrixAntiDmSpam.Core/PolicyExecutor.cs
index 23d70a8..4d1310a 100644 --- a/MatrixAntiDmSpam.Core/PolicyExecutor.cs +++ b/MatrixAntiDmSpam.Core/PolicyExecutor.cs
@@ -104,6 +104,12 @@ public class PolicyExecutor( #endregion +#region Feature: Report blocked invites + + + +#endregion + #region Feature: Reject invites private Task CheckPoliciesAgainstInvite(RoomInviteContext invite) { @@ -153,7 +159,7 @@ public class PolicyExecutor( } if (!policyMatches) return null; - logger.LogWarning("Rejecting invite to {}, matching {} {}", invite.RoomId, policy.GetType().GetFriendlyName(), policy.ToJson(ignoreNull: true)); + logger.LogWarning("[{}] Rejecting invite to {}, matching {} {}", homeserver.WhoAmI.UserId, invite.RoomId, policy.GetType().GetFriendlyName(), policy.ToJson(ignoreNull: true)); return Task.Run(async () => { if (_logRoom is not null) { diff --git a/MatrixAntiDmSpam.Core/PolicyListFetcher.cs b/MatrixAntiDmSpam.Core/PolicyListFetcher.cs
index b8f5518..31b8835 100644 --- a/MatrixAntiDmSpam.Core/PolicyListFetcher.cs +++ b/MatrixAntiDmSpam.Core/PolicyListFetcher.cs
@@ -23,7 +23,11 @@ public class PolicyListFetcher(ILogger<PolicyListFetcher> logger, AntiDmSpamConf logger.LogInformation("Starting policy list fetcher"); await EnsurePolicyListsJoined(); - _ = SyncPolicyLists(); + _ = SyncPolicyLists().ContinueWith(x => { + if (x.IsFaulted) { + logger.LogError(x.Exception, "Error in policy list fetcher"); + } + }, TaskContinuationOptions.OnlyOnFaulted); } public async Task StopAsync(CancellationToken cancellationToken) { @@ -45,8 +49,8 @@ public class PolicyListFetcher(ILogger<PolicyListFetcher> logger, AntiDmSpamConf private async Task SyncPolicyLists() { var syncHelper = new SyncHelper(homeserver, logger) { Timeout = 30_000, - MinimumDelay = TimeSpan.FromSeconds(3), - FilterId = (await homeserver.UploadFilterAsync(new SyncFilter() { + MinimumDelay = config.MinimumSyncTime, + FilterId = (await homeserver.UploadFilterAsync(new SyncFilter { AccountData = SyncFilter.EventFilter.Empty, Presence = SyncFilter.EventFilter.Empty, Room = new SyncFilter.RoomFilter { @@ -57,26 +61,17 @@ public class PolicyListFetcher(ILogger<PolicyListFetcher> logger, AntiDmSpamConf Rooms = config.PolicyLists.Select(x => x.RoomId).ToList(), IncludeLeave = false } - })).FilterId + })).FilterId, + UseMsc4222StateAfter = true }; await foreach (var syncResponse in syncHelper.EnumerateSyncAsync(_cts.Token)) { if (_cts.IsCancellationRequested) return; if (syncResponse is { Rooms.Join.Count: > 0 }) { - foreach (var (roomId, data) in syncResponse.Rooms.Join) { - if (!config.PolicyLists.Any(x => x.RoomId == roomId)) continue; - - if (data.State?.Events is null) - data.State = new() { Events = [] }; - - if (data.Timeline is { Events.Count: > 0 }) { - data.State.Events.AddRange(data.Timeline.Events); - data.Timeline = null; - } - } - - var newPolicies = syncResponse.Rooms!.Join!.SelectMany(x => x.Value.State!.Events!) + var newPolicies = syncResponse.Rooms.Join + .Where(x => config.PolicyLists.Any(y => y.RoomId == x.Key)) + .SelectMany(x => x.Value.StateAfter?.Events ?? []) .Where(x => PolicyRoom.SpecPolicyEventTypes.Contains(x.Type)) .ToList();