about summary refs log tree commit diff
path: root/MatrixAntiDmSpam/PolicyStore.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MatrixAntiDmSpam/PolicyStore.cs')
-rw-r--r--MatrixAntiDmSpam/PolicyStore.cs29
1 files changed, 10 insertions, 19 deletions
diff --git a/MatrixAntiDmSpam/PolicyStore.cs b/MatrixAntiDmSpam/PolicyStore.cs

index f2ddd36..863bc92 100644 --- a/MatrixAntiDmSpam/PolicyStore.cs +++ b/MatrixAntiDmSpam/PolicyStore.cs
@@ -1,33 +1,24 @@ using LibMatrix; -using System.Collections; using LibMatrix.EventTypes.Spec.State.Policy; namespace MatrixAntiDmSpam; public class PolicyStore { - public Dictionary<string, UserPolicyRuleEventContent> UserPolicies { get; } = []; - public Dictionary<string, ServerPolicyRuleEventContent> ServerPolicies { get; } = []; - public Dictionary<string, RoomPolicyRuleEventContent> RoomPolicies { get; } = []; + public Dictionary<string, PolicyRuleEventContent> AllPolicies { get; } = []; public List<Func<PolicyRuleEventContent, Task>> OnPolicyUpdated { get; } = []; - public async Task AddPolicies(IEnumerable<StateEventResponse> events) => events.ToList().Select(AddPolicy).ToList(); + public Task AddPoliciesAsync(IEnumerable<StateEventResponse> events) => Task.WhenAll(events.Select(AddPolicyAsync).ToList()); - public async Task AddPolicy(StateEventResponse evt) { + public async Task AddPolicyAsync(StateEventResponse evt) { var eventKey = $"{evt.RoomId}:{evt.Type}:{evt.StateKey}"; - switch (evt.TypedContent) { - case UserPolicyRuleEventContent userPolicy: - UserPolicies[eventKey] = userPolicy; - break; - case ServerPolicyRuleEventContent serverPolicy: - ServerPolicies[eventKey] = serverPolicy; - break; - case RoomPolicyRuleEventContent roomPolicy: - RoomPolicies[eventKey] = roomPolicy; - break; - } + if (evt.TypedContent is PolicyRuleEventContent policy) { + if (policy.Recommendation == "m.ban") + AllPolicies[eventKey] = policy; + else AllPolicies.Remove(eventKey); - foreach (var callback in OnPolicyUpdated) { - await callback((evt.TypedContent as PolicyRuleEventContent)!); + foreach (var callback in OnPolicyUpdated) { + await callback(policy); + } } } } \ No newline at end of file