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

namespace LibMatrix.RoomTypes;

public class SpaceRoom : GenericRoom {
    private new readonly AuthenticatedHomeserverGeneric _homeserver;
    private readonly GenericRoom _room;

    public SpaceRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) : base(homeserver, roomId) {
        _homeserver = homeserver;
    }

    private static SemaphoreSlim _semaphore = new(1, 1);
    public async IAsyncEnumerable<GenericRoom> GetRoomsAsync(bool includeRemoved = false) {
        await _semaphore.WaitAsync();
        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 await _homeserver.GetRoom(stateEvent.StateKey);
        }
        _semaphore.Release();
    }
}