about summary refs log tree commit diff
path: root/MatrixAntiDmSpam/PolicyStore.cs
blob: 863bc927f8a41db1faf3abe053c23516c4975511 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using LibMatrix;
using LibMatrix.EventTypes.Spec.State.Policy;

namespace MatrixAntiDmSpam;

public class PolicyStore {
    public Dictionary<string, PolicyRuleEventContent> AllPolicies { get; } = [];
    public List<Func<PolicyRuleEventContent, Task>> OnPolicyUpdated { get; } = [];

    public Task AddPoliciesAsync(IEnumerable<StateEventResponse> events) => Task.WhenAll(events.Select(AddPolicyAsync).ToList());

    public async Task AddPolicyAsync(StateEventResponse evt) {
        var eventKey = $"{evt.RoomId}:{evt.Type}:{evt.StateKey}";
        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(policy);
            }
        }
    }
}