about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/ImplementationDetails/Synapse
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix/Homeservers/ImplementationDetails/Synapse')
-rw-r--r--LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs23
-rw-r--r--LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs64
-rw-r--r--LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs107
3 files changed, 194 insertions, 0 deletions
diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs
new file mode 100644
index 0000000..f4c927a
--- /dev/null
+++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs
@@ -0,0 +1,23 @@
+using System.Text.Json.Serialization;
+
+namespace LibMatrix.Homeservers.ImplementationDetails.Synapse.Models.Requests;
+
+public class AdminRoomDeleteRequest {
+    [JsonPropertyName("new_room_user_id")]
+    public string? NewRoomUserId { get; set; }
+
+    [JsonPropertyName("room_name")]
+    public string? RoomName { get; set; }
+
+    [JsonPropertyName("block")]
+    public bool Block { get; set; }
+
+    [JsonPropertyName("purge")]
+    public bool Purge { get; set; }
+
+    [JsonPropertyName("message")]
+    public string? Message { get; set; }
+
+    [JsonPropertyName("force_purge")]
+    public bool ForcePurge { get; set; }
+}
\ No newline at end of file
diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs
new file mode 100644
index 0000000..7ab96ac
--- /dev/null
+++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs
@@ -0,0 +1,64 @@
+using System.Text.Json.Serialization;
+
+namespace LibMatrix.Responses.Admin;
+
+public class AdminRoomListingResult {
+    [JsonPropertyName("offset")]
+    public int Offset { get; set; }
+
+    [JsonPropertyName("total_rooms")]
+    public int TotalRooms { get; set; }
+
+    [JsonPropertyName("next_batch")]
+    public int? NextBatch { get; set; }
+
+    [JsonPropertyName("prev_batch")]
+    public int? PrevBatch { get; set; }
+
+    [JsonPropertyName("rooms")]
+    public List<AdminRoomListingResultRoom> Rooms { get; set; } = new();
+
+    public class AdminRoomListingResultRoom {
+        [JsonPropertyName("room_id")]
+        public required string RoomId { get; set; }
+
+        [JsonPropertyName("name")]
+        public string? Name { get; set; }
+
+        [JsonPropertyName("canonical_alias")]
+        public string? CanonicalAlias { get; set; }
+
+        [JsonPropertyName("joined_members")]
+        public int JoinedMembers { get; set; }
+
+        [JsonPropertyName("joined_local_members")]
+        public int JoinedLocalMembers { get; set; }
+
+        [JsonPropertyName("version")]
+        public string? Version { get; set; }
+
+        [JsonPropertyName("creator")]
+        public string? Creator { get; set; }
+
+        [JsonPropertyName("encryption")]
+        public string? Encryption { get; set; }
+
+        [JsonPropertyName("federatable")]
+        public bool Federatable { get; set; }
+
+        [JsonPropertyName("public")]
+        public bool Public { get; set; }
+
+        [JsonPropertyName("join_rules")]
+        public string? JoinRules { get; set; }
+
+        [JsonPropertyName("guest_access")]
+        public string? GuestAccess { get; set; }
+
+        [JsonPropertyName("history_visibility")]
+        public string? HistoryVisibility { get; set; }
+
+        [JsonPropertyName("state_events")]
+        public int StateEvents { get; set; }
+    }
+}
\ No newline at end of file
diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs
new file mode 100644
index 0000000..5cc055d
--- /dev/null
+++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs
@@ -0,0 +1,107 @@
+using ArcaneLibs.Extensions;
+using LibMatrix.Filters;
+using LibMatrix.Responses.Admin;
+
+namespace LibMatrix.Homeservers.ImplementationDetails.Synapse;
+
+public class SynapseAdminApiClient(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));
+    }
+}
\ No newline at end of file