AuthenticatedHomeServer.cs (3693B)
1 using System.Net.Http.Headers; 2 using System.Net.Http.Json; 3 using System.Text.Json; 4 using System.Text.Json.Nodes; 5 using MatrixRoomUtils.Core.Interfaces; 6 using MatrixRoomUtils.Core.Responses; 7 8 namespace MatrixRoomUtils.Core; 9 10 public class AuthenticatedHomeServer : IHomeServer 11 { 12 public string UserId { get; set; } 13 public string AccessToken { get; set; } 14 public readonly HomeserverAdminApi Admin; 15 16 public AuthenticatedHomeServer(string userId, string accessToken, string canonicalHomeServerDomain) 17 { 18 UserId = userId; 19 AccessToken = accessToken; 20 HomeServerDomain = canonicalHomeServerDomain; 21 Admin = new HomeserverAdminApi(this); 22 _httpClient = new HttpClient(); 23 } 24 25 public async Task<AuthenticatedHomeServer> Configure() 26 { 27 FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain); 28 _httpClient.Dispose(); 29 _httpClient = new HttpClient { BaseAddress = new Uri(FullHomeServerDomain) }; 30 _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken); 31 Console.WriteLine("[AHS] Finished setting up http client"); 32 33 return this; 34 } 35 36 37 public async Task<Room> GetRoom(string roomId) 38 { 39 return new Room(_httpClient, roomId); 40 } 41 42 public async Task<List<Room>> GetJoinedRooms() 43 { 44 var rooms = new List<Room>(); 45 var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms"); 46 if (!roomQuery.IsSuccessStatusCode) 47 { 48 Console.WriteLine($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}"); 49 throw new InvalidDataException($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}"); 50 } 51 52 53 var roomsJson = await roomQuery.Content.ReadFromJsonAsync<JsonElement>(); 54 foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray()) 55 { 56 rooms.Add(new Room(_httpClient, room.GetString())); 57 } 58 59 Console.WriteLine($"Fetched {rooms.Count} rooms"); 60 61 return rooms; 62 } 63 64 public async Task<string> UploadFile(string fileName, Stream fileStream, string contentType = "application/octet-stream") 65 { 66 var res = await _httpClient.PostAsync($"/_matrix/media/r0/upload?filename={fileName}", new StreamContent(fileStream)); 67 if (!res.IsSuccessStatusCode) 68 { 69 Console.WriteLine($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); 70 throw new InvalidDataException($"Failed to upload file: {await res.Content.ReadAsStringAsync()}"); 71 } 72 var resJson = await res.Content.ReadFromJsonAsync<JsonElement>(); 73 return resJson.GetProperty("content_uri").GetString()!; 74 } 75 76 77 public async Task<Room> CreateRoom(CreateRoomRequest creationEvent) 78 { 79 var res = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/createRoom", creationEvent); 80 if (!res.IsSuccessStatusCode) 81 { 82 Console.WriteLine($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); 83 throw new InvalidDataException($"Failed to create room: {await res.Content.ReadAsStringAsync()}"); 84 } 85 86 return await GetRoom((await res.Content.ReadFromJsonAsync<JsonObject>())!["room_id"]!.ToString()!); 87 } 88 89 90 91 public class HomeserverAdminApi 92 { 93 private readonly AuthenticatedHomeServer _authenticatedHomeServer; 94 95 public HomeserverAdminApi(AuthenticatedHomeServer authenticatedHomeServer) 96 { 97 _authenticatedHomeServer = authenticatedHomeServer; 98 } 99 } 100 101 }