about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
Diffstat (limited to 'LibMatrix')
-rw-r--r--LibMatrix/Homeservers/AuthenticatedHomeserverHSE.cs16
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs53
-rw-r--r--LibMatrix/Responses/LoginResponse.cs12
-rw-r--r--LibMatrix/Services/HomeserverProviderService.cs2
4 files changed, 77 insertions, 6 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverHSE.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverHSE.cs
new file mode 100644

index 0000000..1cc8ca2 --- /dev/null +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverHSE.cs
@@ -0,0 +1,16 @@ +using LibMatrix.Homeservers.ImplementationDetails.Synapse; +using LibMatrix.Responses; +using LibMatrix.Services; + +namespace LibMatrix.Homeservers; + +public class AuthenticatedHomeserverHSE : AuthenticatedHomeserverGeneric { + public AuthenticatedHomeserverHSE(string serverName, HomeserverResolverService.WellKnownUris wellKnownUris, string? proxy, string accessToken) : base(serverName, + wellKnownUris, proxy, accessToken) { } + + public Task<Dictionary<string, LoginResponse>> GetExternalProfilesAsync() => + ClientHttpClient.GetFromJsonAsync<Dictionary<string, LoginResponse>>("/_hse/client/v1/external_profiles"); + + public Task SetExternalProfile(string sessionName, LoginResponse session) => + ClientHttpClient.PutAsJsonAsync($"/_hse/client/v1/external_profiles/{sessionName}", session); +} \ No newline at end of file diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs
index 7ac54a7..4ee523f 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -1,4 +1,5 @@ using System.Net.Http.Json; +using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Web; @@ -69,6 +70,15 @@ public class RemoteHomeserver { return data ?? throw new InvalidOperationException($"Could not resolve alias {alias}"); } + public Task<PublicRoomDirectoryResult> GetPublicRoomsAsync(int limit = 100, string? server = null, string? since = null) => + ClientHttpClient.GetFromJsonAsync<PublicRoomDirectoryResult>(buildUriWithParams("/_matrix/client/v3/publicRooms", (nameof(limit), true, limit), + (nameof(server), !string.IsNullOrWhiteSpace(server), server), (nameof(since), !string.IsNullOrWhiteSpace(since), since))); + + // TODO: move this somewhere else + private string buildUriWithParams(string url, params (string name, bool include, object? value)[] values) { + return url + "?" + string.Join("&", values.Where(x => x.include)); + } + #region Authentication public async Task<LoginResponse> LoginAsync(string username, string password, string? deviceName = null) { @@ -109,6 +119,49 @@ public class RemoteHomeserver { public UserInteractiveAuthClient Auth; } +public class PublicRoomDirectoryResult { + [JsonPropertyName("chunk")] + public List<PublicRoomListItem> Chunk { get; set; } + + [JsonPropertyName("next_batch")] + public string? NextBatch { get; set; } + + [JsonPropertyName("prev_batch")] + public string? PrevBatch { get; set; } + + [JsonPropertyName("total_room_count_estimate")] + public int TotalRoomCountEstimate { get; set; } + + public class PublicRoomListItem { + [JsonPropertyName("avatar_url")] + public string? AvatarUrl { get; set; } + + [JsonPropertyName("canonical_alias")] + public string? CanonicalAlias { get; set; } + + [JsonPropertyName("guest_can_join")] + public bool GuestCanJoin { get; set; } + + [JsonPropertyName("join_rule")] + public string JoinRule { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("num_joined_members")] + public int NumJoinedMembers { get; set; } + + [JsonPropertyName("room_id")] + public string RoomId { get; set; } + + [JsonPropertyName("topic")] + public string? Topic { get; set; } + + [JsonPropertyName("world_readable")] + public bool WorldReadable { get; set; } + } +} + public class AliasResult { [JsonPropertyName("room_id")] public string RoomId { get; set; } diff --git a/LibMatrix/Responses/LoginResponse.cs b/LibMatrix/Responses/LoginResponse.cs
index ac2269c..2f78932 100644 --- a/LibMatrix/Responses/LoginResponse.cs +++ b/LibMatrix/Responses/LoginResponse.cs
@@ -1,3 +1,4 @@ +using System.Diagnostics.CodeAnalysis; using System.Text.Json.Serialization; namespace LibMatrix.Responses; @@ -9,20 +10,19 @@ public class LoginResponse { [JsonPropertyName("device_id")] public string DeviceId { get; set; } - private string? _homeserver; - [JsonPropertyName("home_server")] + [field: AllowNull, MaybeNull] public string Homeserver { - get => _homeserver ?? UserId.Split(':', 2).Last(); - protected init => _homeserver = value; + get => field ?? UserId.Split(':', 2).Last(); + set; } [JsonPropertyName("user_id")] public string UserId { get; set; } // public async Task<AuthenticatedHomeserverGeneric> GetAuthenticatedHomeserver(string? proxy = null) { - // var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(Homeserver); - // await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverGeneric>(Homeserver, AccessToken, proxy); + // var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(Homeserver); + // await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverGeneric>(Homeserver, AccessToken, proxy); // } } diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs
index 0fa0e83..601087d 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs
@@ -45,6 +45,8 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger else { if (serverVersion is { Server.Name: "Synapse" }) hs = new AuthenticatedHomeserverSynapse(homeserver, wellKnownUris, proxy, accessToken); + else if (serverVersion is { Server.Name: "LibMatrix.HomeserverEmulator"}) + hs = new AuthenticatedHomeserverHSE(homeserver, wellKnownUris, proxy, accessToken); } } catch (Exception e) {