about summary refs log tree commit diff
path: root/MatrixAntiDmSpam.Core/PolicyExecutor.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixAntiDmSpam.Core/PolicyExecutor.cs')
-rw-r--r--MatrixAntiDmSpam.Core/PolicyExecutor.cs27
1 files changed, 20 insertions, 7 deletions
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>(IgnoredUserListEventContent.EventId); + if (ignoreList != null) { + ignoreList.IgnoredUsers.RemoveAll((id, meta) => { + if (meta.AdditionalData?.ContainsKey(MadsIgnoreMetadataContent.EventId) ?? false) { + var metadata = meta.GetAdditionalData<JsonObject>(MadsIgnoreMetadataContent.EventId); + if (metadata?["was_user_added"]?.GetValue<bool>() ?? 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<IgnoredUserListEventContent> TryRecoverIgnoreList(IgnoredUserListEventContent content) { + + } + #endregion #region Feature: Report blocked invites