From 21da6cde79ccd0cb7f895a29e3d8cab959ef11ba Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 4 Sep 2023 02:17:10 +0200 Subject: Too many changes to name... --- .../Homeservers/AuthenticatedHomeserverSynapse.cs | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs (limited to 'LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs') diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs new file mode 100644 index 0000000..3b0bc10 --- /dev/null +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs @@ -0,0 +1,110 @@ +using System; +using System.Collections.Generic; +using ArcaneLibs.Extensions; +using LibMatrix.Filters; +using LibMatrix.Responses.Admin; +using LibMatrix.Services; + +namespace LibMatrix.Homeservers; + +public class AuthenticatedHomeserverSynapse : AuthenticatedHomeserverGeneric { + public readonly SynapseAdminApi Admin; + public class SynapseAdminApi { + private readonly AuthenticatedHomeserverGeneric _authenticatedHomeserver; + + public SynapseAdminApi(AuthenticatedHomeserverGeneric authenticatedHomeserver) => _authenticatedHomeserver = authenticatedHomeserver; + + public async IAsyncEnumerable 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._httpClient.GetFromJsonAsync(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(TieredStorageService storage, string canonicalHomeServerDomain, string accessToken) : base(storage, canonicalHomeServerDomain, accessToken) { + Admin = new(this); + } +} -- cgit 1.4.1