diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 20:25:05 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-13 20:25:05 +0200 |
commit | 712ad189c99570f686ab779782b2a873e172428e (patch) | |
tree | 6102e4719416e71522e9143fa4e06951258bd77c /MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | |
parent | Fix passwords being visible during editing (diff) | |
download | MatrixUtils-712ad189c99570f686ab779782b2a873e172428e.tar.xz |
Change syntax style
Diffstat (limited to 'MatrixRoomUtils.Core/AuthenticatedHomeServer.cs')
-rw-r--r-- | MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | 83 |
1 files changed, 26 insertions, 57 deletions
diff --git a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs index 368aa20..ee6be72 100644 --- a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs +++ b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs @@ -9,63 +9,47 @@ using MatrixRoomUtils.Core.Responses.Admin; namespace MatrixRoomUtils.Core; -public class AuthenticatedHomeServer : IHomeServer -{ - public string UserId { get; set; } - public string AccessToken { get; set; } +public class AuthenticatedHomeServer : IHomeServer { public readonly HomeserverAdminApi Admin; - public AuthenticatedHomeServer(string userId, string accessToken, string canonicalHomeServerDomain) - { + public AuthenticatedHomeServer(string userId, string accessToken, string canonicalHomeServerDomain) { UserId = userId; AccessToken = accessToken; HomeServerDomain = canonicalHomeServerDomain; Admin = new HomeserverAdminApi(this); - _httpClient = new HttpClient(); + _httpClient = new MatrixHttpClient(); } - public async Task<AuthenticatedHomeServer> Configure() - { + public string UserId { get; set; } + public string AccessToken { get; set; } + + public async Task<AuthenticatedHomeServer> Configure() { FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain); _httpClient.Dispose(); - _httpClient = new HttpClient { BaseAddress = new Uri(FullHomeServerDomain) }; + _httpClient = new MatrixHttpClient { BaseAddress = new Uri(FullHomeServerDomain) }; _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); Console.WriteLine("[AHS] Finished setting up http client"); return this; } - public async Task<Room> GetRoom(string roomId) - { - return new Room(_httpClient, roomId); - } + public async Task<Room> GetRoom(string roomId) => new Room(_httpClient, roomId); - public async Task<List<Room>> GetJoinedRooms() - { + public async Task<List<Room>> GetJoinedRooms() { var rooms = new List<Room>(); var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); - if (!roomQuery.IsSuccessStatusCode) - { - 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())); - } + 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") - { + 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)); - if (!res.IsSuccessStatusCode) - { + if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); throw new InvalidDataException($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); } @@ -74,11 +58,9 @@ public class AuthenticatedHomeServer : IHomeServer return resJson.GetProperty("content_uri").GetString()!; } - public async Task<Room> CreateRoom(CreateRoomRequest creationEvent) - { + public async Task<Room> CreateRoom(CreateRoomRequest creationEvent) { var res = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/createRoom", creationEvent); - if (!res.IsSuccessStatusCode) - { + if (!res.IsSuccessStatusCode) { Console.WriteLine($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); throw new InvalidDataException($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); } @@ -86,47 +68,34 @@ public class AuthenticatedHomeServer : IHomeServer return await GetRoom((await res.Content.ReadFromJsonAsync<JsonObject>())!["room_id"]!.ToString()); } - public class HomeserverAdminApi - { + public class HomeserverAdminApi { private readonly AuthenticatedHomeServer _authenticatedHomeServer; - public HomeserverAdminApi(AuthenticatedHomeServer authenticatedHomeServer) - { - _authenticatedHomeServer = authenticatedHomeServer; - } + public HomeserverAdminApi(AuthenticatedHomeServer authenticatedHomeServer) => _authenticatedHomeServer = authenticatedHomeServer; - public async IAsyncEnumerable<AdminRoomListingResult.AdminRoomListingResultRoom> SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", string? searchTerm = null, string? contentSearch = null) - { + 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; + var i = 0; int? totalRooms = null; - do - { + 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 (!string.IsNullOrEmpty(searchTerm)) url += $"&search_term={searchTerm}"; + + if (res?.NextBatch != null) url += $"&from={res.NextBatch}"; - 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) - { + Console.WriteLine(res.ToJson(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; } |