about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
diff options
context:
space:
mode:
authorTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 01:49:10 +0200
committerTheArcaneBrony <myrainbowdash949@gmail.com>2023-06-13 01:49:10 +0200
commitfc749b3e57098740377e6eabd5d010d133256fa5 (patch)
treecc97267a3d4222c910769e46bdb37c96c7c31531 /MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
parentunknown changes (diff)
downloadMatrixUtils-fc749b3e57098740377e6eabd5d010d133256fa5.tar.xz
Improved many features
Diffstat (limited to 'MatrixRoomUtils.Core/AuthenticatedHomeServer.cs')
-rw-r--r--MatrixRoomUtils.Core/AuthenticatedHomeServer.cs63
1 files changed, 51 insertions, 12 deletions
diff --git a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
index be085c1..368aa20 100644
--- a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
+++ b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs
@@ -2,8 +2,10 @@ using System.Net.Http.Headers;
 using System.Net.Http.Json;
 using System.Text.Json;
 using System.Text.Json.Nodes;
+using MatrixRoomUtils.Core.Extensions;
 using MatrixRoomUtils.Core.Interfaces;
 using MatrixRoomUtils.Core.Responses;
+using MatrixRoomUtils.Core.Responses.Admin;
 
 namespace MatrixRoomUtils.Core;
 
@@ -32,7 +34,6 @@ public class AuthenticatedHomeServer : IHomeServer
 
         return this;
     }
-    
 
     public async Task<Room> GetRoom(string roomId)
     {
@@ -48,19 +49,18 @@ public class AuthenticatedHomeServer : IHomeServer
             Console.WriteLine($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
             throw new InvalidDataException($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
         }
-        
 
         var roomsJson = await roomQuery.Content.ReadFromJsonAsync<JsonElement>();
         foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray())
         {
             rooms.Add(new Room(_httpClient, room.GetString()));
         }
-        
+
         Console.WriteLine($"Fetched {rooms.Count} rooms");
 
         return rooms;
     }
-    
+
     public async Task<string> UploadFile(string fileName, Stream fileStream, string contentType = "application/octet-stream")
     {
         var res = await _httpClient.PostAsync($"/_matrix/media/r0/upload?filename={fileName}", new StreamContent(fileStream));
@@ -69,11 +69,11 @@ public class AuthenticatedHomeServer : IHomeServer
             Console.WriteLine($"Failed to upload file: {await res.Content.ReadAsStringAsync()}");
             throw new InvalidDataException($"Failed to upload file: {await res.Content.ReadAsStringAsync()}");
         }
+
         var resJson = await res.Content.ReadFromJsonAsync<JsonElement>();
         return resJson.GetProperty("content_uri").GetString()!;
     }
-    
-    
+
     public async Task<Room> CreateRoom(CreateRoomRequest creationEvent)
     {
         var res = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/createRoom", creationEvent);
@@ -83,11 +83,9 @@ public class AuthenticatedHomeServer : IHomeServer
             throw new InvalidDataException($"Failed to create room: {await res.Content.ReadAsStringAsync()}");
         }
 
-        return await GetRoom((await res.Content.ReadFromJsonAsync<JsonObject>())!["room_id"]!.ToString()!);
+        return await GetRoom((await res.Content.ReadFromJsonAsync<JsonObject>())!["room_id"]!.ToString());
     }
-    
-    
-    
+
     public class HomeserverAdminApi
     {
         private readonly AuthenticatedHomeServer _authenticatedHomeServer;
@@ -96,6 +94,47 @@ public class AuthenticatedHomeServer : IHomeServer
         {
             _authenticatedHomeServer = authenticatedHomeServer;
         }
-    }
 
-}
+        public async IAsyncEnumerable<AdminRoomListingResult.AdminRoomListingResultRoom> SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", string? searchTerm = null, string? contentSearch = null)
+        {
+            AdminRoomListingResult? res = null;
+            int 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 != null)
+                {
+                    url += $"&from={res.NextBatch}";
+                }
+                Console.WriteLine($"--- ADMIN Querying Room List with URL: {url} - Already have {i} items... ---");
+
+                res = await _authenticatedHomeServer._httpClient.GetFromJsonAsync<AdminRoomListingResult>(url);
+                totalRooms ??= res?.TotalRooms;
+                Console.WriteLine(res.ToJson(indent:false));
+                foreach (var room in res.Rooms)
+                {
+                    if (contentSearch != 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