about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-05-24 21:17:17 +0200
committerRory& <root@rory.gay>2025-05-24 21:17:17 +0200
commit56a9125672968b7c11b6d138fb3ca1851fe7e90e (patch)
treef918e9075840e9d483a7e23427f4b0a9b8f68c38
parentImplement hook that runs *before* rejecting an invite (diff)
downloadMatrixAntiDmSpam-master.tar.xz
Make rejecting invites not depend on having a log room configured HEAD master
-rw-r--r--MatrixAntiDmSpam.Core/InviteManager.cs11
-rw-r--r--MatrixAntiDmSpam.Core/RoomInviteHandler.cs9
2 files changed, 9 insertions, 11 deletions
diff --git a/MatrixAntiDmSpam.Core/InviteManager.cs b/MatrixAntiDmSpam.Core/InviteManager.cs

index e8a5dc1..a48cea2 100644 --- a/MatrixAntiDmSpam.Core/InviteManager.cs +++ b/MatrixAntiDmSpam.Core/InviteManager.cs
@@ -23,7 +23,6 @@ public class InviteManager( public List<Func<RoomInviteContext, StateEventResponse, Task>> OnInviteRejected { get; } = []; public List<Func<RoomInviteContext, StateEventResponse, Task>> OnBeforeInviteRejected { get; } = []; - public async Task StartAsync(CancellationToken cancellationToken) { roomInviteHandler.OnInviteReceived.Add(CheckPoliciesAgainstInvite); policyStore.OnPolicyAdded.Add(CheckPolicyAgainstOutstandingInvites); @@ -90,23 +89,25 @@ public class InviteManager( var policyRoom = config.PolicyLists.First(x => x.RoomId == policyEvent.RoomId); logger.LogWarning("[{}] Rejecting invite to {}, matching {} in {}: {}", homeserver.WhoAmI.UserId, invite.RoomId, policy.GetType().GetFriendlyName(), policyRoom.Name, policy.ToJson(ignoreNull: true)); - + foreach (var callback in OnBeforeInviteRejected) { await callback(invite, policyEvent); } if (_logRoom is not null) { var roomName = await invite.TryGetRoomNameAsync(); - - await roomInviteHandler.RejectInvite(invite, new MessageBuilder() + var logMessage = new MessageBuilder() .WithColoredBody("#FF0000", cb => cb.WithBody("Rejecting invite to ").WithMention(invite.RoomId, roomName) .WithBody($", matching {policy.GetType().GetFriendlyName().ToLowerInvariant()} in {policyRoom.Name}.") .WithNewline()) .WithCollapsibleSection("Policy JSON", cb => cb.WithCodeBlock(policy.ToJson(ignoreNull: true), "json")) - ); + .Build(); + await _logRoom.SendMessageEventAsync(logMessage); } + await roomInviteHandler.RejectInvite(invite); + foreach (var callback in OnInviteRejected) { await callback(invite, policyEvent); } diff --git a/MatrixAntiDmSpam.Core/RoomInviteHandler.cs b/MatrixAntiDmSpam.Core/RoomInviteHandler.cs
index 41f4011..d4d97d6 100644 --- a/MatrixAntiDmSpam.Core/RoomInviteHandler.cs +++ b/MatrixAntiDmSpam.Core/RoomInviteHandler.cs
@@ -55,9 +55,9 @@ public class RoomInviteHandler(ILogger<RoomInviteHandler> logger, AntiDmSpamConf var message = new MessageBuilder() .WithBody("Received invite to ").WithMention(invite.RoomId, await roomNameTask).WithBody(" from ").WithMention(invite.MemberEvent.Sender!, await inviterNameTask) .Build(); - + message.AdditionalData!["gay.rory.invite_logger.invite_data_uri"] = JsonDocument.Parse($"\"{await inviteDataFileUriTask}\"").RootElement; - + await LogRoom.SendMessageEventAsync(message); if (config.LogInviteDataAsFile) { @@ -71,10 +71,7 @@ public class RoomInviteHandler(ILogger<RoomInviteHandler> logger, AntiDmSpamConf } } - public async Task RejectInvite(RoomInviteContext invite, MessageBuilder reason) { - if (LogRoom is not null) - _ = LogRoom.SendMessageEventAsync(reason.Build()); - + public async Task RejectInvite(RoomInviteContext invite) { try { await invite.Homeserver.GetRoom(invite.RoomId).LeaveAsync(); }