about summary refs log tree commit diff
path: root/LibMatrix/RoomTypes/SpaceRoom.cs
blob: da9fcf8e581de282cb11d6e1db3518a538dfd47d (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
using ArcaneLibs.Extensions;
using LibMatrix.Homeservers;

namespace LibMatrix.RoomTypes;

public class SpaceRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) : GenericRoom(homeserver, roomId) {
    private readonly GenericRoom _room;

    public async IAsyncEnumerable<GenericRoom> GetChildrenAsync(bool includeRemoved = false) {
        var rooms = new List<GenericRoom>();
        var state = GetFullStateAsync();
        await foreach (var stateEvent in state) {
            if (stateEvent!.Type != "m.space.child") continue;
            if (stateEvent.RawContent!.ToJson() != "{}" || includeRemoved)
                yield return Homeserver.GetRoom(stateEvent.StateKey);
        }
    }

    public async Task<EventIdResponse> AddChildAsync(GenericRoom room) {
        var members = room.GetMembersAsync(true);
        Dictionary<string, int> memberCountByHs = new();
        await foreach (var member in members) {
            var server = member.StateKey.Split(':')[1];
            if (memberCountByHs.ContainsKey(server)) memberCountByHs[server]++;
            else memberCountByHs[server] = 1;
        }

        var resp = await SendStateEventAsync("m.space.child", room.RoomId, new {
            via = memberCountByHs
                .OrderByDescending(x => x.Value)
                .Select(x => x.Key)
                .Take(10)
        });
        return resp;
    }
}