about summary refs log tree commit diff
path: root/LibMatrix/RoomTypes/PolicyRoom.cs
blob: c6eec63473b94288e6a16322c334a3c052d2556b (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections.Frozen;
using LibMatrix.EventTypes;
using LibMatrix.EventTypes.Spec.State.Policy;
using LibMatrix.Homeservers;

namespace LibMatrix.RoomTypes;

public class PolicyRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) : GenericRoom(homeserver, roomId) {
    public const string TypeName = "support.feline.policy.lists.msc.v1";
    
    public static readonly FrozenSet<string> UserPolicyEventTypes = EventContent.GetMatchingEventTypes<UserPolicyRuleEventContent>().ToFrozenSet();
    public static readonly FrozenSet<string> ServerPolicyEventTypes = EventContent.GetMatchingEventTypes<ServerPolicyRuleEventContent>().ToFrozenSet();
    public static readonly FrozenSet<string> RoomPolicyEventTypes = EventContent.GetMatchingEventTypes<RoomPolicyRuleEventContent>().ToFrozenSet();
    public static readonly FrozenSet<string> SpecPolicyEventTypes = [..UserPolicyEventTypes, ..ServerPolicyEventTypes, ..RoomPolicyEventTypes];

    public async IAsyncEnumerable<StateEventResponse> GetPoliciesAsync() {
        var fullRoomState = GetFullStateAsync();
        await foreach (var eventResponse in fullRoomState) {
            if (SpecPolicyEventTypes.Contains(eventResponse!.Type)) {
                yield return eventResponse;
            }
        }
    }

    public async IAsyncEnumerable<StateEventResponse> GetUserPoliciesAsync() {
        var fullRoomState = GetPoliciesAsync();
        await foreach (var eventResponse in fullRoomState) {
            if (UserPolicyEventTypes.Contains(eventResponse!.Type)) {
                yield return eventResponse;
            }
        }
    }

    public async IAsyncEnumerable<StateEventResponse> GetServerPoliciesAsync() {
        var fullRoomState = GetPoliciesAsync();
        await foreach (var eventResponse in fullRoomState) {
            if (ServerPolicyEventTypes.Contains(eventResponse!.Type)) {
                yield return eventResponse;
            }
        }
    }

    public async IAsyncEnumerable<StateEventResponse> GetRoomPoliciesAsync() {
        var fullRoomState = GetPoliciesAsync();
        await foreach (var eventResponse in fullRoomState) {
            if (RoomPolicyEventTypes.Contains(eventResponse!.Type)) {
                yield return eventResponse;
            }
        }
    }
}