about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-03-18 11:56:27 +0100
committerRory& <root@rory.gay>2025-03-18 11:56:27 +0100
commit8313eded62554b94073e1a7364926e387d4c53be (patch)
tree5fef74db8f753fb3386f4c9307c817bace5c3efa
parentWork (diff)
downloadMatrixAntiDmSpam-8313eded62554b94073e1a7364926e387d4c53be.tar.xz
Add more stores, do osme work
m---------LibMatrix0
-rw-r--r--MatrixAntiDmSpam/AntiDmSpamConfiguration.cs1
-rw-r--r--MatrixAntiDmSpam/InviteHandler.cs4
-rw-r--r--MatrixAntiDmSpam/InviteStore.cs15
-rw-r--r--MatrixAntiDmSpam/PolicyExecutor.cs28
-rw-r--r--MatrixAntiDmSpam/PolicyListFetcher.cs16
-rw-r--r--MatrixAntiDmSpam/PolicyStore.cs5
-rw-r--r--MatrixAntiDmSpam/Program.cs6
-rw-r--r--MatrixAntiDmSpam/appsettings.Development.json9
9 files changed, 79 insertions, 5 deletions
diff --git a/LibMatrix b/LibMatrix
-Subproject ee0f3ff6ae641880c4d8b7308f02839d80c0af8
+Subproject 44bfb17b1da1bd24140da7d9dcff56fa6fce45e
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<PolicyRoomReference> PolicyLists { get; set; } public class PolicyRoomReference { + public string Name { get; set; } public string RoomId { get; set; } public List<string> 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<InviteHandler> 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<InviteHandler> 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<Func<InviteHandlerHostedService.InviteEventArgs, Task>> 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<PolicyExecutor> 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<PolicyListFetcher> logger, AntiDmSpamConfiguration config, AuthenticatedHomeserverGeneric homeserver) : IHostedService { +public class PolicyListFetcher(ILogger<PolicyListFetcher> 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<PolicyListFetcher> 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<string, UserPolicyRuleEventContent> UserPolicies { get; } = []; public Dictionary<string, ServerPolicyRuleEventContent> ServerPolicies { get; } = []; public Dictionary<string, RoomPolicyRuleEventContent> RoomPolicies { get; } = []; + public List<Func<PolicyRuleEventContent, Task>> OnPolicyUpdated { get; } = []; - public void AddPolicies(IEnumerable<StateEventResponse> events) => events.ForEach(AddPolicy); + public void AddPolicies(IEnumerable<StateEventResponse> 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<PolicyListFetcher>(); +builder.Services.AddSingleton<InviteStore>(); +builder.Services.AddSingleton<PolicyStore>(); + +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" ] + } + ] } }