diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-03 18:40:53 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-05-03 18:40:53 +0200 |
commit | 3d3edeae16252a311704b390cfad6faa435a8b84 (patch) | |
tree | 34974194435fbe9789de5140ef9a9c0ddb834c74 /MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | |
parent | Add policy room discovery ,add room state viewer (diff) | |
download | MatrixUtils-3d3edeae16252a311704b390cfad6faa435a8b84.tar.xz |
Refactor
Diffstat (limited to 'MatrixRoomUtils.Core/AuthenticatedHomeServer.cs')
-rw-r--r-- | MatrixRoomUtils.Core/AuthenticatedHomeServer.cs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs new file mode 100644 index 0000000..dd9aa25 --- /dev/null +++ b/MatrixRoomUtils.Core/AuthenticatedHomeServer.cs @@ -0,0 +1,53 @@ +using System.Net.Http.Headers; +using System.Net.Http.Json; +using System.Text.Json; + +namespace MatrixRoomUtils; + +public class AuthenticatedHomeServer : IHomeServer +{ + public string UserId { get; set; } + public string AccessToken { get; set; } + + public AuthenticatedHomeServer(string userId, string accessToken, string canonicalHomeServerDomain) + { + UserId = userId; + 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<Room> GetRoom(string roomId) + { + return new Room(_httpClient, roomId); + } + + public async Task<List<Room>> GetJoinedRooms() + { + var rooms = new List<Room>(); + var _rooms = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); + if (!_rooms.IsSuccessStatusCode) + { + Console.WriteLine($"Failed to get rooms: {await _rooms.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to get rooms: {await _rooms.Content.ReadAsStringAsync()}"); + } + + var roomsJson = await _rooms.Content.ReadFromJsonAsync<JsonElement>(); + foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray()) + { + rooms.Add(new Room(_httpClient, room.GetString())); + } + + return rooms; + } +} \ No newline at end of file |