about summary refs log tree commit diff
path: root/MatrixAntiDmSpam/PolicyStore.cs
blob: f2ddd36710ed63aa2d35af47099f56baa2998d65 (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
25
26
27
28
29
30
31
32
33
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 List<Func<PolicyRuleEventContent, Task>> OnPolicyUpdated { get; } = [];

    public async Task AddPolicies(IEnumerable<StateEventResponse> events) => events.ToList().Select(AddPolicy).ToList();

    public async Task AddPolicy(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;
        }

        foreach (var callback in OnPolicyUpdated) {
            await callback((evt.TypedContent as PolicyRuleEventContent)!);
        }
    }
}