diff options
Diffstat (limited to 'LibMatrix/RoomTypes/SpaceRoom.cs')
-rw-r--r-- | LibMatrix/RoomTypes/SpaceRoom.cs | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/LibMatrix/RoomTypes/SpaceRoom.cs b/LibMatrix/RoomTypes/SpaceRoom.cs index a43ae82..4a8e247 100644 --- a/LibMatrix/RoomTypes/SpaceRoom.cs +++ b/LibMatrix/RoomTypes/SpaceRoom.cs @@ -1,26 +1,37 @@ using ArcaneLibs.Extensions; using LibMatrix.Homeservers; +using Microsoft.Extensions.Logging; namespace LibMatrix.RoomTypes; -public class SpaceRoom : GenericRoom { - private new readonly AuthenticatedHomeserverGeneric _homeserver; +public class SpaceRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) : GenericRoom(homeserver, roomId) { 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> GetChildrenAsync(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 _homeserver.GetRoom(stateEvent.StateKey); + 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; } - // _semaphore.Release(); + + var resp = await SendStateEventAsync("m.space.child", room.RoomId, new { + via = memberCountByHs + .OrderByDescending(x => x.Value) + .Select(x => x.Key) + .Take(10) + }); + return resp; } } |