about summary refs log tree commit diff
path: root/MatrixRoomUtils.LibDMSpace/DMSpaceRoom.cs
blob: 1cf7064412ca1aa71bbadbf00d48d270fb8ad364 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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<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
}