about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs
blob: 65626869c01fbb42219fcaabe5dc9d35e72bf4b9 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using ArcaneLibs.Extensions;
using LibMatrix.Filters;
using LibMatrix.Responses.Admin;

namespace LibMatrix.Homeservers;

public class AuthenticatedHomeserverSynapse : AuthenticatedHomeserverGeneric {
    public readonly SynapseAdminApi Admin;
    public class SynapseAdminApi(AuthenticatedHomeserverSynapse authenticatedHomeserver) {
        public async IAsyncEnumerable<AdminRoomListingResult.AdminRoomListingResultRoom> SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", string? searchTerm = null, LocalRoomQueryFilter? localFilter = null) {
            AdminRoomListingResult? res = null;
            var i = 0;
            int? totalRooms = null;
            do {
                var url = $"/_synapse/admin/v1/rooms?limit={Math.Min(limit, 100)}&dir={dir}&order_by={orderBy}";
                if (!string.IsNullOrEmpty(searchTerm)) url += $"&search_term={searchTerm}";

                if (res?.NextBatch is not null) url += $"&from={res.NextBatch}";

                Console.WriteLine($"--- ADMIN Querying Room List with URL: {url} - Already have {i} items... ---");

                res = await authenticatedHomeserver.ClientHttpClient.GetFromJsonAsync<AdminRoomListingResult>(url);
                totalRooms ??= res.TotalRooms;
                Console.WriteLine(res.ToJson(false));
                foreach (var room in res.Rooms) {
                    if (localFilter is not null) {
                        if (!room.RoomId.Contains(localFilter.RoomIdContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Name?.Contains(localFilter.NameContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.CanonicalAlias?.Contains(localFilter.CanonicalAliasContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Version.Contains(localFilter.VersionContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Creator.Contains(localFilter.CreatorContains)) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.Encryption?.Contains(localFilter.EncryptionContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.JoinRules?.Contains(localFilter.JoinRulesContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.GuestAccess?.Contains(localFilter.GuestAccessContains) == true) {
                            totalRooms--;
                            continue;
                        }
                        if (!room.HistoryVisibility?.Contains(localFilter.HistoryVisibilityContains) == true) {
                            totalRooms--;
                            continue;
                        }

                        if (localFilter.CheckFederation && room.Federatable != localFilter.Federatable) {
                            totalRooms--;
                            continue;
                        }
                        if (localFilter.CheckPublic && room.Public != localFilter.Public) {
                            totalRooms--;
                            continue;
                        }

                        if (room.JoinedMembers < localFilter.JoinedMembersGreaterThan || room.JoinedMembers > localFilter.JoinedMembersLessThan) {
                            totalRooms--;
                            continue;
                        }
                        if (room.JoinedLocalMembers < localFilter.JoinedLocalMembersGreaterThan || room.JoinedLocalMembers > localFilter.JoinedLocalMembersLessThan) {
                            totalRooms--;
                            continue;
                        }
                    }
                    // if (contentSearch is not null && !string.IsNullOrEmpty(contentSearch) &&
                    //     !(
                    //         room.Name?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true ||
                    //         room.CanonicalAlias?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true ||
                    //         room.Creator?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true
                    //     )
                    //    ) {
                    //     totalRooms--;
                    //     continue;
                    // }

                    i++;
                    yield return room;
                }
            } while (i < Math.Min(limit, totalRooms ?? limit));
        }
    }

    public AuthenticatedHomeserverSynapse(string serverName, string accessToken) : base(serverName, accessToken) {
        Admin = new(this);
    }
}