diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-04 00:13:25 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-04 00:18:46 +0200 |
commit | b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1 (patch) | |
tree | 4176eb2b7f1befca685d00e119d842eb3f07da1f /MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | |
parent | Small refactoring (diff) | |
download | MatrixUtils-b933f7ed1189c7e14d82b4fcf5c98fb3ef4b9cf1.tar.xz |
Refactoring
Diffstat (limited to 'MatrixRoomUtils.Core/AuthenticatedHomeServer.cs')
-rw-r--r-- | MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | 44 |
1 files changed, 28 insertions, 16 deletions
diff --git a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs index dd9aa25..031b6b6 100644 --- a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs +++ b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs @@ -1,8 +1,10 @@ using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text.Json; +using MatrixRoomUtils.Core.Extensions; +using MatrixRoomUtils.Core.Interfaces; -namespace MatrixRoomUtils; +namespace MatrixRoomUtils.Core; public class AuthenticatedHomeServer : IHomeServer { @@ -15,18 +17,20 @@ public class AuthenticatedHomeServer : IHomeServer AccessToken = accessToken; HomeServerDomain = canonicalHomeServerDomain; _httpClient = new HttpClient(); - - var rhsfwt = ResolveHomeserverFromWellKnown(canonicalHomeServerDomain); - rhsfwt.ContinueWith(_ => - { - FullHomeServerDomain = rhsfwt.Result; - _httpClient.Dispose(); - _httpClient = new HttpClient {BaseAddress = new Uri(FullHomeServerDomain)}; - _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); - Console.WriteLine("[AHS] Finished setting up http client :)"); - }); } + public async Task<AuthenticatedHomeServer> Configure() + { + FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain); + _httpClient.Dispose(); + _httpClient = new HttpClient { 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); @@ -35,19 +39,27 @@ public class AuthenticatedHomeServer : IHomeServer public async Task<List<Room>> GetJoinedRooms() { var rooms = new List<Room>(); - var _rooms = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); - if (!_rooms.IsSuccessStatusCode) + var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); + if (!roomQuery.IsSuccessStatusCode) { - Console.WriteLine($"Failed to get rooms: {await _rooms.Content.ReadAsStringAsync()}"); - throw new InvalidDataException($"Failed to get rooms: {await _rooms.Content.ReadAsStringAsync()}"); + Console.WriteLine($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}"); } + - var roomsJson = await _rooms.Content.ReadFromJsonAsync<JsonElement>(); + 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> ResolveMediaUri(string mxc) + { + return mxc.Replace("mxc://", $"{FullHomeServerDomain}/_matrix/media/r0/download/"); + } } \ No newline at end of file |