diff options
author | Rory& <root@rory.gay> | 2024-01-24 02:31:56 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-24 17:05:25 +0100 |
commit | 03313562d21d5db9bf6a14ebbeab80e06c883d3a (patch) | |
tree | e000546a2ee8e6a886a7ed9fd01ad674178fb7cb /MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs | |
parent | Make RMU installable (diff) | |
download | MatrixUtils-03313562d21d5db9bf6a14ebbeab80e06c883d3a.tar.xz |
MRU->RMU, fixes, cleanup
Diffstat (limited to 'MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs')
-rw-r--r-- | MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs b/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs deleted file mode 100644 index 1cf7064..0000000 --- a/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs +++ /dev/null @@ -1,83 +0,0 @@ -using ArcaneLibs.Extensions; -using LibMatrix; -using LibMatrix.Homeservers; -using LibMatrix.RoomTypes; -using MatrixRoomUtils.LibDMSpace.StateEvents; - -namespace MatrixRoomUtils.LibDMSpace; - -public class DMSpaceRoom(AuthenticatedHomeserverGeneric homeserver, string roomId) : SpaceRoom(homeserver, roomId) { - private readonly GenericRoom _room; - - public async Task<DMSpaceInfo?> GetDmSpaceInfo() { - return await GetStateOrNullAsync<DMSpaceInfo>(DMSpaceInfo.EventId); - } - - 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.GetMembersEnumerableAsync(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; - } - - public async Task ImportNativeDMs() { - var dmSpaceInfo = await GetDmSpaceInfo(); - if (dmSpaceInfo is null) throw new NullReferenceException("DM Space is not configured!"); - if (dmSpaceInfo.LayerByUser) - await ImportNativeDMsIntoLayers(); - else await ImportNativeDMsWithoutLayers(); - } - - #region Import Native DMs - - private async Task ImportNativeDMsWithoutLayers() { - var mdirect = await homeserver.GetAccountDataAsync<Dictionary<string, List<string>>>("m.direct"); - foreach (var (userId, dmRooms) in mdirect) { - foreach (var roomid in dmRooms) { - var dri = new DMRoomInfo() { - RemoteUsers = new() { - userId - } - }; - // Add all DM room members - var members = homeserver.GetRoom(roomid).GetMembersEnumerableAsync(); - await foreach (var member in members) - if (member.StateKey != userId) - dri.RemoteUsers.Add(member.StateKey); - // Remove members of DM space - members = GetMembersEnumerableAsync(); - await foreach (var member in members) - if (dri.RemoteUsers.Contains(member.StateKey)) - dri.RemoteUsers.Remove(member.StateKey); - await SendStateEventAsync(DMRoomInfo.EventId, roomid, dri); - } - } - } - - private async Task ImportNativeDMsIntoLayers() { - var mdirect = await homeserver.GetAccountDataAsync<Dictionary<string, List<string>>>("m.direct"); - } - - #endregion -} |