From 89315fa530e1f21e2e50d94f955693b9413c98fe Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Wed, 25 Oct 2023 14:00:02 +0200 Subject: New things --- MatrixRoomUtils.LibDMSpace/DMSpaceConfiguration.cs | 12 ++++ MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs | 83 ++++++++++++++++++++++ .../MatrixRoomUtils.LibDMSpace.csproj | 16 +++++ .../StateEvents/DMRoomInfo.cs | 14 ++++ .../StateEvents/DMSpaceInfo.cs | 14 ++++ 5 files changed, 139 insertions(+) create mode 100644 MatrixRoomUtils.LibDMSpace/DMSpaceConfiguration.cs create mode 100644 MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs create mode 100644 MatrixRoomUtils.LibDMSpace/MatrixRoomUtils.LibDMSpace.csproj create mode 100644 MatrixRoomUtils.LibDMSpace/StateEvents/DMRoomInfo.cs create mode 100644 MatrixRoomUtils.LibDMSpace/StateEvents/DMSpaceInfo.cs (limited to 'MatrixRoomUtils.LibDMSpace') diff --git a/MatrixRoomUtils.LibDMSpace/DMSpaceConfiguration.cs b/MatrixRoomUtils.LibDMSpace/DMSpaceConfiguration.cs new file mode 100644 index 0000000..4085367 --- /dev/null +++ b/MatrixRoomUtils.LibDMSpace/DMSpaceConfiguration.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace MatrixRoomUtils.LibDMSpace; + +//gay.rory.dm_space +public class DMSpaceConfiguration { + public const string EventId = "gay.rory.dm_space"; + + [JsonPropertyName("dm_space_id")] + public string? DMSpaceId { get; set; } + +} \ No newline at end of file diff --git a/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs b/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs new file mode 100644 index 0000000..247ace8 --- /dev/null +++ b/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs @@ -0,0 +1,83 @@ +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 GetDmSpaceInfo() { + return await GetStateOrNullAsync(DMSpaceInfo.EventId); + } + + public async IAsyncEnumerable GetChildrenAsync(bool includeRemoved = false) { + var rooms = new List(); + 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 AddChildAsync(GenericRoom room) { + var members = room.GetMembersAsync(true); + Dictionary 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>>("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).GetMembersAsync(); + await foreach (var member in members) + if (member.StateKey != userId) + dri.RemoteUsers.Add(member.StateKey); + // Remove members of DM space + members = GetMembersAsync(); + 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>>("m.direct"); + } + +#endregion +} \ No newline at end of file diff --git a/MatrixRoomUtils.LibDMSpace/MatrixRoomUtils.LibDMSpace.csproj b/MatrixRoomUtils.LibDMSpace/MatrixRoomUtils.LibDMSpace.csproj new file mode 100644 index 0000000..70b4ffc --- /dev/null +++ b/MatrixRoomUtils.LibDMSpace/MatrixRoomUtils.LibDMSpace.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + enable + enable + preview + + + + + + + + + diff --git a/MatrixRoomUtils.LibDMSpace/StateEvents/DMRoomInfo.cs b/MatrixRoomUtils.LibDMSpace/StateEvents/DMRoomInfo.cs new file mode 100644 index 0000000..b88f06a --- /dev/null +++ b/MatrixRoomUtils.LibDMSpace/StateEvents/DMRoomInfo.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes; +using LibMatrix.Interfaces; + +namespace MatrixRoomUtils.LibDMSpace.StateEvents; + +[MatrixEvent(EventName = EventId)] +public class DMRoomInfo : EventContent { + public const string EventId = "gay.rory.dm_room_info"; + [JsonPropertyName("remote_users")] + public List RemoteUsers { get; set; } + + +} \ No newline at end of file diff --git a/MatrixRoomUtils.LibDMSpace/StateEvents/DMSpaceInfo.cs b/MatrixRoomUtils.LibDMSpace/StateEvents/DMSpaceInfo.cs new file mode 100644 index 0000000..7824324 --- /dev/null +++ b/MatrixRoomUtils.LibDMSpace/StateEvents/DMSpaceInfo.cs @@ -0,0 +1,14 @@ +using System.Text.Json.Serialization; +using LibMatrix.EventTypes; +using LibMatrix.Interfaces; + +namespace MatrixRoomUtils.LibDMSpace.StateEvents; + +[MatrixEvent(EventName = EventId)] +public class DMSpaceInfo : EventContent { + public const string EventId = "gay.rory.dm_space_info"; + + [JsonPropertyName("is_layered")] + public bool LayerByUser { get; set; } + +} \ No newline at end of file -- cgit 1.5.1