about summary refs log tree commit diff
path: root/LibMatrix/Homeservers
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs38
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs3
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs2
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs49
4 files changed, 74 insertions, 18 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
index b881e6c..f70dd39 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs
@@ -1,3 +1,4 @@
+using System.Net.Http.Headers;
 using System.Net.Http.Json;
 using System.Text.Json;
 using System.Text.Json.Nodes;
@@ -12,18 +13,30 @@ using LibMatrix.Services;
 namespace LibMatrix.Homeservers;
 
 public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
-    public AuthenticatedHomeserverGeneric(string canonicalHomeServerDomain, string accessToken) : base(canonicalHomeServerDomain) {
+    public AuthenticatedHomeserverGeneric(string baseUrl, string accessToken) : base(baseUrl) {
         AccessToken = accessToken.Trim();
         SyncHelper = new SyncHelper(this);
+
+        _httpClient.Timeout = TimeSpan.FromMinutes(15);
+        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
     }
 
     public virtual SyncHelper SyncHelper { get; init; }
-    public virtual WhoAmIResponse WhoAmI { get; set; } = null!;
-    public virtual string UserId => WhoAmI.UserId;
+    private WhoAmIResponse? _whoAmI;
+
+    public WhoAmIResponse? WhoAmI => _whoAmI ??= _httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami").Result;
+    public string UserId => WhoAmI.UserId;
+
+    // public virtual async Task<WhoAmIResponse> WhoAmI() {
+    // if (_whoAmI is not null) return _whoAmI;
+    // _whoAmI = await _httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami");
+    // return _whoAmI;
+    // }
+
     public virtual string AccessToken { get; set; }
 
     public virtual GenericRoom GetRoom(string roomId) {
-        if(roomId is null || !roomId.StartsWith("!")) throw new ArgumentException("Room ID must start with !", nameof(roomId));
+        if (roomId is null || !roomId.StartsWith("!")) throw new ArgumentException("Room ID must start with !", nameof(roomId));
         return new GenericRoom(this, roomId);
     }
 
@@ -50,7 +63,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
     }
 
     public virtual async Task<GenericRoom> CreateRoom(CreateRoomRequest creationEvent) {
-        creationEvent.CreationContent["creator"] = UserId;
+        creationEvent.CreationContent["creator"] = WhoAmI.UserId;
         var res = await _httpClient.PostAsJsonAsync("/_matrix/client/v3/createRoom", creationEvent, new JsonSerializerOptions {
             DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
         });
@@ -61,9 +74,10 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
 
         var room = GetRoom((await res.Content.ReadFromJsonAsync<JsonObject>())!["room_id"]!.ToString());
 
-        foreach (var user in creationEvent.Invite) {
-            await room.InviteUser(user);
-        }
+        if (creationEvent.Invite is not null)
+            foreach (var user in creationEvent.Invite) {
+                await room.InviteUserAsync(user);
+            }
 
         return room;
     }
@@ -77,6 +91,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
     }
 
 #region Utility Functions
+
     public virtual async IAsyncEnumerable<GenericRoom> GetJoinedRoomsByType(string type) {
         var rooms = await GetJoinedRooms();
         var tasks = rooms.Select(async room => {
@@ -92,6 +107,7 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
             if (result is not null) yield return result;
         }
     }
+
 #endregion
 
 #region Account Data
@@ -104,11 +120,11 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
         // }
         //
         // return await res.Content.ReadFromJsonAsync<T>();
-        return await _httpClient.GetFromJsonAsync<T>($"/_matrix/client/v3/user/{UserId}/account_data/{key}");
+        return await _httpClient.GetFromJsonAsync<T>($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}");
     }
 
     public virtual async Task SetAccountData(string key, object data) {
-        var res = await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{UserId}/account_data/{key}", data);
+        var res = await _httpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}", data);
         if (!res.IsSuccessStatusCode) {
             Console.WriteLine($"Failed to set account data: {await res.Content.ReadAsStringAsync()}");
             throw new InvalidDataException($"Failed to set account data: {await res.Content.ReadAsStringAsync()}");
@@ -116,4 +132,6 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeServer {
     }
 
 #endregion
+
+
 }
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs
index 5319f46..a420d71 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs
@@ -4,5 +4,4 @@ using LibMatrix.Services;
 
 namespace LibMatrix.Homeservers;
 
