From 8313eded62554b94073e1a7364926e387d4c53be Mon Sep 17 00:00:00 2001 From: Rory& Date: Tue, 18 Mar 2025 11:56:27 +0100 Subject: Add more stores, do osme work --- LibMatrix | 2 +- MatrixAntiDmSpam/AntiDmSpamConfiguration.cs | 1 + MatrixAntiDmSpam/InviteHandler.cs | 4 +++- MatrixAntiDmSpam/InviteStore.cs | 15 ++++++++++++++ MatrixAntiDmSpam/PolicyExecutor.cs | 28 +++++++++++++++++++++++++++ MatrixAntiDmSpam/PolicyListFetcher.cs | 16 ++++++++++++++- MatrixAntiDmSpam/PolicyStore.cs | 5 +++-- MatrixAntiDmSpam/Program.cs | 6 ++++++ MatrixAntiDmSpam/appsettings.Development.json | 9 ++++++++- 9 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 MatrixAntiDmSpam/InviteStore.cs create mode 100644 MatrixAntiDmSpam/PolicyExecutor.cs diff --git a/LibMatrix b/LibMatrix index ee0f3ff..44bfb17 160000 --- a/LibMatrix +++ b/LibMatrix @@ -1 +1 @@ -Subproject commit ee0f3ff6ae641880c4d8b7308f02839d80c0af88 +Subproject commit 44bfb17b1da1bd24140da7d9dcff56fa6fce45e4 diff --git a/MatrixAntiDmSpam/AntiDmSpamConfiguration.cs b/MatrixAntiDmSpam/AntiDmSpamConfiguration.cs index 0e10a91..0d90b3e 100644 --- a/MatrixAntiDmSpam/AntiDmSpamConfiguration.cs +++ b/MatrixAntiDmSpam/AntiDmSpamConfiguration.cs @@ -8,6 +8,7 @@ public class AntiDmSpamConfiguration { public List PolicyLists { get; set; } public class PolicyRoomReference { + public string Name { get; set; } public string RoomId { get; set; } public List Vias { get; set; } } diff --git a/MatrixAntiDmSpam/InviteHandler.cs b/MatrixAntiDmSpam/InviteHandler.cs index cfa04dc..c4831ee 100644 --- a/MatrixAntiDmSpam/InviteHandler.cs +++ b/MatrixAntiDmSpam/InviteHandler.cs @@ -9,7 +9,7 @@ namespace MatrixAntiDmSpam; public class InviteHandler(ILogger logger, AntiDmSpamConfiguration config) : InviteHandlerHostedService.IInviteHandler { public async Task HandleInviteAsync(InviteHandlerHostedService.InviteEventArgs invite) { - logger.LogInformation("Received invite to room {}", invite.RoomId); + // logger.LogInformation("Received invite to room {}", invite.RoomId); await LogInvite(invite); } @@ -86,7 +86,9 @@ public class InviteHandler(ILogger logger, AntiDmSpamConfiguratio // try get room name via public previews try { +#pragma warning disable CS0618 // Type or member is obsolete name = await invite.Homeserver.GetRoom(invite.RoomId).GetNameOrFallbackAsync(); +#pragma warning restore CS0618 // Type or member is obsolete if (name != invite.RoomId && !string.IsNullOrWhiteSpace(name)) return name; } diff --git a/MatrixAntiDmSpam/InviteStore.cs b/MatrixAntiDmSpam/InviteStore.cs new file mode 100644 index 0000000..abfa49c --- /dev/null +++ b/MatrixAntiDmSpam/InviteStore.cs @@ -0,0 +1,15 @@ +using LibMatrix; +using System.Collections; +using LibMatrix.EventTypes.Spec.State.Policy; +using LibMatrix.Responses; +using LibMatrix.Utilities.Bot.Services; + +namespace MatrixAntiDmSpam; + +public class InviteStore { + public void AddInvite(SyncResponse.RoomsDataStructure.InvitedRoomDataStructure invite) { + + } + + public List> OnInviteReceived { get; set; } = []; +} \ No newline at end of file diff --git a/MatrixAntiDmSpam/PolicyExecutor.cs b/MatrixAntiDmSpam/PolicyExecutor.cs new file mode 100644 index 0000000..2f93f11 --- /dev/null +++ b/MatrixAntiDmSpam/PolicyExecutor.cs @@ -0,0 +1,28 @@ +using LibMatrix.EventTypes.Spec.State.Policy; +using LibMatrix.Homeservers; +using LibMatrix.Utilities.Bot.Services; + +namespace MatrixAntiDmSpam; + +public class PolicyExecutor(ILogger logger, InviteStore inviteStore, PolicyStore policyStore, AuthenticatedHomeserverGeneric homeserver) : IHostedService { + public async Task StartAsync(CancellationToken cancellationToken) { + inviteStore.OnInviteReceived.Add(CheckPoliciesAgainstInvite); + policyStore.OnPolicyUpdated.Add(CheckPolicyAgainstInvites); + } + + public async Task StopAsync(CancellationToken cancellationToken) { + + } + + public async Task CheckPoliciesAgainstInvite(InviteHandlerHostedService.InviteEventArgs inviteEventArgs) { + if(policyStore.RoomPolicies.Any(x=>x.Value.Entity == inviteEventArgs.RoomId)) + { + + } + + } + + public async Task CheckPolicyAgainstInvites(PolicyRuleEventContent policy) { + + } +} \ No newline at end of file diff --git a/MatrixAntiDmSpam/PolicyListFetcher.cs b/MatrixAntiDmSpam/PolicyListFetcher.cs index ee80120..a068f33 100644 --- a/MatrixAntiDmSpam/PolicyListFetcher.cs +++ b/MatrixAntiDmSpam/PolicyListFetcher.cs @@ -1,11 +1,14 @@ +using System.Diagnostics; using LibMatrix.Homeservers; +using LibMatrix.RoomTypes; namespace MatrixAntiDmSpam; -public class PolicyListFetcher(ILogger logger, AntiDmSpamConfiguration config, AuthenticatedHomeserverGeneric homeserver) : IHostedService { +public class PolicyListFetcher(ILogger logger, AntiDmSpamConfiguration config, AuthenticatedHomeserverGeneric homeserver, PolicyStore policyStore) : IHostedService { public async Task StartAsync(CancellationToken cancellationToken) { logger.LogInformation("Starting policy list fetcher"); await EnsurePolicyListsJoined(); + await LoadPolicyLists(); } public async Task StopAsync(CancellationToken cancellationToken) { @@ -22,4 +25,15 @@ public class PolicyListFetcher(ILogger logger, AntiDmSpamConf await homeserver.GetRoom(room.RoomId).JoinAsync(room.Vias); } } + + private async Task LoadPolicyLists() { + foreach (var room in config.PolicyLists) { + var sw = Stopwatch.StartNew(); + await LoadPolicyList(homeserver.GetRoom(room.RoomId).AsPolicyRoom()); + logger.LogInformation("Loaded policy list {} in {}", room.RoomId, sw.Elapsed); + } + } + private async Task LoadPolicyList(PolicyRoom room) { + policyStore.AddPolicies(room.GetPoliciesAsync().ToBlockingEnumerable()); + } } \ No newline at end of file diff --git a/MatrixAntiDmSpam/PolicyStore.cs b/MatrixAntiDmSpam/PolicyStore.cs index 1aa78d4..d16f8df 100644 --- a/MatrixAntiDmSpam/PolicyStore.cs +++ b/MatrixAntiDmSpam/PolicyStore.cs @@ -8,12 +8,13 @@ public class PolicyStore { public Dictionary UserPolicies { get; } = []; public Dictionary ServerPolicies { get; } = []; public Dictionary RoomPolicies { get; } = []; + public List> OnPolicyUpdated { get; } = []; - public void AddPolicies(IEnumerable events) => events.ForEach(AddPolicy); + public void AddPolicies(IEnumerable events) => events.ToList().ForEach(AddPolicy); public void AddPolicy(StateEventResponse evt) { var eventKey = $"{evt.RoomId}:{evt.Type}:{evt.StateKey}"; - UserPolicies.ToList().ForEach(x => Console.WriteLine(x.Key)); + // UserPolicies.ToList().ForEach(x => Console.WriteLine(x.Key)); switch (evt.TypedContent) { case UserPolicyRuleEventContent userPolicy: UserPolicies[eventKey] = userPolicy; diff --git a/MatrixAntiDmSpam/Program.cs b/MatrixAntiDmSpam/Program.cs index 39d8273..f813eab 100644 --- a/MatrixAntiDmSpam/Program.cs +++ b/MatrixAntiDmSpam/Program.cs @@ -1,3 +1,4 @@ +using LibMatrix.Extensions; using LibMatrix.Services; using LibMatrix.Utilities.Bot; using MatrixAntiDmSpam; @@ -11,5 +12,10 @@ builder.Services.AddRoryLibMatrixServices() builder.Services.AddHostedService(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +MatrixHttpClient.LogRequests = false; + var host = builder.Build(); host.Run(); \ No newline at end of file diff --git a/MatrixAntiDmSpam/appsettings.Development.json b/MatrixAntiDmSpam/appsettings.Development.json index 45717aa..958cc7a 100644 --- a/MatrixAntiDmSpam/appsettings.Development.json +++ b/MatrixAntiDmSpam/appsettings.Development.json @@ -11,6 +11,13 @@ }, "AntiDmSpam": { "LogRoom": "!GrLSwdAkdrvfMrRYKR:rory.gay", - "LogInviteDataAsFile": true + "LogInviteDataAsFile": true, + "PolicyLists": [ + { + "Name": "Community Moderation Effort", + "RoomId": "!fTjMjIzNKEsFlUIiru:neko.dev", + "Vias": [ "rory.gay" ] + } + ] } } -- cgit 1.5.1