From 2d83d90df1723366f16ec5fd78302a848e1a76d7 Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 9 May 2025 07:10:24 +0200 Subject: try to fix invalid ignore list entries --- LibMatrix | 2 +- MatrixAntiDmSpam.Core/PolicyExecutor.cs | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/LibMatrix b/LibMatrix index f8f1fef..51ff4a3 160000 --- a/LibMatrix +++ b/LibMatrix @@ -1 +1 @@ -Subproject commit f8f1feff2bcbfb2bd5fdb498b2e8572ecba37b2c +Subproject commit 51ff4a3b95ab38dd46c88f4e5482bf78449bb88c diff --git a/MatrixAntiDmSpam.Core/PolicyExecutor.cs b/MatrixAntiDmSpam.Core/PolicyExecutor.cs index 4d1310a..40432b8 100644 --- a/MatrixAntiDmSpam.Core/PolicyExecutor.cs +++ b/MatrixAntiDmSpam.Core/PolicyExecutor.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Text.Json.Nodes; using ArcaneLibs.Attributes; using ArcaneLibs.Extensions; using LibMatrix; @@ -22,19 +23,27 @@ public class PolicyExecutor( AuthenticatedHomeserverGeneric homeserver) : IHostedService { private readonly GenericRoom? _logRoom = string.IsNullOrWhiteSpace(config.LogRoom) ? null : homeserver.GetRoom(config.LogRoom); - public Task StartAsync(CancellationToken cancellationToken) { + public async Task StartAsync(CancellationToken cancellationToken) { roomInviteHandler.OnInviteReceived.Add(CheckPoliciesAgainstInvite); policyStore.OnPolicyAdded.Add(CheckPolicyAgainstOutstandingInvites); if (config.IgnoreBannedUsers) { + var ignoreList = await homeserver.GetAccountDataOrNullAsync(IgnoredUserListEventContent.EventId); + if (ignoreList != null) { + ignoreList.IgnoredUsers.RemoveAll((id, meta) => { + if (meta.AdditionalData?.ContainsKey(MadsIgnoreMetadataContent.EventId) ?? false) { + var metadata = meta.GetAdditionalData(MadsIgnoreMetadataContent.EventId); + if (metadata?["was_user_added"]?.GetValue() ?? false) { + return true; + } + } + return false; + }); + } policyStore.OnPoliciesChanged.Add(UpdateIgnoreList); } - - return Task.CompletedTask; } - public Task StopAsync(CancellationToken cancellationToken) { - return Task.CompletedTask; - } + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; #region Feature: Manage ignore list @@ -52,7 +61,7 @@ public class PolicyExecutor( var policyEventReference = new MadsIgnoreMetadataContent.PolicyEventReference() { Type = newEvent.Type, - RoomId = newEvent.RoomId, + RoomId = newEvent.RoomId ?? throw new InvalidOperationException("RoomId is null"), StateKey = newEvent.StateKey! }; @@ -102,6 +111,10 @@ public class PolicyExecutor( await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreListContent); } + private async Task TryRecoverIgnoreList(IgnoredUserListEventContent content) { + + } + #endregion #region Feature: Report blocked invites -- cgit 1.5.1