MatrixRoomUtils

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

RemoteHomeServer.cs (1669B)


      1 using System.Net.Http.Json;
      2 using System.Text.Json;
      3 using MatrixRoomUtils.Core.Interfaces;
      4 
      5 namespace MatrixRoomUtils.Core;
      6 
      7 public class RemoteHomeServer : IHomeServer
      8 {
      9 
     10 
     11     public RemoteHomeServer(string canonicalHomeServerDomain)
     12     {
     13         HomeServerDomain = canonicalHomeServerDomain;
     14         _httpClient = new HttpClient();
     15         _httpClient.Timeout = TimeSpan.FromSeconds(5);
     16     }
     17     public async Task<RemoteHomeServer> Configure()
     18     {
     19         FullHomeServerDomain = await ResolveHomeserverFromWellKnown(HomeServerDomain);
     20         _httpClient.Dispose();
     21         _httpClient = new HttpClient { BaseAddress = new Uri(FullHomeServerDomain) };
     22         _httpClient.Timeout = TimeSpan.FromSeconds(5);
     23         Console.WriteLine("[RHS] Finished setting up http client");
     24 
     25         return this;
     26     }
     27     
     28     public async Task<Room> GetRoom(string roomId)
     29     {
     30         return new Room(_httpClient, roomId);
     31     }
     32 
     33     public async Task<List<Room>> GetJoinedRooms()
     34     {
     35         var rooms = new List<Room>();
     36         var roomQuery = await _httpClient.GetAsync("/_matrix/client/v3/joined_rooms");
     37         if (!roomQuery.IsSuccessStatusCode)
     38         {
     39             Console.WriteLine($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
     40             throw new InvalidDataException($"Failed to get rooms: {await roomQuery.Content.ReadAsStringAsync()}");
     41         }
     42 
     43         var roomsJson = await roomQuery.Content.ReadFromJsonAsync<JsonElement>();
     44         foreach (var room in roomsJson.GetProperty("joined_rooms").EnumerateArray())
     45         {
     46             rooms.Add(new Room(_httpClient, room.GetString()));
     47         }
     48 
     49         return rooms;
     50     }
     51 }