From 4f6f25b093ac231b8b099d19fc0d26fe8dd69049 Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 8 May 2025 23:45:53 +0200 Subject: Switch to msc4222 --- LibMatrix | 2 +- MatrixAntiDmSpam.Core/AntiDmSpamConfiguration.cs | 7 +++--- MatrixAntiDmSpam.Core/PolicyExecutor.cs | 8 ++++++- MatrixAntiDmSpam.Core/PolicyListFetcher.cs | 29 ++++++++++-------------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/LibMatrix b/LibMatrix index 2fde2d5..f8f1fef 160000 --- a/LibMatrix +++ b/LibMatrix @@ -1 +1 @@ -Subproject commit 2fde2d5f961eabf3167280ba55786cdb6b38f2c0 +Subproject commit f8f1feff2bcbfb2bd5fdb498b2e8572ecba37b2c 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 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 @@ -102,6 +102,12 @@ public class PolicyExecutor( await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreListContent); } +#endregion + +#region Feature: Report blocked invites + + + #endregion #region Feature: Reject invites @@ -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 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 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 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(); -- cgit 1.5.1