-public class AuthenticatedHomeserverMxApiExtended(string canonicalHomeServerDomain, string accessToken) : AuthenticatedHomeserverGeneric(canonicalHomeServerDomain,
-    accessToken);
+public class AuthenticatedHomeserverMxApiExtended(string baseUrl, string accessToken) : AuthenticatedHomeserverGeneric(baseUrl, accessToken);
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs
index ae26f69..e355d2d 100644
--- a/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs
+++ b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs
@@ -102,7 +102,7 @@ public class AuthenticatedHomeserverSynapse : AuthenticatedHomeserverGeneric {
         }
     }
 
-    public AuthenticatedHomeserverSynapse(string canonicalHomeServerDomain, string accessToken) : base(canonicalHomeServerDomain, accessToken) {
+    public AuthenticatedHomeserverSynapse(string baseUrl, string accessToken) : base(baseUrl, accessToken) {
         Admin = new(this);
     }
 }
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs
index ab3ab51..d10c837 100644
--- a/LibMatrix/Homeservers/RemoteHomeServer.cs
+++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -1,19 +1,22 @@
 using System.Net.Http.Json;
+using System.Text.Json;
 using System.Text.Json.Serialization;
 using ArcaneLibs.Extensions;
 using LibMatrix.EventTypes.Spec.State;
 using LibMatrix.Extensions;
 using LibMatrix.Responses;
+using LibMatrix.Services;
 
 namespace LibMatrix.Homeservers;
 
-public class RemoteHomeServer(string canonicalHomeServerDomain) {
-    // _httpClient.Timeout = TimeSpan.FromSeconds(5);
+public class RemoteHomeServer(string baseUrl) {
 
     private Dictionary<string, object> _profileCache { get; set; } = new();
-    public string HomeServerDomain { get; } = canonicalHomeServerDomain.Trim();
-    public string FullHomeServerDomain { get; set; }
-    public MatrixHttpClient _httpClient { get; set; } = new();
+    public string BaseUrl { get; } = baseUrl.Trim();
+    public MatrixHttpClient _httpClient { get; set; } = new() {
+        BaseAddress = new Uri(new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl).Result ?? throw new InvalidOperationException("Failed to resolve homeserver")),
+        Timeout = TimeSpan.FromSeconds(120)
+    };
 
     public async Task<ProfileResponseEventContent> GetProfileAsync(string mxid) {
         if (mxid is null) throw new ArgumentNullException(nameof(mxid));
@@ -46,6 +49,42 @@ public class RemoteHomeServer(string canonicalHomeServerDomain) {
         if (!resp.IsSuccessStatusCode) Console.WriteLine("ResolveAlias: " + data.ToJson());
         return data;
     }
+
+#region Authentication
+
+    public async Task<LoginResponse> LoginAsync(string username, string password, string? deviceName = null) {
+        var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/login", new {
+            type = "m.login.password",
+            identifier = new {
+                type = "m.id.user",
+                user = username
+            },
+            password = password,
+            initial_device_display_name = deviceName
+        });
+        var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
+        if (!resp.IsSuccessStatusCode) Console.WriteLine("Login: " + data.ToJson());
+        return data;
+    }
+
+    public async Task<LoginResponse> RegisterAsync(string username, string password, string? deviceName = null) {
+        var resp = await _httpClient.PostAsJsonAsync("/_matrix/client/r0/register", new {
+            kind = "user",
+            auth = new {
+                type = "m.login.dummy"
+            },
+            username,
+            password,
+            initial_device_display_name = deviceName ?? "LibMatrix"
+        }, new JsonSerializerOptions() {
+            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
+        });
+        var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
+        if (!resp.IsSuccessStatusCode) Console.WriteLine("Register: " + data.ToJson());
+        return data;
+    }
+
+#endregion
 }
 
 public class AliasResult {