From 37b97d65c0a5262539a5de560e911048166b8bba Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Fri, 5 Apr 2024 18:58:32 +0200 Subject: Fix homeserver resolution, rewrite homeserver initialisation, HSE work --- ArcaneLibs | 2 +- LibMatrix/Extensions/HttpClientExtensions.cs | 12 + .../Homeservers/AuthenticatedHomeserverGeneric.cs | 48 ++-- .../AuthenticatedHomeserverMxApiExtended.cs | 5 +- .../Homeservers/AuthenticatedHomeserverSynapse.cs | 108 +------- LibMatrix/Homeservers/FederationClient.cs | 39 +-- .../Models/Requests/AdminRoomDeleteRequest.cs | 23 ++ .../Models/Responses/AdminRoomListingResult.cs | 64 +++++ .../Synapse/SynapseAdminApiClient.cs | 107 ++++++++ LibMatrix/Homeservers/RemoteHomeServer.cs | 44 +--- .../Responses/Admin/AdminRoomDeleteRequest.cs | 23 -- .../Responses/Admin/AdminRoomListingResult.cs | 64 ----- LibMatrix/Responses/LoginResponse.cs | 5 +- LibMatrix/RoomTypes/GenericRoom.cs | 3 +- LibMatrix/Services/HomeserverProviderService.cs | 126 ++++----- LibMatrix/Services/HomeserverResolverService.cs | 169 ++++++++---- LibMatrix/Services/ServiceInstaller.cs | 3 +- LibMatrix/StateEvent.cs | 7 +- .../Controllers/AuthController.cs | 116 ++++---- .../Controllers/LegacyController.cs | 2 +- .../Controllers/Media/MediaController.cs | 99 ++++++- .../Controllers/Rooms/RoomAccountDataController.cs | 78 ++++++ .../Controllers/Rooms/RoomMembersController.cs | 53 ++-- .../Controllers/Rooms/RoomStateController.cs | 31 +-- .../Controllers/Rooms/RoomTimelineController.cs | 292 ++++++++++++++++++++- .../Controllers/Rooms/RoomsController.cs | 6 +- .../Controllers/SyncController.cs | 270 +++++++++++++++---- .../Controllers/ThirdParty/ThirdPartyController.cs | 25 ++ .../Controllers/Users/AccountDataController.cs | 147 +++++------ .../Controllers/Users/FilterController.cs | 12 - .../Controllers/Users/UserController.cs | 18 -- .../Controllers/VersionsController.cs | 186 ++++++------- .../Extensions/EventExtensions.cs | 8 +- .../LibMatrix.HomeserverEmulator.csproj | 52 ++-- Tests/LibMatrix.HomeserverEmulator/Program.cs | 216 +++++++-------- .../Services/MediaStore.cs | 65 +++-- .../Services/PaginationTokenResolverService.cs | 54 ++++ .../Services/RoomStore.cs | 85 ++++-- .../Services/TokenService.cs | 9 +- .../Services/UserStore.cs | 60 ++++- .../Services/CommandListenerHostedService.cs | 17 +- 41 files changed, 1778 insertions(+), 975 deletions(-) create mode 100644 LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs create mode 100644 LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs create mode 100644 LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs delete mode 100644 LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs delete mode 100644 LibMatrix/Responses/Admin/AdminRoomListingResult.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomAccountDataController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs create mode 100644 Tests/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs diff --git a/ArcaneLibs b/ArcaneLibs index 7c6627c..ca63a0d 160000 --- a/ArcaneLibs +++ b/ArcaneLibs @@ -1 +1 @@ -Subproject commit 7c6627c1311dbaea9a6f488334c71c10990ab3dd +Subproject commit ca63a0d5524793d1d76c071e5002173a2651bc7c diff --git a/LibMatrix/Extensions/HttpClientExtensions.cs b/LibMatrix/Extensions/HttpClientExtensions.cs index 60b1fc1..598f8e5 100644 --- a/LibMatrix/Extensions/HttpClientExtensions.cs +++ b/LibMatrix/Extensions/HttpClientExtensions.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; +using ArcaneLibs; using ArcaneLibs.Extensions; namespace LibMatrix.Extensions; @@ -38,6 +39,7 @@ public class MatrixHttpClient : HttpClient { } public async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + Console.WriteLine($"Sending {request.Method} {BaseAddress}{request.RequestUri} ({Util.BytesToString(request.Content?.Headers.ContentLength ?? 0)})"); if (request.RequestUri is null) throw new NullReferenceException("RequestUri is null"); if (!request.RequestUri.IsAbsoluteUri) request.RequestUri = new Uri(BaseAddress, request.RequestUri); // if (AssertedUserId is not null) request.RequestUri = request.RequestUri.AddQuery("user_id", AssertedUserId); @@ -97,6 +99,16 @@ public class MatrixHttpClient : HttpClient { SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUri), cancellationToken ?? CancellationToken.None); // GetFromJsonAsync + public async Task TryGetFromJsonAsync(string requestUri, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) { + try { + return await GetFromJsonAsync(requestUri, options, cancellationToken); + } + catch (HttpRequestException e) { + Console.WriteLine($"Failed to get {requestUri}: {e.Message}"); + return default; + } + } + public async Task GetFromJsonAsync(string requestUri, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default) { options = GetJsonSerializerOptions(options); // Console.WriteLine($"GetFromJsonAsync called for {requestUri} with json options {options?.ToJson(ignoreNull:true)} and cancellation token {cancellationToken}"); diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index 727c0ea..afa6a6c 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -14,49 +14,35 @@ using LibMatrix.Responses; using LibMatrix.RoomTypes; using LibMatrix.Services; using LibMatrix.Utilities; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Homeservers; -public class AuthenticatedHomeserverGeneric(string serverName, string accessToken) : RemoteHomeserver(serverName) { - public static async Task Create(string serverName, string accessToken, string? proxy = null) where T : AuthenticatedHomeserverGeneric => - await Create(typeof(T), serverName, accessToken, proxy) as T ?? throw new InvalidOperationException($"Failed to create instance of {typeof(T).Name}"); - - public static async Task Create(Type type, string serverName, string accessToken, string? proxy = null) { - if (string.IsNullOrWhiteSpace(proxy)) - proxy = null; - if (!type.IsAssignableTo(typeof(AuthenticatedHomeserverGeneric))) throw new ArgumentException("Type must be a subclass of AuthenticatedHomeserverGeneric", nameof(type)); - var instance = Activator.CreateInstance(type, serverName, accessToken) as AuthenticatedHomeserverGeneric - ?? throw new InvalidOperationException($"Failed to create instance of {type.Name}"); - - instance.ClientHttpClient = new MatrixHttpClient { - Timeout = TimeSpan.FromMinutes(15), - DefaultRequestHeaders = { - Authorization = new AuthenticationHeaderValue("Bearer", accessToken) - } - }; - instance.FederationClient = await FederationClient.TryCreate(serverName, proxy); +public class AuthenticatedHomeserverGeneric : RemoteHomeserver { + public AuthenticatedHomeserverGeneric(string serverName, HomeserverResolverService.WellKnownUris wellKnownUris, ref string? proxy, string accessToken) : base(serverName, + wellKnownUris, ref proxy) { + AccessToken = accessToken; + ClientHttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); - if (string.IsNullOrWhiteSpace(proxy)) { - var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(serverName); - instance.ClientHttpClient.BaseAddress = new Uri(urls?.Client ?? throw new InvalidOperationException("Failed to resolve homeserver")); - } - else { - instance.ClientHttpClient.BaseAddress = new Uri(proxy); - instance.ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", serverName); - } + NamedCaches = new HsNamedCaches(this); + } + + public async Task Initialise() { + WhoAmI = await ClientHttpClient.GetFromJsonAsync("/_matrix/client/v3/account/whoami"); + } - instance.WhoAmI = await instance.ClientHttpClient.GetFromJsonAsync("/_matrix/client/v3/account/whoami"); - instance.NamedCaches = new HsNamedCaches(instance); + private WhoAmIResponse? _whoAmI; - return instance; + public WhoAmIResponse WhoAmI { + get => _whoAmI ?? throw new Exception("Initialise was not called or awaited, WhoAmI is null!"); + private set => _whoAmI = value; } - public WhoAmIResponse WhoAmI { get; set; } public string UserId => WhoAmI.UserId; public string UserLocalpart => UserId.Split(":")[0][1..]; public string ServerName => UserId.Split(":", 2)[1]; - public string AccessToken { get; set; } = accessToken; + public string AccessToken { get; set; } public HsNamedCaches NamedCaches { get; set; } = null!; diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs index f0b5675..f55acb8 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverMxApiExtended.cs @@ -1,3 +1,6 @@ +using LibMatrix.Services; + namespace LibMatrix.Homeservers; -public class AuthenticatedHomeserverMxApiExtended(string baseUrl, string accessToken) : AuthenticatedHomeserverGeneric(baseUrl, accessToken); \ No newline at end of file +public class AuthenticatedHomeserverMxApiExtended(string serverName, HomeserverResolverService.WellKnownUris wellKnownUris, ref string? proxy, string accessToken) + : AuthenticatedHomeserverGeneric(serverName, wellKnownUris, ref proxy, accessToken); \ No newline at end of file diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs index 8df0c5b..307a226 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverSynapse.cs @@ -1,113 +1,17 @@ using ArcaneLibs.Extensions; using LibMatrix.Filters; +using LibMatrix.Homeservers.ImplementationDetails.Synapse; using LibMatrix.Responses.Admin; +using LibMatrix.Services; namespace LibMatrix.Homeservers; public class AuthenticatedHomeserverSynapse : AuthenticatedHomeserverGeneric { - public readonly SynapseAdminApi Admin; + public readonly SynapseAdminApiClient Admin; - public class SynapseAdminApi(AuthenticatedHomeserverSynapse authenticatedHomeserver) { - public async IAsyncEnumerable SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", - string? searchTerm = null, LocalRoomQueryFilter? localFilter = null) { - AdminRoomListingResult? res = null; - var i = 0; - int? totalRooms = null; - do { - var url = $"/_synapse/admin/v1/rooms?limit={Math.Min(limit, 100)}&dir={dir}&order_by={orderBy}"; - if (!string.IsNullOrEmpty(searchTerm)) url += $"&search_term={searchTerm}"; - - if (res?.NextBatch is not null) url += $"&from={res.NextBatch}"; - - Console.WriteLine($"--- ADMIN Querying Room List with URL: {url} - Already have {i} items... ---"); - - res = await authenticatedHomeserver.ClientHttpClient.GetFromJsonAsync(url); - totalRooms ??= res.TotalRooms; - Console.WriteLine(res.ToJson(false)); - foreach (var room in res.Rooms) { - if (localFilter is not null) { - if (!room.RoomId.Contains(localFilter.RoomIdContains)) { - totalRooms--; - continue; - } - - if (!room.Name?.Contains(localFilter.NameContains) == true) { - totalRooms--; - continue; - } - - if (!room.CanonicalAlias?.Contains(localFilter.CanonicalAliasContains) == true) { - totalRooms--; - continue; - } - - if (!room.Version.Contains(localFilter.VersionContains)) { - totalRooms--; - continue; - } - - if (!room.Creator.Contains(localFilter.CreatorContains)) { - totalRooms--; - continue; - } - - if (!room.Encryption?.Contains(localFilter.EncryptionContains) == true) { - totalRooms--; - continue; - } - - if (!room.JoinRules?.Contains(localFilter.JoinRulesContains) == true) { - totalRooms--; - continue; - } - - if (!room.GuestAccess?.Contains(localFilter.GuestAccessContains) == true) { - totalRooms--; - continue; - } - - if (!room.HistoryVisibility?.Contains(localFilter.HistoryVisibilityContains) == true) { - totalRooms--; - continue; - } - - if (localFilter.CheckFederation && room.Federatable != localFilter.Federatable) { - totalRooms--; - continue; - } - - if (localFilter.CheckPublic && room.Public != localFilter.Public) { - totalRooms--; - continue; - } - - if (room.JoinedMembers < localFilter.JoinedMembersGreaterThan || room.JoinedMembers > localFilter.JoinedMembersLessThan) { - totalRooms--; - continue; - } - - if (room.JoinedLocalMembers < localFilter.JoinedLocalMembersGreaterThan || room.JoinedLocalMembers > localFilter.JoinedLocalMembersLessThan) { - totalRooms--; - continue; - } - } - // if (contentSearch is not null && !string.IsNullOrEmpty(contentSearch) && - // !( - // room.Name?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true || - // room.CanonicalAlias?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true || - // room.Creator?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true - // ) - // ) { - // totalRooms--; - // continue; - // } - - i++; - yield return room; - } - } while (i < Math.Min(limit, totalRooms ?? limit)); - } + public AuthenticatedHomeserverSynapse(string serverName, HomeserverResolverService.WellKnownUris wellKnownUris, ref string? proxy, string accessToken) : base(serverName, + wellKnownUris, ref proxy, accessToken) { + Admin = new(this); } - public AuthenticatedHomeserverSynapse(string serverName, string accessToken) : base(serverName, accessToken) => Admin = new SynapseAdminApi(this); } \ No newline at end of file diff --git a/LibMatrix/Homeservers/FederationClient.cs b/LibMatrix/Homeservers/FederationClient.cs index 3926b29..dc0d1f6 100644 --- a/LibMatrix/Homeservers/FederationClient.cs +++ b/LibMatrix/Homeservers/FederationClient.cs @@ -1,41 +1,19 @@ using System.Text.Json.Serialization; using LibMatrix.Extensions; using LibMatrix.Services; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Homeservers; -public class FederationClient(string baseUrl) { - public static async Task TryCreate(string baseUrl, string? proxy = null) { - try { - return await Create(baseUrl, proxy); - } - catch (Exception e) { - Console.WriteLine($"Failed to create homeserver {baseUrl}: {e.Message}"); - return null; - } +public class FederationClient { + public FederationClient(string federationEndpoint, string? proxy = null) { + HttpClient = new MatrixHttpClient { + BaseAddress = new Uri(proxy?.TrimEnd('/') ?? federationEndpoint.TrimEnd('/')), + Timeout = TimeSpan.FromSeconds(120) + }; + if (proxy is not null) HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", federationEndpoint); } - public static async Task Create(string baseUrl, string? proxy = null) { - var homeserver = new FederationClient(baseUrl); - homeserver.WellKnownUris = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl); - if (string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Client)) - Console.WriteLine($"Failed to resolve homeserver client URI for {baseUrl}"); - if (string.IsNullOrWhiteSpace(proxy) && string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server)) - Console.WriteLine($"Failed to resolve homeserver server URI for {baseUrl}"); - - if (!string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server)) - homeserver.HttpClient = new MatrixHttpClient { - BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Server ?? throw new InvalidOperationException($"Failed to resolve homeserver server URI for {baseUrl}")), - Timeout = TimeSpan.FromSeconds(120) - }; - - if (proxy is not null) homeserver.HttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); - - return homeserver; - } - - public string BaseUrl { get; } = baseUrl; - public MatrixHttpClient HttpClient { get; set; } = null!; public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } = null!; @@ -46,7 +24,6 @@ public class ServerVersionResponse { [JsonPropertyName("server")] public required ServerInfo Server { get; set; } - // ReSharper disable once ClassNeverInstantiated.Global public class ServerInfo { [JsonPropertyName("name")] public string Name { get; set; } diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs new file mode 100644 index 0000000..f4c927a --- /dev/null +++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Requests/AdminRoomDeleteRequest.cs @@ -0,0 +1,23 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Homeservers.ImplementationDetails.Synapse.Models.Requests; + +public class AdminRoomDeleteRequest { + [JsonPropertyName("new_room_user_id")] + public string? NewRoomUserId { get; set; } + + [JsonPropertyName("room_name")] + public string? RoomName { get; set; } + + [JsonPropertyName("block")] + public bool Block { get; set; } + + [JsonPropertyName("purge")] + public bool Purge { get; set; } + + [JsonPropertyName("message")] + public string? Message { get; set; } + + [JsonPropertyName("force_purge")] + public bool ForcePurge { get; set; } +} \ No newline at end of file diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs new file mode 100644 index 0000000..7ab96ac --- /dev/null +++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/Models/Responses/AdminRoomListingResult.cs @@ -0,0 +1,64 @@ +using System.Text.Json.Serialization; + +namespace LibMatrix.Responses.Admin; + +public class AdminRoomListingResult { + [JsonPropertyName("offset")] + public int Offset { get; set; } + + [JsonPropertyName("total_rooms")] + public int TotalRooms { get; set; } + + [JsonPropertyName("next_batch")] + public int? NextBatch { get; set; } + + [JsonPropertyName("prev_batch")] + public int? PrevBatch { get; set; } + + [JsonPropertyName("rooms")] + public List Rooms { get; set; } = new(); + + public class AdminRoomListingResultRoom { + [JsonPropertyName("room_id")] + public required string RoomId { get; set; } + + [JsonPropertyName("name")] + public string? Name { get; set; } + + [JsonPropertyName("canonical_alias")] + public string? CanonicalAlias { get; set; } + + [JsonPropertyName("joined_members")] + public int JoinedMembers { get; set; } + + [JsonPropertyName("joined_local_members")] + public int JoinedLocalMembers { get; set; } + + [JsonPropertyName("version")] + public string? Version { get; set; } + + [JsonPropertyName("creator")] + public string? Creator { get; set; } + + [JsonPropertyName("encryption")] + public string? Encryption { get; set; } + + [JsonPropertyName("federatable")] + public bool Federatable { get; set; } + + [JsonPropertyName("public")] + public bool Public { get; set; } + + [JsonPropertyName("join_rules")] + public string? JoinRules { get; set; } + + [JsonPropertyName("guest_access")] + public string? GuestAccess { get; set; } + + [JsonPropertyName("history_visibility")] + public string? HistoryVisibility { get; set; } + + [JsonPropertyName("state_events")] + public int StateEvents { get; set; } + } +} \ No newline at end of file diff --git a/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs b/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs new file mode 100644 index 0000000..5cc055d --- /dev/null +++ b/LibMatrix/Homeservers/ImplementationDetails/Synapse/SynapseAdminApiClient.cs @@ -0,0 +1,107 @@ +using ArcaneLibs.Extensions; +using LibMatrix.Filters; +using LibMatrix.Responses.Admin; + +namespace LibMatrix.Homeservers.ImplementationDetails.Synapse; + +public class SynapseAdminApiClient(AuthenticatedHomeserverSynapse authenticatedHomeserver) { + public async IAsyncEnumerable SearchRoomsAsync(int limit = int.MaxValue, string orderBy = "name", string dir = "f", + string? searchTerm = null, LocalRoomQueryFilter? localFilter = null) { + AdminRoomListingResult? res = null; + var i = 0; + int? totalRooms = null; + do { + var url = $"/_synapse/admin/v1/rooms?limit={Math.Min(limit, 100)}&dir={dir}&order_by={orderBy}"; + if (!string.IsNullOrEmpty(searchTerm)) url += $"&search_term={searchTerm}"; + + if (res?.NextBatch is not null) url += $"&from={res.NextBatch}"; + + Console.WriteLine($"--- ADMIN Querying Room List with URL: {url} - Already have {i} items... ---"); + + res = await authenticatedHomeserver.ClientHttpClient.GetFromJsonAsync(url); + totalRooms ??= res.TotalRooms; + Console.WriteLine(res.ToJson(false)); + foreach (var room in res.Rooms) { + if (localFilter is not null) { + if (!room.RoomId.Contains(localFilter.RoomIdContains)) { + totalRooms--; + continue; + } + + if (!room.Name?.Contains(localFilter.NameContains) == true) { + totalRooms--; + continue; + } + + if (!room.CanonicalAlias?.Contains(localFilter.CanonicalAliasContains) == true) { + totalRooms--; + continue; + } + + if (!room.Version.Contains(localFilter.VersionContains)) { + totalRooms--; + continue; + } + + if (!room.Creator.Contains(localFilter.CreatorContains)) { + totalRooms--; + continue; + } + + if (!room.Encryption?.Contains(localFilter.EncryptionContains) == true) { + totalRooms--; + continue; + } + + if (!room.JoinRules?.Contains(localFilter.JoinRulesContains) == true) { + totalRooms--; + continue; + } + + if (!room.GuestAccess?.Contains(localFilter.GuestAccessContains) == true) { + totalRooms--; + continue; + } + + if (!room.HistoryVisibility?.Contains(localFilter.HistoryVisibilityContains) == true) { + totalRooms--; + continue; + } + + if (localFilter.CheckFederation && room.Federatable != localFilter.Federatable) { + totalRooms--; + continue; + } + + if (localFilter.CheckPublic && room.Public != localFilter.Public) { + totalRooms--; + continue; + } + + if (room.JoinedMembers < localFilter.JoinedMembersGreaterThan || room.JoinedMembers > localFilter.JoinedMembersLessThan) { + totalRooms--; + continue; + } + + if (room.JoinedLocalMembers < localFilter.JoinedLocalMembersGreaterThan || room.JoinedLocalMembers > localFilter.JoinedLocalMembersLessThan) { + totalRooms--; + continue; + } + } + // if (contentSearch is not null && !string.IsNullOrEmpty(contentSearch) && + // !( + // room.Name?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true || + // room.CanonicalAlias?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true || + // room.Creator?.Contains(contentSearch, StringComparison.InvariantCultureIgnoreCase) == true + // ) + // ) { + // totalRooms--; + // continue; + // } + + i++; + yield return room; + } + } while (i < Math.Min(limit, totalRooms ?? limit)); + } +} \ No newline at end of file diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs index 8cd7ad7..e6d58b1 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs @@ -6,50 +6,32 @@ using ArcaneLibs.Extensions; using LibMatrix.Extensions; using LibMatrix.Responses; using LibMatrix.Services; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Homeservers; -public class RemoteHomeserver(string baseUrl) { - public static async Task TryCreate(string baseUrl, string? proxy = null) { - try { - return await Create(baseUrl, proxy); - } - catch (Exception e) { - Console.WriteLine($"Failed to create homeserver {baseUrl}: {e.Message}"); - return null; - } - } - - public static async Task Create(string baseUrl, string? proxy = null) { +public class RemoteHomeserver { + public RemoteHomeserver(string baseUrl, HomeserverResolverService.WellKnownUris wellKnownUris, ref string? proxy) { if (string.IsNullOrWhiteSpace(proxy)) proxy = null; - var homeserver = new RemoteHomeserver(baseUrl); - homeserver.WellKnownUris = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(baseUrl); - if (string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Client)) - Console.WriteLine($"Failed to resolve homeserver client URI for {baseUrl}"); - if (string.IsNullOrWhiteSpace(homeserver.WellKnownUris.Server)) - Console.WriteLine($"Failed to resolve homeserver server URI for {baseUrl}"); - - Console.WriteLine(homeserver.WellKnownUris.ToJson(ignoreNull: false)); - - homeserver.ClientHttpClient = new MatrixHttpClient { - BaseAddress = new Uri(proxy ?? homeserver.WellKnownUris.Client ?? throw new InvalidOperationException($"Failed to resolve homeserver client URI for {baseUrl}")), + BaseUrl = baseUrl; + WellKnownUris = wellKnownUris; + ClientHttpClient = new MatrixHttpClient { + BaseAddress = new Uri(proxy?.TrimEnd('/') ?? wellKnownUris.Client?.TrimEnd('/') ?? throw new InvalidOperationException($"No client URI for {baseUrl}!")), Timeout = TimeSpan.FromSeconds(300) }; - homeserver.FederationClient = await FederationClient.TryCreate(baseUrl, proxy); - - if (proxy is not null) homeserver.ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); - - return homeserver; + if (proxy is not null) ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", baseUrl); + if (!string.IsNullOrWhiteSpace(wellKnownUris.Server)) + FederationClient = new FederationClient(WellKnownUris.Server!, proxy); } private Dictionary _profileCache { get; set; } = new(); - public string BaseUrl { get; } = baseUrl; + public string BaseUrl { get; } - public MatrixHttpClient ClientHttpClient { get; set; } = null!; + public MatrixHttpClient ClientHttpClient { get; set; } public FederationClient? FederationClient { get; set; } - public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } = null!; + public HomeserverResolverService.WellKnownUris WellKnownUris { get; set; } public async Task GetProfileAsync(string mxid, bool useCache = false) { if (mxid is null) throw new ArgumentNullException(nameof(mxid)); diff --git a/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs b/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs deleted file mode 100644 index ceb1b3f..0000000 --- a/LibMatrix/Responses/Admin/AdminRoomDeleteRequest.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Text.Json.Serialization; - -namespace LibMatrix.Responses.Admin; - -public class AdminRoomDeleteRequest { - [JsonPropertyName("new_room_user_id")] - public string? NewRoomUserId { get; set; } - - [JsonPropertyName("room_name")] - public string? RoomName { get; set; } - - [JsonPropertyName("block")] - public bool Block { get; set; } - - [JsonPropertyName("purge")] - public bool Purge { get; set; } - - [JsonPropertyName("message")] - public string? Message { get; set; } - - [JsonPropertyName("force_purge")] - public bool ForcePurge { get; set; } -} \ No newline at end of file diff --git a/LibMatrix/Responses/Admin/AdminRoomListingResult.cs b/LibMatrix/Responses/Admin/AdminRoomListingResult.cs deleted file mode 100644 index 7ab96ac..0000000 --- a/LibMatrix/Responses/Admin/AdminRoomListingResult.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Text.Json.Serialization; - -namespace LibMatrix.Responses.Admin; - -public class AdminRoomListingResult { - [JsonPropertyName("offset")] - public int Offset { get; set; } - - [JsonPropertyName("total_rooms")] - public int TotalRooms { get; set; } - - [JsonPropertyName("next_batch")] - public int? NextBatch { get; set; } - - [JsonPropertyName("prev_batch")] - public int? PrevBatch { get; set; } - - [JsonPropertyName("rooms")] - public List Rooms { get; set; } = new(); - - public class AdminRoomListingResultRoom { - [JsonPropertyName("room_id")] - public required string RoomId { get; set; } - - [JsonPropertyName("name")] - public string? Name { get; set; } - - [JsonPropertyName("canonical_alias")] - public string? CanonicalAlias { get; set; } - - [JsonPropertyName("joined_members")] - public int JoinedMembers { get; set; } - - [JsonPropertyName("joined_local_members")] - public int JoinedLocalMembers { get; set; } - - [JsonPropertyName("version")] - public string? Version { get; set; } - - [JsonPropertyName("creator")] - public string? Creator { get; set; } - - [JsonPropertyName("encryption")] - public string? Encryption { get; set; } - - [JsonPropertyName("federatable")] - public bool Federatable { get; set; } - - [JsonPropertyName("public")] - public bool Public { get; set; } - - [JsonPropertyName("join_rules")] - public string? JoinRules { get; set; } - - [JsonPropertyName("guest_access")] - public string? GuestAccess { get; set; } - - [JsonPropertyName("history_visibility")] - public string? HistoryVisibility { get; set; } - - [JsonPropertyName("state_events")] - public int StateEvents { get; set; } - } -} \ No newline at end of file diff --git a/LibMatrix/Responses/LoginResponse.cs b/LibMatrix/Responses/LoginResponse.cs index 3962fa6..28fb245 100644 --- a/LibMatrix/Responses/LoginResponse.cs +++ b/LibMatrix/Responses/LoginResponse.cs @@ -21,9 +21,10 @@ public class LoginResponse { [JsonPropertyName("user_id")] public string UserId { get; set; } = null!; - public async Task GetAuthenticatedHomeserver(string? proxy = null) => + // public async Task GetAuthenticatedHomeserver(string? proxy = null) { // var urls = await new HomeserverResolverService().ResolveHomeserverFromWellKnown(Homeserver); - await AuthenticatedHomeserverGeneric.Create(Homeserver, AccessToken, proxy); + // await AuthenticatedHomeserverGeneric.Create(Homeserver, AccessToken, proxy); + // } } public class LoginRequest { diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index cf32df8..36abadc 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -12,6 +12,7 @@ using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Homeservers; using LibMatrix.Services; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.RoomTypes; @@ -313,7 +314,7 @@ public class GenericRoom { if (useOriginHomeserver) try { var hs = avatar.Url.Split('/', 3)[1]; - return await new HomeserverResolverService().ResolveMediaUri(hs, avatar.Url); + return await new HomeserverResolverService(NullLogger.Instance).ResolveMediaUri(hs, avatar.Url); } catch (Exception e) { Console.WriteLine(e); diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 8e2e15b..3995a26 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -1,4 +1,5 @@ using System.Net.Http.Json; +using ArcaneLibs.Collections; using ArcaneLibs.Extensions; using LibMatrix.Homeservers; using LibMatrix.Responses; @@ -6,99 +7,62 @@ using Microsoft.Extensions.Logging; namespace LibMatrix.Services; -public class HomeserverProviderService(ILogger logger) { - private static readonly Dictionary AuthenticatedHomeserverSemaphore = new(); - private static readonly Dictionary AuthenticatedHomeserverCache = new(); - - private static readonly Dictionary RemoteHomeserverSemaphore = new(); - private static readonly Dictionary RemoteHomeserverCache = new(); +public class HomeserverProviderService(ILogger logger, HomeserverResolverService hsResolver) { + private static SemaphoreCache AuthenticatedHomeserverCache = new(); + private static SemaphoreCache RemoteHomeserverCache = new(); public async Task GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null, string? impersonatedMxid = null) { - var cacheKey = homeserver + accessToken + proxy + impersonatedMxid; - var sem = AuthenticatedHomeserverSemaphore.GetOrCreate(cacheKey, _ => new SemaphoreSlim(1, 1)); - await sem.WaitAsync(); - AuthenticatedHomeserverGeneric? hs; - lock (AuthenticatedHomeserverCache) { - if (AuthenticatedHomeserverCache.TryGetValue(cacheKey, out hs)) { - sem.Release(); - return hs; - } - } - - var rhs = await RemoteHomeserver.Create(homeserver, proxy); - ClientVersionsResponse clientVersions = new(); - try { - clientVersions = await rhs.GetClientVersionsAsync(); - } - catch (Exception e) { - logger.LogError(e, "Failed to get client versions for {homeserver}", homeserver); - } - - if (proxy is not null) - logger.LogInformation("Homeserver {homeserver} proxied via {proxy}...", homeserver, proxy); - logger.LogInformation("{homeserver}: {clientVersions}", homeserver, clientVersions.ToJson()); + return await AuthenticatedHomeserverCache.GetOrAdd($"{homeserver}{accessToken}{proxy}{impersonatedMxid}", async () => { + var wellKnownUris = await hsResolver.ResolveHomeserverFromWellKnown(homeserver); + var rhs = new RemoteHomeserver(homeserver, wellKnownUris, ref proxy); - ServerVersionResponse serverVersion; - try { - serverVersion = serverVersion = await (rhs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult(null)!); - } - catch (Exception e) { - logger.LogWarning(e, "Failed to get server version for {homeserver}", homeserver); - sem.Release(); - throw; - } - - try { - if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out var a) && a) - hs = await AuthenticatedHomeserverGeneric.Create(homeserver, accessToken, proxy); - else { - if (serverVersion is { Server.Name: "Synapse" }) - hs = await AuthenticatedHomeserverGeneric.Create(homeserver, accessToken, proxy); - else - hs = await AuthenticatedHomeserverGeneric.Create(homeserver, accessToken, proxy); + ClientVersionsResponse? clientVersions = new(); + try { + clientVersions = await rhs.GetClientVersionsAsync(); + } + catch (Exception e) { + logger.LogError(e, "Failed to get client versions for {homeserver}", homeserver); } - } - catch (Exception e) { - logger.LogError(e, "Failed to create authenticated homeserver for {homeserver}", homeserver); - sem.Release(); - throw; - } - - if (impersonatedMxid is not null) - await hs.SetImpersonate(impersonatedMxid); - - lock (AuthenticatedHomeserverCache) { - AuthenticatedHomeserverCache[cacheKey] = hs; - } - - sem.Release(); - - return hs; - } - public async Task GetRemoteHomeserver(string homeserver, string? proxy = null) { - var cacheKey = homeserver + proxy; - var sem = RemoteHomeserverSemaphore.GetOrCreate(cacheKey, _ => new SemaphoreSlim(1, 1)); - await sem.WaitAsync(); - RemoteHomeserver? hs; - lock (RemoteHomeserverCache) { - if (RemoteHomeserverCache.TryGetValue(cacheKey, out hs)) { - sem.Release(); - return hs; + ServerVersionResponse? serverVersion; + try { + serverVersion = await (rhs.FederationClient?.GetServerVersionAsync() ?? Task.FromResult(null)!); + } + catch (Exception e) { + logger.LogWarning(e, "Failed to get server version for {homeserver}", homeserver); + throw; } - } - hs = await RemoteHomeserver.Create(homeserver, proxy); + AuthenticatedHomeserverGeneric hs; + try { + if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out var a) && a) + hs = new AuthenticatedHomeserverMxApiExtended(homeserver, wellKnownUris, ref proxy, accessToken); + else { + if (serverVersion is { Server.Name: "Synapse" }) + hs = new AuthenticatedHomeserverSynapse(homeserver, wellKnownUris, ref proxy, accessToken); + else + hs = new AuthenticatedHomeserverGeneric(homeserver, wellKnownUris, ref proxy, accessToken); + } + } + catch (Exception e) { + logger.LogError(e, "Failed to create authenticated homeserver for {homeserver}", homeserver); + throw; + } - lock (RemoteHomeserverCache) { - RemoteHomeserverCache[cacheKey] = hs; - } + await hs.Initialise(); - sem.Release(); + if (impersonatedMxid is not null) + await hs.SetImpersonate(impersonatedMxid); - return hs; + return hs; + }); } + public async Task GetRemoteHomeserver(string homeserver, string? proxy = null) => + await RemoteHomeserverCache.GetOrAdd($"{homeserver}{proxy}", async () => { + return new RemoteHomeserver(homeserver, await hsResolver.ResolveHomeserverFromWellKnown(homeserver), ref proxy); + }); + public async Task Login(string homeserver, string user, string password, string? proxy = null) { var hs = await GetRemoteHomeserver(homeserver, proxy); var payload = new LoginRequest { diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index bcef541..42ad0a1 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -1,87 +1,135 @@ using System.Collections.Concurrent; +using System.Diagnostics; +using System.Net.Http.Json; using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using ArcaneLibs.Collections; using ArcaneLibs.Extensions; using LibMatrix.Extensions; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace LibMatrix.Services; -public class HomeserverResolverService(ILogger? logger = null) { +public class HomeserverResolverService { private readonly MatrixHttpClient _httpClient = new() { Timeout = TimeSpan.FromMilliseconds(10000) }; - private static readonly ConcurrentDictionary WellKnownCache = new(); - private static readonly ConcurrentDictionary WellKnownSemaphores = new(); + private static readonly SemaphoreCache WellKnownCache = new(); - public async Task ResolveHomeserverFromWellKnown(string homeserver) { - if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); - WellKnownSemaphores.TryAdd(homeserver, new SemaphoreSlim(1, 1)); - await WellKnownSemaphores[homeserver].WaitAsync(); - if (WellKnownCache.TryGetValue(homeserver, out var known)) { - WellKnownSemaphores[homeserver].Release(); - return known; + private readonly ILogger _logger; + + public HomeserverResolverService(ILogger logger) { + _logger = logger; + if (logger is NullLogger) { + var stackFrame = new StackTrace(true).GetFrame(1); + Console.WriteLine( + $"WARN | Null logger provided to HomeserverResolverService!\n{stackFrame.GetMethod().DeclaringType} at {stackFrame.GetFileName()}:{stackFrame.GetFileLineNumber()}"); } + } + + private static SemaphoreSlim _wellKnownSemaphore = new(1, 1); + + public async Task ResolveHomeserverFromWellKnown(string homeserver) { + ArgumentNullException.ThrowIfNull(homeserver); - logger?.LogInformation("Resolving homeserver: {}", homeserver); - var res = new WellKnownUris { - Client = await _tryResolveFromClientWellknown(homeserver), - Server = await _tryResolveFromServerWellknown(homeserver) - }; - WellKnownCache.TryAdd(homeserver, res); - WellKnownSemaphores[homeserver].Release(); - return res; + return await WellKnownCache.GetOrAdd(homeserver, async () => { + await _wellKnownSemaphore.WaitAsync(); + _logger.LogTrace($"Resolving homeserver well-knowns: {homeserver}"); + var client = _tryResolveClientEndpoint(homeserver); + + var res = new WellKnownUris(); + + // try { + res.Client = await client ?? throw new Exception("Could not resolve client URL."); + // } + // catch (Exception e) { + // _logger.LogError(e, "Error resolving client well-known for {hs}", homeserver); + // } + + var server = _tryResolveServerEndpoint(homeserver); + + // try { + res.Server = await server ?? throw new Exception("Could not resolve server URL."); + // } + // catch (Exception e) { + // _logger.LogError(e, "Error resolving server well-known for {hs}", homeserver); + // } + + _logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false)); + _wellKnownSemaphore.Release(); + return res; + }); } - private async Task _tryResolveFromClientWellknown(string homeserver) { - if (!homeserver.StartsWith("http")) { - if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/.well-known/matrix/client")) - homeserver = "https://" + homeserver; - else if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/.well-known/matrix/client")) { - homeserver = "http://" + homeserver; - } + private async Task _tryResolveClientEndpoint(string homeserver) { + ArgumentNullException.ThrowIfNull(homeserver); + _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); + ClientWellKnown? clientWellKnown = null; + // check if homeserver has a client well-known + if (homeserver.StartsWith("https://")) { + clientWellKnown = await _httpClient.TryGetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); } - - try { - var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); - var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); - return hs; + else if (homeserver.StartsWith("http://")) { + clientWellKnown = await _httpClient.TryGetFromJsonAsync($"{homeserver}/.well-known/matrix/client"); } - catch { - // ignored + else { + clientWellKnown ??= await _httpClient.TryGetFromJsonAsync($"https://{homeserver}/.well-known/matrix/client"); + clientWellKnown ??= await _httpClient.TryGetFromJsonAsync($"http://{homeserver}/.well-known/matrix/client"); + + if (clientWellKnown is null) { + if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/_matrix/client/versions")) + return $"https://{homeserver}"; + if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/_matrix/client/versions")) + return $"http://{homeserver}"; + } } - logger?.LogInformation("No client well-known..."); + if (!string.IsNullOrWhiteSpace(clientWellKnown?.Homeserver.BaseUrl)) + return clientWellKnown.Homeserver.BaseUrl; + + _logger.LogInformation("No client well-known..."); return null; } - private async Task _tryResolveFromServerWellknown(string homeserver) { - if (!homeserver.StartsWith("http")) { - if (await _httpClient.CheckSuccessStatus($"https://{homeserver}/.well-known/matrix/server")) - homeserver = "https://" + homeserver; - else if (await _httpClient.CheckSuccessStatus($"http://{homeserver}/.well-known/matrix/server")) { - homeserver = "http://" + homeserver; - } + private async Task _tryResolveServerEndpoint(string homeserver) { + // TODO: implement SRV delegation via DoH: https://developers.google.com/speed/public-dns/docs/doh/json + ArgumentNullException.ThrowIfNull(homeserver); + _logger.LogTrace($"Resolving server well-known: {homeserver}"); + ServerWellKnown? serverWellKnown = null; + // check if homeserver has a server well-known + if (homeserver.StartsWith("https://")) { + serverWellKnown = await _httpClient.TryGetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); } - - try { - var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); - var hs = resp.GetProperty("m.server").GetString(); - if (hs is null) throw new InvalidDataException("m.server is null"); - if (!hs.StartsWithAnyOf("http://", "https://")) - hs = $"https://{hs}"; - return hs; + else if (homeserver.StartsWith("http://")) { + serverWellKnown = await _httpClient.TryGetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); + } + else { + serverWellKnown ??= await _httpClient.TryGetFromJsonAsync($"https://{homeserver}/.well-known/matrix/server"); + serverWellKnown ??= await _httpClient.TryGetFromJsonAsync($"http://{homeserver}/.well-known/matrix/server"); } - catch { - // ignored + + _logger.LogInformation("Server well-known for {hs}: {json}", homeserver, serverWellKnown?.ToJson() ?? "null"); + + if (!string.IsNullOrWhiteSpace(serverWellKnown?.Homeserver)) { + var resolved = serverWellKnown.Homeserver; + if (resolved.StartsWith("https://") || resolved.StartsWith("http://")) + return resolved; + if (await _httpClient.CheckSuccessStatus($"https://{resolved}/_matrix/federation/v1/version")) + return $"https://{resolved}"; + if (await _httpClient.CheckSuccessStatus($"http://{resolved}/_matrix/federation/v1/version")) + return $"http://{resolved}"; + _logger.LogWarning("Server well-known points to invalid server: {resolved}", resolved); } - // fallback: most servers host these on the same location - var clientUrl = await _tryResolveFromClientWellknown(homeserver); + // fallback: most servers host C2S and S2S on the same domain + var clientUrl = await _tryResolveClientEndpoint(homeserver); if (clientUrl is not null && await _httpClient.CheckSuccessStatus($"{clientUrl}/_matrix/federation/v1/version")) return clientUrl; - logger?.LogInformation("No server well-known..."); + _logger.LogInformation("No server well-known..."); return null; } @@ -97,4 +145,19 @@ public class HomeserverResolverService(ILogger? logge public string? Client { get; set; } public string? Server { get; set; } } + + public class ClientWellKnown { + [JsonPropertyName("m.homeserver")] + public WellKnownHomeserver Homeserver { get; set; } + + public class WellKnownHomeserver { + [JsonPropertyName("base_url")] + public string BaseUrl { get; set; } + } + } + + public class ServerWellKnown { + [JsonPropertyName("m.server")] + public string Homeserver { get; set; } + } } \ No newline at end of file diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index 0f07b61..06ea9de 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; namespace LibMatrix.Services; @@ -11,7 +12,7 @@ public static class ServiceInstaller { services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); //Add services - services.AddSingleton(); + services.AddSingleton(sp => new HomeserverResolverService(sp.GetRequiredService>())); // if (services.First(x => x.ServiceType == typeof(TieredStorageService)).Lifetime == ServiceLifetime.Singleton) { services.AddSingleton(); diff --git a/LibMatrix/StateEvent.cs b/LibMatrix/StateEvent.cs index 9800c27..26c6a5f 100644 --- a/LibMatrix/StateEvent.cs +++ b/LibMatrix/StateEvent.cs @@ -24,7 +24,8 @@ public class StateEvent { return dict; }).ToFrozenDictionary(); - public static Type GetStateEventType(string? type) => string.IsNullOrWhiteSpace(type) ? typeof(UnknownEventContent) : KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); + public static Type GetStateEventType(string? type) => + string.IsNullOrWhiteSpace(type) ? typeof(UnknownEventContent) : KnownStateEventTypesByName.GetValueOrDefault(type) ?? typeof(UnknownEventContent); [JsonIgnore] public Type MappedType => GetStateEventType(Type); @@ -67,7 +68,9 @@ public class StateEvent { set { if (value is null) RawContent?.Clear(); - else RawContent = JsonSerializer.Deserialize(JsonSerializer.Serialize(value, value.GetType())); + else + RawContent = JsonSerializer.Deserialize(JsonSerializer.Serialize(value, value.GetType(), + new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull })); } } diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs index da56ec4..66548e2 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/AuthController.cs @@ -1,49 +1,69 @@ -using System.Text.Json.Nodes; -using LibMatrix.HomeserverEmulator.Services; -using LibMatrix.Responses; -using LibMatrix.Services; -using Microsoft.AspNetCore.Mvc; - -namespace LibMatrix.HomeserverEmulator.Controllers; - -[ApiController] -[Route("/_matrix/client/{version}/")] -public class AuthController(ILogger logger, UserStore userStore, TokenService tokenService) : ControllerBase { - [HttpPost("login")] - public async Task Login(LoginRequest request) { - if(!request.Identifier.User.StartsWith('@')) - request.Identifier.User = $"@{request.Identifier.User}:{tokenService.GenerateServerName(HttpContext)}"; - if(request.Identifier.User.EndsWith("localhost")) - request.Identifier.User = request.Identifier.User.Replace("localhost", tokenService.GenerateServerName(HttpContext)); - - var user = await userStore.GetUserById(request.Identifier.User); - if(user is null) { - user = await userStore.CreateUser(request.Identifier.User); - } - - return user.Login(); - } - - [HttpGet("login")] - public async Task GetLoginFlows() { - return new LoginFlowsResponse { - Flows = ((string[]) [ - "m.login.password", - "m.login.recaptcha", - "m.login.sso", - "m.login.email.identity", - "m.login.msisdn", - "m.login.dummy", - "m.login.registration_token", - ]).Select(x => new LoginFlowsResponse.LoginFlow { Type = x }).ToList() - }; - } -} - -public class LoginFlowsResponse { - public required List Flows { get; set; } - - public class LoginFlow { - public required string Type { get; set; } - } +using System.Text.Json.Nodes; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class AuthController(ILogger logger, UserStore userStore, TokenService tokenService) : ControllerBase { + [HttpPost("login")] + public async Task Login(LoginRequest request) { + if (!request.Identifier.User.StartsWith('@')) + request.Identifier.User = $"@{request.Identifier.User}:{tokenService.GenerateServerName(HttpContext)}"; + if (request.Identifier.User.EndsWith("localhost")) + request.Identifier.User = request.Identifier.User.Replace("localhost", tokenService.GenerateServerName(HttpContext)); + + var user = await userStore.GetUserById(request.Identifier.User); + if (user is null) { + user = await userStore.CreateUser(request.Identifier.User); + } + + return user.Login(); + } + + [HttpGet("login")] + public async Task GetLoginFlows() { + return new LoginFlowsResponse { + Flows = ((string[]) [ + "m.login.password", + "m.login.recaptcha", + "m.login.sso", + "m.login.email.identity", + "m.login.msisdn", + "m.login.dummy", + "m.login.registration_token", + ]).Select(x => new LoginFlowsResponse.LoginFlow { Type = x }).ToList() + }; + } + + [HttpPost("logout")] + public async Task Logout() { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + if (user == null) + throw new MatrixException() { + ErrorCode = "M_UNKNOWN_TOKEN", + Error = "No such user" + }; + + if (!user.AccessTokens.ContainsKey(token)) + throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_NOT_FOUND, + Error = "Token not found" + }; + + user.AccessTokens.Remove(token); + return new { }; + } +} + +public class LoginFlowsResponse { + public required List Flows { get; set; } + + public class LoginFlow { + public required string Type { get; set; } + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs index e3f781b..1fb427e 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/LegacyController.cs @@ -18,7 +18,7 @@ public class LegacyController(ILogger logger, TokenService tok [SuppressMessage("ReSharper.DPA", "DPA0011: High execution time of MVC action", Justification = "Endpoint is expected to wait until data is available or timeout.")] public async Task Sync([FromRoute] string roomId, [FromQuery] int limit = 20) { var sw = Stopwatch.StartNew(); - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs index dba36d7..4820a65 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Media/MediaController.cs @@ -1,14 +1,26 @@ +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using ArcaneLibs.Extensions; using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Services; +using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc; namespace LibMatrix.HomeserverEmulator.Controllers.Media; [ApiController] [Route("/_matrix/media/{version}/")] -public class MediaController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { +public class MediaController( + ILogger logger, + TokenService tokenService, + UserStore userStore, + HSEConfiguration cfg, + HomeserverResolverService hsResolver, + MediaStore mediaStore) + : ControllerBase { [HttpPost("upload")] public async Task UploadMedia([FromHeader(Name = "Content-Type")] string ContentType, [FromQuery] string filename, [FromBody] Stream file) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -21,14 +33,89 @@ public class MediaController(ILogger logger, TokenService token ErrorCode = "M_UNKNOWN_TOKEN", Error = "No such user" }; - - - + var mediaId = Guid.NewGuid().ToString(); var media = new { content_uri = $"mxc://{tokenService.GenerateServerName(HttpContext)}/{mediaId}" }; return media; - + } + + private Dictionary downloadLocks = new(); + + [HttpGet("download/{serverName}/{mediaId}")] + public async Task DownloadMedia(string serverName, string mediaId) { + while (true) + try { + if (cfg.StoreData) { + SemaphoreSlim ss; + if (!downloadLocks.ContainsKey(serverName + mediaId)) + downloadLocks[serverName + mediaId] = new SemaphoreSlim(1); + ss = downloadLocks[serverName + mediaId]; + await ss.WaitAsync(); + var serverMediaPath = Path.Combine(cfg.DataStoragePath, "media", serverName); + Directory.CreateDirectory(serverMediaPath); + var mediaPath = Path.Combine(serverMediaPath, mediaId); + if (System.IO.File.Exists(mediaPath)) { + ss.Release(); + await using var stream = new FileStream(mediaPath, FileMode.Open); + await stream.CopyToAsync(Response.Body); + return; + } + else { + var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + await using var stream = System.IO.File.OpenWrite(mediaPath); + using var response = await new HttpClient().GetAsync(mediaUrl); + await response.Content.CopyToAsync(stream); + await stream.FlushAsync(); + ss.Release(); + await DownloadMedia(serverName, mediaId); + return; + } + } + else { + var mediaUrl = await hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var response = await new HttpClient().GetAsync(mediaUrl); + await response.Content.CopyToAsync(Response.Body); + return; + } + + return; + } + catch (IOException) { + //ignored + } + } + + [HttpGet("thumbnail/{serverName}/{mediaId}")] + public async Task DownloadThumbnail(string serverName, string mediaId) { + await DownloadMedia(serverName, mediaId); + } + + [HttpGet("preview_url")] + public async Task GetPreviewUrl([FromQuery] string url) { + JsonObject data = new(); + + using var hc = new HttpClient(); + using var response = await hc.GetAsync(url); + var doc = await response.Content.ReadAsStringAsync(); + var match = Regex.Match(doc, " logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpPost("read_markers")] + public async Task SetReadMarkers(string roomId, [FromBody] ReadMarkersData data) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + var room = roomStore.GetRoomById(roomId); + if (room == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId)) + throw new MatrixException() { + ErrorCode = "M_FORBIDDEN", + Error = "User is not in the room" + }; + + if (!room.ReadMarkers.ContainsKey(user.UserId)) + room.ReadMarkers[user.UserId] = new(); + + if (data.FullyRead != null) + room.ReadMarkers[user.UserId].FullyRead = data.FullyRead; + if (data.Read != null) + room.ReadMarkers[user.UserId].Read = data.Read; + if (data.ReadPrivate != null) + room.ReadMarkers[user.UserId].ReadPrivate = data.ReadPrivate; + + if (!room.AccountData.ContainsKey(user.UserId)) + room.AccountData[user.UserId] = new(); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.fully_read", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.FullyRead + } + }); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.read", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.Read + } + }); + + room.AccountData[user.UserId].Add(new StateEventResponse() { + Type = "m.read.private", + StateKey = user.UserId, + RawContent = new() { + ["event_id"] = data.ReadPrivate + } + }); + + return data; + } +} + +public class ReadMarkersData { + [JsonPropertyName("m.fully_read")] + public string? FullyRead { get; set; } + + [JsonPropertyName("m.read")] + public string? Read { get; set; } + + [JsonPropertyName("m.read.private")] + public string? ReadPrivate { get; set; } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs index d5f4217..7d735f7 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomMembersController.cs @@ -1,19 +1,20 @@ using LibMatrix.EventTypes.Spec.State; using LibMatrix.HomeserverEmulator.Services; -using LibMatrix.Responses; -using LibMatrix.RoomTypes; -using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.OpenApi.Validations.Rules; namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; [ApiController] [Route("/_matrix/client/{version}/rooms/{roomId}/")] -public class RoomMembersController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { +public class RoomMembersController( + ILogger logger, + TokenService tokenService, + UserStore userStore, + RoomStore roomStore, + PaginationTokenResolverService paginationTokenResolver) : ControllerBase { [HttpGet("members")] - public async Task> CreateRoom(string roomId, string? at = null, string? membership = null, string? not_membership = null) { - var token = tokenService.GetAccessToken(HttpContext); + public async Task> GetMembers(string roomId, string? at = null, string? membership = null, string? not_membership = null) { + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -26,30 +27,50 @@ public class RoomMembersController(ILogger logger, TokenS ErrorCode = "M_UNKNOWN_TOKEN", Error = "No such user" }; - + var room = roomStore.GetRoomById(roomId); if (room == null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", Error = "Room not found" }; - - var members = room.State.Where(x => x.Type == "m.room.member").ToList(); - - if(membership != null) + + var members = room.Timeline.Where(x => x.Type == "m.room.member" && x.StateKey != null).ToList(); + + if (membership != null) members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership == membership).ToList(); - - if(not_membership != null) + + if (not_membership != null) members = members.Where(x => (x.TypedContent as RoomMemberEventContent)?.Membership != not_membership).ToList(); if (at != null) { - var evt = room.Timeline.FirstOrDefault(x => x.EventId == at); + StateEventResponse? evt = null; + if (at.StartsWith('$')) + evt = await paginationTokenResolver.ResolveTokenToEvent(at, room); + + if (evt is null) { + var time = await paginationTokenResolver.ResolveTokenToTimestamp(at); + evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= time); + if (evt is null) { + logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at); + return []; + } + } + else if (!room.Timeline.Contains(evt)) { + evt = room.Timeline.LastOrDefault(x => x.OriginServerTs <= evt.OriginServerTs); + if (evt is null) { + logger.LogWarning("Sent empty list of members for room {roomId} at {at}, because there were no events at this time!", roomId, at); + return []; + } + } + + // evt = room.Timeline.FirstOrDefault(x => x.EventId == at); if (evt == null) throw new MatrixException() { ErrorCode = "M_NOT_FOUND", Error = "Event not found" }; - + members = members.Where(x => x.OriginServerTs <= evt.OriginServerTs).ToList(); } diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs index cb5f213..3896ac0 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomStateController.cs @@ -10,7 +10,7 @@ namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; public class RoomStateController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { [HttpGet("")] public async Task> GetState(string roomId) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -23,7 +23,7 @@ public class RoomStateController(ILogger logger, TokenServi ErrorCode = "M_UNKNOWN_TOKEN", Error = "No such user" }; - + var room = roomStore.GetRoomById(roomId); if (room == null) throw new MatrixException() { @@ -33,15 +33,15 @@ public class RoomStateController(ILogger logger, TokenServi return room.State; } - + [HttpGet("{eventType}")] - public async Task GetState(string roomId, string eventType) { - return await GetState(roomId, eventType, ""); + public async Task GetState(string roomId, string eventType, [FromQuery] string format = "client") { + return await GetState(roomId, eventType, "", format); } - + [HttpGet("{eventType}/{stateKey}")] - public async Task GetState(string roomId, string eventType, string stateKey) { - var token = tokenService.GetAccessToken(HttpContext); + public async Task GetState(string roomId, string eventType, string stateKey, [FromQuery] string format = "client") { + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -54,7 +54,7 @@ public class RoomStateController(ILogger logger, TokenServi ErrorCode = "M_UNKNOWN_TOKEN", Error = "No such user" }; - + var room = roomStore.GetRoomById(roomId); if (room == null) throw new MatrixException() { @@ -68,17 +68,18 @@ public class RoomStateController(ILogger logger, TokenServi ErrorCode = "M_NOT_FOUND", Error = "Event not found" }; - return stateEvent; + // return stateEvent; + return format == "event" ? stateEvent : stateEvent.RawContent; } - + [HttpPut("{eventType}")] public async Task SetState(string roomId, string eventType, [FromBody] StateEvent request) { return await SetState(roomId, eventType, "", request); } - + [HttpPut("{eventType}/{stateKey}")] public async Task SetState(string roomId, string eventType, string stateKey, [FromBody] StateEvent request) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -91,7 +92,7 @@ public class RoomStateController(ILogger logger, TokenServi ErrorCode = "M_UNKNOWN_TOKEN", Error = "No such user" }; - + var room = roomStore.GetRoomById(roomId); if (room == null) throw new MatrixException() { @@ -101,7 +102,7 @@ public class RoomStateController(ILogger logger, TokenServi var evt = room.SetStateInternal(request.ToStateEvent(user, room)); evt.Type = eventType; evt.StateKey = stateKey; - return new EventIdResponse(){ + return new EventIdResponse() { EventId = evt.EventId ?? throw new InvalidOperationException("EventId is null") }; } diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs index c9bdb57..3d23660 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomTimelineController.cs @@ -1,29 +1,30 @@ -using System.Collections.Frozen; +using System.Collections.Immutable; +using System.Diagnostics; using System.Text.Json.Nodes; +using ArcaneLibs; +using LibMatrix.EventTypes.Spec; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Helpers; using LibMatrix.HomeserverEmulator.Extensions; using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using LibMatrix.Services; using Microsoft.AspNetCore.Mvc; namespace LibMatrix.HomeserverEmulator.Controllers.Rooms; [ApiController] [Route("/_matrix/client/{version}/rooms/{roomId}")] -public class RoomTimelineController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { +public class RoomTimelineController( + ILogger logger, + TokenService tokenService, + UserStore userStore, + RoomStore roomStore, + HomeserverProviderService hsProvider) : ControllerBase { [HttpPut("send/{eventType}/{txnId}")] public async Task SendMessage(string roomId, string eventType, string txnId, [FromBody] JsonObject content) { var token = tokenService.GetAccessToken(HttpContext); - if (token == null) - throw new MatrixException() { - ErrorCode = "M_MISSING_TOKEN", - Error = "Missing token" - }; - var user = await userStore.GetUserByToken(token); - if (user == null) - throw new MatrixException() { - ErrorCode = "M_UNKNOWN_TOKEN", - Error = "No such user" - }; var room = roomStore.GetRoomById(roomId); if (room == null) @@ -32,7 +33,7 @@ public class RoomTimelineController(ILogger logger, Toke Error = "Room not found" }; - if (!room.JoinedMembers.Any(x=>x.StateKey == user.UserId)) + if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId)) throw new MatrixException() { ErrorCode = "M_FORBIDDEN", Error = "User is not in the room" @@ -44,9 +45,272 @@ public class RoomTimelineController(ILogger logger, Toke }.ToStateEvent(user, room); room.Timeline.Add(evt); + if (evt.Type == RoomMessageEventContent.EventId && (evt.TypedContent as RoomMessageEventContent).Body.StartsWith("!hse")) + await HandleHseCommand(evt, room, user); + // else return new() { EventId = evt.EventId }; } + + [HttpGet("messages")] + public async Task GetMessages(string roomId, [FromQuery] string? from = null, [FromQuery] string? to = null, [FromQuery] int limit = 100, + [FromQuery] string? dir = "b") { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + var room = roomStore.GetRoomById(roomId); + if (room == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId)) + throw new MatrixException() { + ErrorCode = "M_FORBIDDEN", + Error = "User is not in the room" + }; + + if (dir == "b") { + var timeline = room.Timeline.TakeLast(limit).ToList(); + return new() { + Start = timeline.First().EventId, + End = timeline.Last().EventId, + Chunk = timeline.AsEnumerable().Reverse().ToList(), + State = timeline.GetCalculatedState() + }; + } + else if (dir == "f") { + var timeline = room.Timeline.Take(limit).ToList(); + return new() { + Start = timeline.First().EventId, + End = room.Timeline.Last() == timeline.Last() ? null : timeline.Last().EventId, + Chunk = timeline + }; + } + else + throw new MatrixException() { + ErrorCode = "M_BAD_REQUEST", + Error = $"Invalid direction '{dir}'" + }; + } + + [HttpGet("event/{eventId}")] + public async Task GetEvent(string roomId, string eventId) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token); + + var room = roomStore.GetRoomById(roomId); + if (room == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Room not found" + }; + + if (!room.JoinedMembers.Any(x => x.StateKey == user.UserId)) + throw new MatrixException() { + ErrorCode = "M_FORBIDDEN", + Error = "User is not in the room" + }; + + var evt = room.Timeline.SingleOrDefault(x => x.EventId == eventId); + if (evt == null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Event not found" + }; + + return evt; + } + +#region Commands + + private void InternalSendMessage(RoomStore.Room room, string content) { + InternalSendMessage(room, new MessageBuilder().WithBody(content).Build()); + } + + private void InternalSendMessage(RoomStore.Room room, RoomMessageEventContent content) { + logger.LogInformation("Sending internal message: {content}", content.Body); + room.Timeline.Add(new StateEventResponse() { + Type = RoomMessageEventContent.EventId, + TypedContent = content, + Sender = $"@hse:{tokenService.GenerateServerName(HttpContext)}", + RoomId = room.RoomId, + EventId = "$" + string.Join("", Random.Shared.GetItems("abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789".ToCharArray(), 100)), + OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds() + }); + } + + private async Task HandleHseCommand(StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + try { + var msgContent = evt.TypedContent as RoomMessageEventContent; + var parts = msgContent.Body.Split('\n')[0].Split(" "); + if (parts.Length < 2) return; + + var command = parts[1]; + switch (command) { + case "import": + await HandleImportCommand(parts[2..], evt, room, user); + break; + case "import-nheko-profiles": + await HandleImportNhekoProfilesCommand(parts[2..], evt, room, user); + break; + case "clear-sync-states": + foreach (var (token, session) in user.AccessTokens) { + session.SyncStates.Clear(); + InternalSendMessage(room, $"Cleared sync states for {token}."); + } + + break; + case "rsp": { + await Task.Delay(1000); + var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ0123456789"; + for (int i = 0; i < 10000; i++) { + // await Task.Delay(100); + // InternalSendMessage(room, $"https://music.youtube.com/watch?v=90oZtyvavSk&i={i}"); + var url = $"https://music.youtube.com/watch?v="; + for (int j = 0; j < 11; j++) { + url += chars[Random.Shared.Next(chars.Length)]; + } + + InternalSendMessage(room, url + "&i=" + i); + if (i % 5000 == 0 || i == 9999) { + Thread.Sleep(5000); + + do { + InternalSendMessage(room, + $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + GC.WaitForPendingFinalizers(); + InternalSendMessage(room, + $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + await Task.Delay(5000); + } while (Process.GetCurrentProcess().WorkingSet64 >= 1_024_000_000); + } + } + break; + } + case "genrooms": { + var sw = Stopwatch.StartNew(); + var count = 1000; + for (int i = 0; i < count; i++) { + var crq = new CreateRoomRequest() { + Name = "Test room", + CreationContent = new() { + ["version"] = "11" + }, + InitialState = [] + }; + + if (Random.Shared.Next(100) > 75) { + crq.CreationContent["type"] = "m.space"; + foreach (var item in Random.Shared.GetItems(roomStore._rooms.ToArray(), 50)) { + crq.InitialState!.Add(new StateEvent() { + Type = "m.space.child", + StateKey = item.RoomId, + TypedContent = new SpaceChildEventContent() { + Suggested = true, + AutoJoin = true, + Via = new List() + } + }.ToStateEvent(user, room)); + } + } + var newRoom = roomStore.CreateRoom(crq); + newRoom.AddUser(user.UserId); + } + InternalSendMessage(room, $"Generated {count} new rooms in {sw.Elapsed}!"); + break; + } + case "gc": + InternalSendMessage(room, + $"Current GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + GC.Collect(GC.MaxGeneration, GCCollectionMode.Aggressive, true, true); + GC.WaitForPendingFinalizers(); + InternalSendMessage(room, + $"GC memory: {Util.BytesToString(GC.GetTotalMemory(false))}, total process memory: {Util.BytesToString(Process.GetCurrentProcess().WorkingSet64)}"); + break; + default: + InternalSendMessage(room, $"Command {command} not found!"); + break; + } + } + catch (Exception ex) { + InternalSendMessage(room, $"An error occurred: {ex.Message}"); + } + } + + private async Task HandleImportNhekoProfilesCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + var msgContent = evt.TypedContent as RoomMessageEventContent; + var parts = msgContent.Body.Split('\n'); + + var data = parts.Where(x => x.Contains(@"\auth\access_token") || x.Contains(@"\auth\home_server")).ToList(); + if (data.Count < 2) { + InternalSendMessage(room, "Invalid data."); + return; + } + + foreach (var line in data) { + var processedLine = line.Replace("\\\\", "\\").Replace("\\_", "_"); + + if (!processedLine.Contains(@"\auth\")) continue; + var profile = processedLine.Split(@"\auth\")[0]; + if (!user.AuthorizedSessions.ContainsKey(profile)) + user.AuthorizedSessions.Add(profile, new()); + if (processedLine.Contains(@"home_server")) { + var server = processedLine.Split('=')[1]; + user.AuthorizedSessions[profile].Homeserver = server; + } + else if (processedLine.Contains(@"access_token")) { + var token = processedLine.Split('=')[1]; + user.AuthorizedSessions[profile].AccessToken = token; + } + } + + foreach (var (key, session) in user.AuthorizedSessions.ToList()) { + if (string.IsNullOrWhiteSpace(session.Homeserver) || string.IsNullOrWhiteSpace(session.AccessToken)) { + InternalSendMessage(room, $"Invalid profile {key}"); + user.AuthorizedSessions.Remove(key); + continue; + } + + InternalSendMessage(room, $"Got profile {key} with server {session.AccessToken}"); + } + } + + private async Task HandleImportCommand(string[] args, StateEventResponse evt, RoomStore.Room room, UserStore.User user) { + var roomId = args[0]; + var profile = args[1]; + + InternalSendMessage(room, $"Importing room {roomId} through profile {profile}..."); + if (!user.AuthorizedSessions.ContainsKey(profile)) { + InternalSendMessage(room, $"Profile {profile} not found."); + return; + } + + var userProfile = user.AuthorizedSessions[profile]; + + InternalSendMessage(room, $"Authenticating with {userProfile.Homeserver}..."); + var hs = await hsProvider.GetAuthenticatedWithToken(userProfile.Homeserver, userProfile.AccessToken); + InternalSendMessage(room, $"Authenticated with {userProfile.Homeserver}."); + var hsRoom = hs.GetRoom(roomId); + + InternalSendMessage(room, $"Starting import..."); + var internalRoom = new RoomStore.Room(roomId); + + var timeline = hsRoom.GetManyMessagesAsync(limit: int.MaxValue, dir: "b", chunkSize: 100000); + await foreach (var resp in timeline) { + internalRoom.Timeline = new(resp.Chunk.AsEnumerable().Reverse().Concat(internalRoom.Timeline)); + InternalSendMessage(room, $"Imported {resp.Chunk.Count} events. Now up to a total of {internalRoom.Timeline.Count} events."); + } + + InternalSendMessage(room, $"Import complete. Saving and inserting user"); + roomStore.AddRoom(internalRoom); + internalRoom.AddUser(user.UserId); + InternalSendMessage(room, $"Import complete. Room is now available."); + } + +#endregion } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs index b0f5014..6849ff8 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Rooms/RoomsController.cs @@ -14,7 +14,7 @@ public class RoomsController(ILogger logger, TokenService token //createRoom [HttpPost("createRoom")] public async Task CreateRoom([FromBody] CreateRoomRequest request) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -71,7 +71,7 @@ public class RoomsController(ILogger logger, TokenService token [HttpPost("rooms/{roomId}/upgrade")] public async Task UpgradeRoom(string roomId, [FromBody] UpgradeRoomRequest request) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", @@ -125,7 +125,7 @@ public class RoomsController(ILogger logger, TokenService token [HttpPost("rooms/{roomId}/leave")] // TODO: implement public async Task LeaveRoom(string roomId) { - var token = tokenService.GetAccessToken(HttpContext); + var token = tokenService.GetAccessTokenOrNull(HttpContext); if (token == null) throw new MatrixException() { ErrorCode = "M_MISSING_TOKEN", diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs index afcf711..024d071 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/SyncController.cs @@ -1,6 +1,8 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Extensions; using LibMatrix.HomeserverEmulator.Services; using LibMatrix.Responses; using Microsoft.AspNetCore.Mvc; @@ -15,11 +17,6 @@ public class SyncController(ILogger logger, TokenService tokenSe public async Task Sync([FromQuery] string? since = null, [FromQuery] int? timeout = 5000) { var sw = Stopwatch.StartNew(); var token = tokenService.GetAccessToken(HttpContext); - if (token == null) - throw new MatrixException() { - ErrorCode = "M_MISSING_TOKEN", - Error = "Missing token" - }; var user = await userStore.GetUserByToken(token); if (user == null) @@ -28,63 +25,67 @@ public class SyncController(ILogger logger, TokenService tokenSe Error = "No such user" }; var session = user.AccessTokens[token]; + UserStore.User.SessionInfo.UserSyncState newSyncState = new(); + + SyncResponse syncResp; + if (string.IsNullOrWhiteSpace(since) || !session.SyncStates.ContainsKey(since)) + syncResp = InitialSync(user, session); + else { + var syncState = session.SyncStates[since]; + newSyncState = syncState.Clone(); - if (string.IsNullOrWhiteSpace(since)) - return InitialSync(user, session); + var newSyncToken = Guid.NewGuid().ToString(); + do { + syncResp = IncrementalSync(user, session, syncState); + syncResp.NextBatch = newSyncToken; + } while (!await HasDataOrStall(syncResp) && sw.ElapsedMilliseconds < timeout); - if (!session.SyncStates.TryGetValue(since, out var syncState)) - if (!cfg.UnknownSyncTokenIsInitialSync) - throw new MatrixException() { - ErrorCode = "M_UNKNOWN", - Error = "Unknown sync token." + if (sw.ElapsedMilliseconds > timeout) { + logger.LogTrace("Sync timed out after {Elapsed}", sw.Elapsed); + return new() { + NextBatch = since }; - else - return InitialSync(user, session); + } + } - var response = new SyncResponse() { - NextBatch = Guid.NewGuid().ToString(), - DeviceOneTimeKeysCount = new() - }; + session.SyncStates[syncResp.NextBatch] = RecalculateSyncStates(newSyncState, syncResp); + logger.LogTrace("Responding to sync after {totalElapsed}", sw.Elapsed); + return syncResp; + } - session.SyncStates.Add(response.NextBatch, new() { - RoomPositions = syncState.RoomPositions.ToDictionary(x => x.Key, x => new UserStore.User.SessionInfo.UserSyncState.SyncRoomPosition() { - TimelinePosition = roomStore._rooms.First(y => y.RoomId == x.Key).Timeline.Count, - AccountDataPosition = roomStore._rooms.First(y => y.RoomId == x.Key).AccountData[user.UserId].Count - }) - }); - - if (!string.IsNullOrWhiteSpace(since)) { - while (sw.ElapsedMilliseconds < timeout && response.Rooms?.Join is not { Count: > 0 }) { - await Task.Delay(100); - var rooms = roomStore._rooms.Where(x => x.State.Any(y => y.Type == "m.room.member" && y.StateKey == user.UserId)).ToList(); - foreach (var room in rooms) { - var roomPositions = syncState.RoomPositions[room.RoomId]; - - response.Rooms ??= new(); - response.Rooms.Join ??= new(); - response.Rooms.Join[room.RoomId] = new() { - Timeline = new(events: room.Timeline.Skip(roomPositions.TimelinePosition).ToList(), limited: false), - AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).Skip(roomPositions.AccountDataPosition).ToList()) - }; - if (response.Rooms.Join[room.RoomId].Timeline?.Events?.Count > 0) - response.Rooms.Join[room.RoomId].State = new(response.Rooms.Join[room.RoomId].Timeline!.Events.Where(x => x.StateKey != null).ToList()); - session.SyncStates[response.NextBatch].RoomPositions[room.RoomId] = new() { - TimelinePosition = room.Timeline.Count, - AccountDataPosition = room.AccountData[user.UserId].Count - }; + private UserStore.User.SessionInfo.UserSyncState RecalculateSyncStates(UserStore.User.SessionInfo.UserSyncState newSyncState, SyncResponse sync) { + logger.LogTrace("Updating sync state"); + var syncStateRecalcSw = Stopwatch.StartNew(); + foreach (var (roomId, roomData) in sync.Rooms?.Join ?? []) { + if (!newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions[roomId] = new(); + var state = newSyncState.RoomPositions[roomId]; - if (response.Rooms.Join[room.RoomId].State?.Events?.Count == 0 && - response.Rooms.Join[room.RoomId].Timeline?.Events?.Count == 0 && - response.Rooms.Join[room.RoomId].AccountData?.Events?.Count == 0 - ) - response.Rooms.Join.Remove(room.RoomId); - } - } + state.TimelinePosition += roomData.Timeline?.Events?.Count ?? 0; + state.LastTimelineEventId = roomData.Timeline?.Events?.LastOrDefault()?.EventId ?? state.LastTimelineEventId; + state.AccountDataPosition += roomData.AccountData?.Events?.Count ?? 0; + state.Joined = true; } - return response; + foreach (var (roomId, _) in sync.Rooms?.Invite ?? []) { + if (!newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions[roomId] = new() { + Joined = false + }; + } + + foreach (var (roomId, _) in sync.Rooms?.Leave ?? []) { + if (newSyncState.RoomPositions.ContainsKey(roomId)) + newSyncState.RoomPositions.Remove(roomId); + } + + logger.LogTrace("Updated sync state in {Elapsed}", syncStateRecalcSw.Elapsed); + + return newSyncState; } +#region Initial Sync parts + private SyncResponse InitialSync(UserStore.User user, UserStore.User.SessionInfo session) { var response = new SyncResponse() { NextBatch = Guid.NewGuid().ToString(), @@ -98,17 +99,174 @@ public class SyncController(ILogger logger, TokenService tokenSe foreach (var room in rooms) { response.Rooms ??= new(); response.Rooms.Join ??= new(); + response.Rooms.Join[room.RoomId] = new() { State = new(room.State.ToList()), Timeline = new(events: room.Timeline.ToList(), limited: false), AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) }; - session.SyncStates[response.NextBatch].RoomPositions[room.RoomId] = new() { - TimelinePosition = room.Timeline.Count, - AccountDataPosition = room.AccountData[user.UserId].Count - }; } return response; } + + private SyncResponse.RoomsDataStructure.JoinedRoomDataStructure GetInitialSyncRoomData(RoomStore.Room room, UserStore.User user) { + return new() { + State = new(room.State.ToList()), + Timeline = new(room.Timeline.ToList(), false), + AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) + }; + } + +#endregion + + private SyncResponse IncrementalSync(UserStore.User user, UserStore.User.SessionInfo session, UserStore.User.SessionInfo.UserSyncState syncState) { + return new SyncResponse { + Rooms = GetIncrementalSyncRooms(user, session, syncState) + }; + } + +#region Incremental Sync parts + + private SyncResponse.RoomsDataStructure GetIncrementalSyncRooms(UserStore.User user, UserStore.User.SessionInfo session, UserStore.User.SessionInfo.UserSyncState syncState) { + SyncResponse.RoomsDataStructure data = new() { + Join = [], + Invite = [], + Leave = [] + }; + + // step 1: check previously synced rooms + foreach (var (roomId, roomPosition) in syncState.RoomPositions) { + var room = roomStore.GetRoomById(roomId); + if (room == null) { + // room no longer exists + data.Leave[roomId] = new(); + continue; + } + + if (roomPosition.Joined) { + var newTimelineEvents = room.Timeline.Skip(roomPosition.TimelinePosition).ToList(); + var newAccountDataEvents = room.AccountData[user.UserId].Skip(roomPosition.AccountDataPosition).ToList(); + if (newTimelineEvents.Count == 0 && newAccountDataEvents.Count == 0) continue; + data.Join[room.RoomId] = new() { + State = new(newTimelineEvents.GetCalculatedState()), + Timeline = new(newTimelineEvents, false) + }; + } + } + + if (data.Join.Count > 0) return data; + + // step 2: check newly joined rooms + var untrackedRooms = roomStore._rooms.Where(r => !syncState.RoomPositions.ContainsKey(r.RoomId)).ToList(); + + var allJoinedRooms = roomStore.GetRoomsByMember(user.UserId).ToArray(); + if (allJoinedRooms.Length == 0) return data; + var rooms = Random.Shared.GetItems(allJoinedRooms, Math.Min(allJoinedRooms.Length, 50)); + foreach (var membership in rooms) { + var membershipContent = membership.TypedContent as RoomMemberEventContent ?? + throw new InvalidOperationException("Membership event content is not RoomMemberEventContent"); + var room = roomStore.GetRoomById(membership.RoomId!); + //handle leave + if (syncState.RoomPositions.TryGetValue(membership.RoomId!, out var syncPosition)) { + // logger.LogTrace("Found sync position {roomId} {value}", room.RoomId, syncPosition.ToJson(indent: false, ignoreNull: false)); + + if (membershipContent.Membership == "join") { + var newTimelineEvents = room.Timeline.Skip(syncPosition.TimelinePosition).ToList(); + var newAccountDataEvents = room.AccountData[user.UserId].Skip(syncPosition.AccountDataPosition).ToList(); + if (newTimelineEvents.Count == 0 && newAccountDataEvents.Count == 0) continue; + data.Join[room.RoomId] = new() { + State = new(newTimelineEvents.GetCalculatedState()), + Timeline = new(newTimelineEvents, false) + }; + } + } + else { + //syncPosisition = null + if (membershipContent.Membership == "join") { + var joinData = data.Join[membership.RoomId!] = new() { + State = new(room.State.ToList()), + Timeline = new(events: room.Timeline.ToList(), limited: false), + AccountData = new(room.AccountData.GetOrCreate(user.UserId, _ => []).ToList()) + }; + } + } + } + + //handle nonexistant rooms + foreach (var roomId in syncState.RoomPositions.Keys) { + if (!roomStore._rooms.Any(x => x.RoomId == roomId)) { + data.Leave[roomId] = new(); + session.SyncStates[session.SyncStates.Last().Key].RoomPositions.Remove(roomId); + } + } + + return data; + } + +#endregion + + private async Task HasDataOrStall(SyncResponse resp) { + // logger.LogTrace("Checking if sync response has data: {resp}", resp.ToJson(indent: false, ignoreNull: true)); + // if (resp.AccountData?.Events?.Count > 0) return true; + // if (resp.Rooms?.Invite?.Count > 0) return true; + // if (resp.Rooms?.Join?.Count > 0) return true; + // if (resp.Rooms?.Leave?.Count > 0) return true; + // if (resp.Presence?.Events?.Count > 0) return true; + // if (resp.DeviceLists?.Changed?.Count > 0) return true; + // if (resp.DeviceLists?.Left?.Count > 0) return true; + // if (resp.ToDevice?.Events?.Count > 0) return true; + // + // var hasData = + // resp is not { + // AccountData: null or { + // Events: null or { Count: 0 } + // }, + // Rooms: null or { + // Invite: null or { Count: 0 }, + // Join: null or { Count: 0 }, + // Leave: null or { Count: 0 } + // }, + // Presence: null or { + // Events: null or { Count: 0 } + // }, + // DeviceLists: null or { + // Changed: null or { Count: 0 }, + // Left: null or { Count: 0 } + // }, + // ToDevice: null or { + // Events: null or { Count: 0 } + // } + // }; + + var hasData = resp is { + AccountData: { + Events: { Count: > 0 } + } + } or { + Presence: { + Events: { Count: > 0 } + } + } or { + DeviceLists: { + Changed: { Count: > 0 }, + Left: { Count: > 0 } + } + } or { + ToDevice: { + Events: { Count: > 0 } + } + }; + + if (!hasData) { + // hasData = + } + + if (!hasData) { + // logger.LogDebug($"Sync response has no data, stalling for 1000ms: {resp.ToJson(indent: false, ignoreNull: true)}"); + await Task.Delay(10); + } + + return hasData; + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs new file mode 100644 index 0000000..ff006df --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/ThirdParty/ThirdPartyController.cs @@ -0,0 +1,25 @@ +using LibMatrix.HomeserverEmulator.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers.ThirdParty; + +[ApiController] +[Route("/_matrix/client/{version}/thirdparty/")] +public class ThirdPartyController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("protocols")] + public async Task GetProtocols() { + return new { }; + } + + [HttpGet("location")] + public async Task> GetLocations([FromQuery] string alias) { + // TODO: implement + return []; + } + + [HttpGet("location/{protocol}")] + public async Task> GetLocation([FromRoute] string protocol) { + // TODO: implement + return []; + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs index 8cd5c75..a32d283 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/AccountDataController.cs @@ -1,81 +1,68 @@ -using System.Text.Json.Nodes; -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.Filters; -using LibMatrix.HomeserverEmulator.Services; -using LibMatrix.Responses; -using Microsoft.AspNetCore.Mvc; - -namespace LibMatrix.HomeserverEmulator.Controllers; - -[ApiController] -[Route("/_matrix/client/{version}/")] -public class AccountDataController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { - [HttpGet("user/{mxid}/account_data/{type}")] - public async Task GetAccountData(string type) { - var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - - var user = await userStore.GetUserByToken(token, false); - if (user is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "Invalid token." - }; - var value = user.AccountData.FirstOrDefault(x=>x.Type == type); - if (value is null) - throw new MatrixException() { - ErrorCode = "M_NOT_FOUND", - Error = "Key not found." - }; - return value; - } - - [HttpPut("user/{mxid}/account_data/{type}")] - public async Task SetAccountData(string type, [FromBody] JsonObject data) { - var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - - var user = await userStore.GetUserByToken(token, false); - if (user is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "Invalid token." - }; - - user.AccountData.Where(x=>x.Type == type).ToList().ForEach(response => user.AccountData.Remove(response)); - - user.AccountData.Add(new() { - Type = type, - RawContent = data - }); - return data; - } - - // specialised account data... - [HttpGet("pushrules")] - public async Task GetPushRules() { - var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - - var user = await userStore.GetUserByToken(token, false); - if (user is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "Invalid token." - }; - return new { }; - } +using System.Text.Json.Nodes; +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Filters; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Responses; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/client/{version}/")] +public class AccountDataController(ILogger logger, TokenService tokenService, UserStore userStore, RoomStore roomStore) : ControllerBase { + [HttpGet("user/{mxid}/account_data/{type}")] + public async Task GetAccountData(string type) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + + var value = user.AccountData.FirstOrDefault(x => x.Type == type); + if (value is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Key not found." + }; + return value; + } + + [HttpPut("user/{mxid}/account_data/{type}")] + public async Task SetAccountData(string type, [FromBody] JsonObject data) { + var token = tokenService.GetAccessToken(HttpContext); + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + + user.AccountData.Add(new() { + Type = type, + RawContent = data + }); + return data; + } + + // specialised account data... + [HttpGet("pushrules")] + public async Task GetPushRules() { + var token = tokenService.GetAccessToken(HttpContext); + if (token is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "No token passed." + }; + + var user = await userStore.GetUserByToken(token, false); + if (user is null) + throw new MatrixException() { + ErrorCode = "M_UNAUTHORIZED", + Error = "Invalid token." + }; + return new { }; + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs index ecbccd4..bdee7bc 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/FilterController.cs @@ -14,12 +14,6 @@ public class FilterController(ILogger logger, TokenService tok [HttpPost("user/{mxid}/filter")] public async Task CreateFilter(string mxid, [FromBody] SyncFilter filter) { var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - var user = await userStore.GetUserByToken(token, false); if (user is null) throw new MatrixException() { @@ -36,12 +30,6 @@ public class FilterController(ILogger logger, TokenService tok [HttpGet("user/{mxid}/filter/{filterId}")] public async Task GetFilter(string mxid, string filterId) { var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - var user = await userStore.GetUserByToken(token, false); if (user is null) throw new MatrixException() { diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs index e886d46..2be3896 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/Users/UserController.cs @@ -15,12 +15,6 @@ public class UserController(ILogger logger, TokenService tokenSe [HttpGet("account/whoami")] public async Task Login() { var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - var user = await userStore.GetUserByToken(token, Random.Shared.Next(101) <= 10, tokenService.GenerateServerName(HttpContext)); if (user is null) throw new MatrixException() { @@ -36,12 +30,6 @@ public class UserController(ILogger logger, TokenService tokenSe [HttpGet("joined_rooms")] public async Task GetJoinedRooms() { var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - var user = await userStore.GetUserByToken(token, false); if (user is null) throw new MatrixException() { @@ -60,12 +48,6 @@ public class UserController(ILogger logger, TokenService tokenSe [HttpGet("devices")] public async Task GetDevices() { var token = tokenService.GetAccessToken(HttpContext); - if (token is null) - throw new MatrixException() { - ErrorCode = "M_UNAUTHORIZED", - Error = "No token passed." - }; - var user = await userStore.GetUserByToken(token, false); if (user is null) throw new MatrixException() { diff --git a/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs index 704e26b..0c3bde6 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Controllers/VersionsController.cs @@ -1,92 +1,96 @@ -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using LibMatrix.Homeservers; -using LibMatrix.Responses; -using LibMatrix.Services; -using Microsoft.AspNetCore.Mvc; - -namespace LibMatrix.HomeserverEmulator.Controllers; - -[ApiController] -[Route("/_matrix/")] -public class VersionsController(ILogger logger) : ControllerBase { - [HttpGet("client/versions")] - public async Task GetClientVersions() { - var clientVersions = new ClientVersionsResponse { - Versions = new() { - "r0.0.1", - "r0.1.0", - "r0.2.0", - "r0.3.0", - "r0.4.0", - "r0.5.0", - "r0.6.0", - "r0.6.1", - "v1.1", - "v1.2", - "v1.3", - "v1.4", - "v1.5", - "v1.6", - "v1.7", - "v1.8", - }, - UnstableFeatures = new() - }; - return clientVersions; - } - - [HttpGet("federation/v1/version")] - public async Task GetServerVersions() { - var clientVersions = new ServerVersionResponse() { - Server = new() { - Name = "LibMatrix.HomeserverEmulator", - Version = "0.0.0" - } - }; - return clientVersions; - } - - [HttpGet("client/{version}/capabilities")] - public async Task GetCapabilities() { - return new() { - Capabilities = new() { - ChangePassword = new() { - Enabled = false - }, - RoomVersions = new() { - Default = "11", - Available = new() { - ["11"] = "unstable" - } - } - } - }; - } -} - -public class CapabilitiesResponse { - [JsonPropertyName("capabilities")] - public CapabilitiesContent Capabilities { get; set; } - - public class CapabilitiesContent { - [JsonPropertyName("m.room_versions")] - public RoomVersionsContent RoomVersions { get; set; } - - [JsonPropertyName("m.change_password")] - public ChangePasswordContent ChangePassword { get; set; } - - public class ChangePasswordContent { - [JsonPropertyName("enabled")] - public bool Enabled { get; set; } - } - - public class RoomVersionsContent { - [JsonPropertyName("default")] - public string Default { get; set; } - - [JsonPropertyName("available")] - public Dictionary Available { get; set; } - } - } +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using LibMatrix.Homeservers; +using LibMatrix.Responses; +using LibMatrix.Services; +using Microsoft.AspNetCore.Mvc; + +namespace LibMatrix.HomeserverEmulator.Controllers; + +[ApiController] +[Route("/_matrix/")] +public class VersionsController(ILogger logger) : ControllerBase { + [HttpGet("client/versions")] + public async Task GetClientVersions() { + var clientVersions = new ClientVersionsResponse { + Versions = new() { + "r0.0.1", + "r0.1.0", + "r0.2.0", + "r0.3.0", + "r0.4.0", + "r0.5.0", + "r0.6.0", + "r0.6.1", + "v1.1", + "v1.2", + "v1.3", + "v1.4", + "v1.5", + "v1.6", + "v1.7", + "v1.8", + }, + UnstableFeatures = new() + }; + return clientVersions; + } + + [HttpGet("federation/v1/version")] + public async Task GetServerVersions() { + var clientVersions = new ServerVersionResponse() { + Server = new() { + Name = "LibMatrix.HomeserverEmulator", + Version = "0.0.0" + } + }; + return clientVersions; + } + + [HttpGet("client/{version}/capabilities")] + public async Task GetCapabilities() { + var caps = new CapabilitiesResponse() { + Capabilities = new() { + ChangePassword = new() { + Enabled = false + }, + RoomVersions = new() { + Default = "11", + Available = [] + } + } + }; + + for (int i = 0; i < 15; i++) { + caps.Capabilities.RoomVersions.Available.Add(i.ToString(), "stable"); + } + + return caps; + } +} + +public class CapabilitiesResponse { + [JsonPropertyName("capabilities")] + public CapabilitiesContent Capabilities { get; set; } + + public class CapabilitiesContent { + [JsonPropertyName("m.room_versions")] + public RoomVersionsContent RoomVersions { get; set; } + + [JsonPropertyName("m.change_password")] + public ChangePasswordContent ChangePassword { get; set; } + + public class ChangePasswordContent { + [JsonPropertyName("enabled")] + public bool Enabled { get; set; } + } + + public class RoomVersionsContent { + [JsonPropertyName("default")] + public string Default { get; set; } + + [JsonPropertyName("available")] + public Dictionary Available { get; set; } + } + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs b/Tests/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs index 1d03d7a..d938b1b 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Extensions/EventExtensions.cs @@ -14,5 +14,11 @@ public static class EventExtensions { OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds() }; } - + + public static List GetCalculatedState(this IEnumerable events) { + return events.Where(s => s.StateKey != null) + .OrderByDescending(s => s.OriginServerTs) + .DistinctBy(x => (x.Type, x.StateKey)) + .ToList(); + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj b/Tests/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj index 6588675..8fd5503 100644 --- a/Tests/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj +++ b/Tests/LibMatrix.HomeserverEmulator/LibMatrix.HomeserverEmulator.csproj @@ -1,25 +1,27 @@ - - - - net8.0 - enable - enable - true - preview - true - - - - - - - - - - - - - - - - + + + + net8.0 + enable + enable + true + preview + true + + + + + + + + + + + + + + + + + + diff --git a/Tests/LibMatrix.HomeserverEmulator/Program.cs b/Tests/LibMatrix.HomeserverEmulator/Program.cs index ddf39c7..91e0f88 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Program.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Program.cs @@ -1,107 +1,109 @@ -using System.Net.Mime; -using System.Text.Json.Serialization; -using LibMatrix; -using LibMatrix.HomeserverEmulator.Services; -using Microsoft.AspNetCore.Diagnostics; -using Microsoft.AspNetCore.Http.Timeouts; -using Microsoft.AspNetCore.Mvc; -using Microsoft.OpenApi.Models; - -var builder = WebApplication.CreateBuilder(args); - -// Add services to the container. - -builder.Services.AddControllers().AddJsonOptions(options => { - options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; -}); - -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(c => { - c.SwaggerDoc("v1", new OpenApiInfo() { - Version = "v1", - Title = "Rory&::LibMatrix.HomeserverEmulator", - Description = "Partial Matrix implementation" - }); - c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "LibMatrix.HomeserverEmulator.xml")); -}); - -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); -builder.Services.AddSingleton(); - -builder.Services.AddScoped(); - -builder.Services.AddRequestTimeouts(x => { - x.DefaultPolicy = new RequestTimeoutPolicy { - Timeout = TimeSpan.FromMinutes(10), - WriteTimeoutResponse = async context => { - context.Response.StatusCode = 504; - context.Response.ContentType = "application/json"; - await context.Response.StartAsync(); - await context.Response.WriteAsJsonAsync(new MatrixException() { - ErrorCode = "M_TIMEOUT", - Error = "Request timed out" - }.GetAsJson()); - await context.Response.CompleteAsync(); - } - }; -}); -builder.Services.AddCors(options => { - options.AddPolicy( - "Open", - policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); -}); -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment() || true) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseCors("Open"); -app.UseExceptionHandler(exceptionHandlerApp => { - exceptionHandlerApp.Run(async context => { - - var exceptionHandlerPathFeature = - context.Features.Get(); - if(exceptionHandlerPathFeature?.Error is not null) - Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!); - - if (exceptionHandlerPathFeature?.Error is MatrixException mxe) { - context.Response.StatusCode = mxe.ErrorCode switch { - "M_NOT_FOUND" => StatusCodes.Status404NotFound, - "M_UNAUTHORIZED" => StatusCodes.Status401Unauthorized, - _ => StatusCodes.Status500InternalServerError - }; - context.Response.ContentType = MediaTypeNames.Application.Json; - await context.Response.WriteAsync(mxe.GetAsJson()!); - } - else { - context.Response.StatusCode = StatusCodes.Status500InternalServerError; - context.Response.ContentType = MediaTypeNames.Application.Json; - await context.Response.WriteAsync(new MatrixException() { - ErrorCode = "M_UNKNOWN", - Error = exceptionHandlerPathFeature?.Error.ToString() - }.GetAsJson()); - } - }); -}); - -app.UseAuthorization(); - -app.MapControllers(); - -app.Map("/_matrix/{*_}", (HttpContext ctx) => { - Console.WriteLine($"Client hit non-existing route: {ctx.Request.Method} {ctx.Request.Path}"); - ctx.Response.StatusCode = StatusCodes.Status404NotFound; - ctx.Response.ContentType = MediaTypeNames.Application.Json; - return ctx.Response.WriteAsJsonAsync(new MatrixException() { - ErrorCode = MatrixException.ErrorCodes.M_UNRECOGNISED, - Error = "Endpoint not implemented" - }); -}); - -app.Run(); +using System.Net.Mime; +using System.Text.Json.Serialization; +using LibMatrix; +using LibMatrix.HomeserverEmulator.Services; +using LibMatrix.Services; +using Microsoft.AspNetCore.Diagnostics; +using Microsoft.AspNetCore.Http.Timeouts; +using Microsoft.AspNetCore.Mvc; +using Microsoft.OpenApi.Models; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; }); + +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(c => { + c.SwaggerDoc("v1", new OpenApiInfo() { + Version = "v1", + Title = "Rory&::LibMatrix.HomeserverEmulator", + Description = "Partial Matrix homeserver implementation" + }); + c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "LibMatrix.HomeserverEmulator.xml")); +}); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddScoped(); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddRequestTimeouts(x => { + x.DefaultPolicy = new RequestTimeoutPolicy { + Timeout = TimeSpan.FromMinutes(10), + WriteTimeoutResponse = async context => { + context.Response.StatusCode = 504; + context.Response.ContentType = "application/json"; + await context.Response.StartAsync(); + await context.Response.WriteAsJsonAsync(new MatrixException() { + ErrorCode = "M_TIMEOUT", + Error = "Request timed out" + }.GetAsJson()); + await context.Response.CompleteAsync(); + } + }; +}); +builder.Services.AddCors(options => { + options.AddPolicy( + "Open", + policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod()); +}); +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment() || true) { + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseCors("Open"); +app.UseExceptionHandler(exceptionHandlerApp => { + exceptionHandlerApp.Run(async context => { + var exceptionHandlerPathFeature = + context.Features.Get(); + if (exceptionHandlerPathFeature?.Error is not null) + Console.WriteLine(exceptionHandlerPathFeature.Error.ToString()!); + + if (exceptionHandlerPathFeature?.Error is MatrixException mxe) { + context.Response.StatusCode = mxe.ErrorCode switch { + "M_NOT_FOUND" => StatusCodes.Status404NotFound, + "M_UNAUTHORIZED" => StatusCodes.Status401Unauthorized, + _ => StatusCodes.Status500InternalServerError + }; + context.Response.ContentType = MediaTypeNames.Application.Json; + await context.Response.WriteAsync(mxe.GetAsJson()!); + } + else { + context.Response.StatusCode = StatusCodes.Status500InternalServerError; + context.Response.ContentType = MediaTypeNames.Application.Json; + await context.Response.WriteAsync(new MatrixException() { + ErrorCode = "M_UNKNOWN", + Error = exceptionHandlerPathFeature?.Error.ToString() + }.GetAsJson()); + } + }); +}); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Map("/_matrix/{*_}", (HttpContext ctx, ILogger logger) => { + logger.LogWarning("Client hit non-existing route: {Method} {Path}{Query}", ctx.Request.Method, ctx.Request.Path, ctx.Request.Query); + // Console.WriteLine($"Client hit non-existing route: {ctx.Request.Method} {ctx.Request.Path}"); + ctx.Response.StatusCode = StatusCodes.Status404NotFound; + ctx.Response.ContentType = MediaTypeNames.Application.Json; + return ctx.Response.WriteAsJsonAsync(new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNRECOGNISED, + Error = "Endpoint not implemented" + }); +}); + +app.Run(); \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs index e40af89..00f2a42 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/MediaStore.cs @@ -1,29 +1,20 @@ -using System.Collections.Concurrent; -using System.Collections.Frozen; -using System.Collections.ObjectModel; -using System.Security.Cryptography; using System.Text.Json; -using System.Text.Json.Nodes; -using System.Text.Json.Serialization; -using ArcaneLibs; -using ArcaneLibs.Collections; -using ArcaneLibs.Extensions; -using LibMatrix.EventTypes; -using LibMatrix.EventTypes.Spec.State; -using LibMatrix.Responses; +using LibMatrix.Services; namespace LibMatrix.HomeserverEmulator.Services; public class MediaStore { private readonly HSEConfiguration _config; + private readonly HomeserverResolverService _hsResolver; private List index = new(); - public MediaStore(HSEConfiguration config) { + public MediaStore(HSEConfiguration config, HomeserverResolverService hsResolver) { _config = config; + _hsResolver = hsResolver; if (config.StoreData) { var path = Path.Combine(config.DataStoragePath, "media"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); - if(File.Exists(Path.Combine(path, "index.json"))) + if (File.Exists(Path.Combine(path, "index.json"))) index = JsonSerializer.Deserialize>(File.ReadAllText(Path.Combine(path, "index.json"))); } else @@ -31,17 +22,43 @@ public class MediaStore { } // public async Task UploadMedia(string userId, string mimeType, Stream stream, string? filename = null) { - // var mediaId = $"mxc://{Guid.NewGuid().ToString()}"; - // var path = Path.Combine(_config.DataStoragePath, "media", mediaId); - // if (!Directory.Exists(path)) Directory.CreateDirectory(path); - // var file = Path.Combine(path, filename ?? "file"); - // await using var fs = File.Create(file); - // await stream.CopyToAsync(fs); - // index.Add(new() { - // - // }); - // return media; + // var mediaId = $"mxc://{Guid.NewGuid().ToString()}"; + // var path = Path.Combine(_config.DataStoragePath, "media", mediaId); + // if (!Directory.Exists(path)) Directory.CreateDirectory(path); + // var file = Path.Combine(path, filename ?? "file"); + // await using var fs = File.Create(file); + // await stream.CopyToAsync(fs); + // index.Add(new() { }); + // return media; // } + public async Task GetRemoteMedia(string serverName, string mediaId) { + if (_config.StoreData) { + var path = Path.Combine(_config.DataStoragePath, "media", serverName, mediaId); + if (!File.Exists(path)) { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + var stream = await client.GetStreamAsync(mediaUrl); + await using var fs = File.Create(path); + await stream.CopyToAsync(fs); + } + return new FileStream(path, FileMode.Open); + } + else { + var mediaUrl = await _hsResolver.ResolveMediaUri(serverName, $"mxc://{serverName}/{mediaId}"); + if (mediaUrl is null) + throw new MatrixException() { + ErrorCode = "M_NOT_FOUND", + Error = "Media not found" + }; + using var client = new HttpClient(); + return await client.GetStreamAsync(mediaUrl); + } + } public class MediaInfo { } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs b/Tests/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs new file mode 100644 index 0000000..0128ba6 --- /dev/null +++ b/Tests/LibMatrix.HomeserverEmulator/Services/PaginationTokenResolverService.cs @@ -0,0 +1,54 @@ +namespace LibMatrix.HomeserverEmulator.Services; + +public class PaginationTokenResolverService(ILogger logger, RoomStore roomStore, UserStore userStore) { + public async Task ResolveTokenToTimestamp(string token) { + logger.LogTrace("ResolveTokenToTimestamp({token})", token); + if (token.StartsWith('$')) { + //we have an event ID + foreach (var room in roomStore._rooms) { + var evt = await ResolveTokenToEvent(token, room); + if (evt is not null) return evt.OriginServerTs; + } + + // event not found + throw new NotImplementedException(); + } + else { + // we have a sync token + foreach (var user in userStore._users) { + foreach (var (_, session) in user.AccessTokens) { + if (!session.SyncStates.TryGetValue(token, out var syncState)) continue; + long? maxTs = 0; + foreach (var room in syncState.RoomPositions) { + var roomObj = roomStore.GetRoomById(room.Key); + if (roomObj is null) + continue; + var ts = roomObj.Timeline.Last().OriginServerTs; + if (ts > maxTs) maxTs = ts; + } + + return maxTs; + } + } + + throw new NotImplementedException(); + } + } + + public async Task ResolveTokenToEvent(string token, RoomStore.Room room) { + if (token.StartsWith('$')) { + //we have an event ID + logger.LogTrace("ResolveTokenToEvent(EventId({token}), Room({room})): searching for event...", token, room.RoomId); + + var evt = room.Timeline.SingleOrDefault(x => x.EventId == token); + if (evt is not null) return evt; + logger.LogTrace("ResolveTokenToEvent({token}, Room({room})): event not in requested room...", token, room.RoomId); + return null; + } + else { + // we have a sync token + logger.LogTrace("ResolveTokenToEvent(SyncToken({token}), Room({room}))", token, room.RoomId); + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs index d2c9e15..7b36f37 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/RoomStore.cs @@ -3,24 +3,24 @@ using System.Collections.Frozen; using System.Collections.Immutable; using System.Collections.ObjectModel; using System.Collections.Specialized; -using System.Security.Cryptography; using System.Text.Json; using System.Text.Json.Nodes; -using System.Text.Json.Serialization; using ArcaneLibs; using ArcaneLibs.Collections; using ArcaneLibs.Extensions; -using LibMatrix.EventTypes; using LibMatrix.EventTypes.Spec.State; +using LibMatrix.HomeserverEmulator.Controllers.Rooms; using LibMatrix.Responses; namespace LibMatrix.HomeserverEmulator.Services; public class RoomStore { + private readonly ILogger _logger; public ConcurrentBag _rooms = new(); private Dictionary _roomsById = new(); - public RoomStore(HSEConfiguration config) { + public RoomStore(ILogger logger, HSEConfiguration config) { + _logger = logger; if (config.StoreData) { var path = Path.Combine(config.DataStoragePath, "rooms"); if (!Directory.Exists(path)) Directory.CreateDirectory(path); @@ -50,8 +50,19 @@ public class RoomStore { return CreateRoom(new() { }); } - public Room CreateRoom(CreateRoomRequest request) { + public Room CreateRoom(CreateRoomRequest request, UserStore.User? user = null) { var room = new Room(roomId: $"!{Guid.NewGuid().ToString()}"); + var newCreateEvent = new StateEvent() { + Type = RoomCreateEventContent.EventId, + RawContent = new() { } + }; + foreach (var (key, value) in request.CreationContent) { + newCreateEvent.RawContent[key] = value.DeepClone(); + } + + if (user != null) newCreateEvent.RawContent["creator"] = user.UserId; + var createEvent = room.SetStateInternal(newCreateEvent); + if (!string.IsNullOrWhiteSpace(request.Name)) room.SetStateInternal(new StateEvent() { Type = RoomNameEventContent.EventId, @@ -77,23 +88,33 @@ public class RoomStore { return room; } + public Room AddRoom(Room room) { + _rooms.Add(room); + RebuildIndexes(); + + return room; + } + public class Room : NotifyPropertyChanged { private CancellationTokenSource _debounceCts = new(); private ObservableCollection _timeline; private ObservableDictionary> _accountData; + private ObservableDictionary _readMarkers; + private FrozenSet _stateCache; + private int _timelineHash; public Room(string roomId) { if (string.IsNullOrWhiteSpace(roomId)) throw new ArgumentException("Value cannot be null or whitespace.", nameof(roomId)); if (roomId[0] != '!') throw new ArgumentException("Room ID must start with !", nameof(roomId)); RoomId = roomId; - State = FrozenSet.Empty; Timeline = new(); AccountData = new(); + ReadMarkers = new(); } public string RoomId { get; set; } - public FrozenSet State { get; private set; } + public FrozenSet State => _timelineHash == _timeline.GetHashCode() ? _stateCache : RebuildState(); public ObservableCollection Timeline { get => _timeline; @@ -129,11 +150,21 @@ public class RoomStore { public ImmutableList JoinedMembers => State.Where(s => s is { Type: RoomMemberEventContent.EventId, TypedContent: RoomMemberEventContent { Membership: "join" } }).ToImmutableList(); + public ObservableDictionary ReadMarkers { + get => _readMarkers; + set { + if (Equals(value, _readMarkers)) return; + _readMarkers = new(value); + _readMarkers.CollectionChanged += (sender, args) => SaveDebounced(); + OnPropertyChanged(); + } + } + internal StateEventResponse SetStateInternal(StateEvent request) { var state = new StateEventResponse() { Type = request.Type, - StateKey = request.StateKey, - EventId = Guid.NewGuid().ToString(), + StateKey = request.StateKey ?? "", + EventId = "$" + Guid.NewGuid().ToString(), RoomId = RoomId, OriginServerTs = DateTimeOffset.Now.ToUnixTimeMilliseconds(), Sender = "", @@ -142,12 +173,12 @@ public class RoomStore { : JsonSerializer.Deserialize(JsonSerializer.Serialize(request.TypedContent))) }; Timeline.Add(state); - if (state.StateKey is not null) - // we want state to be deduplicated by type and key, and we want the latest state to be the one that is returned - State = Timeline.Where(s => s.StateKey != null) - .OrderByDescending(s => s.OriginServerTs) - .DistinctBy(x => (x.Type, x.StateKey)) - .ToFrozenSet(); + // if (state.StateKey is not null) + // we want state to be deduplicated by type and key, and we want the latest state to be the one that is returned + // State = Timeline.Where(s => s.StateKey != null) + // .OrderByDescending(s => s.OriginServerTs) + // .DistinctBy(x => (x.Type, x.StateKey)) + // .ToFrozenSet(); return state; } @@ -179,12 +210,32 @@ public class RoomStore { catch (TaskCanceledException) { } } - private void RebuildState() { - State = Timeline //.Where(s => s.Type == state.Type && s.StateKey == state.StateKey) + private FrozenSet RebuildState() { + foreach (var evt in Timeline) { + if (evt.EventId == null) + evt.EventId = "$" + Guid.NewGuid(); + else if (!evt.EventId.StartsWith('$')) { + evt.EventId = "$" + evt.EventId; + Console.WriteLine($"Sanitised invalid event ID {evt.EventId}"); + } + } + + _stateCache = Timeline //.Where(s => s.Type == state.Type && s.StateKey == state.StateKey) .Where(x => x.StateKey != null) .OrderByDescending(s => s.OriginServerTs) .DistinctBy(x => (x.Type, x.StateKey)) .ToFrozenSet(); + + return _stateCache; } } + + public List GetRoomsByMember(string userId) { + // return _rooms + // // .Where(r => r.State.Any(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId)) + // .Select(r => (Room: r, MemberEvent: r.State.SingleOrDefault(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId))) + // .Where(r => r.MemberEvent != null) + // .ToDictionary(x => x.Room, x => x.MemberEvent!); + return _rooms.SelectMany(r => r.State.Where(s => s.Type == RoomMemberEventContent.EventId && s.StateKey == userId)).ToList(); + } } \ No newline at end of file diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs b/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs index 1f59342..cf79aae 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/TokenService.cs @@ -1,7 +1,7 @@ namespace LibMatrix.HomeserverEmulator.Services; public class TokenService{ - public string? GetAccessToken(HttpContext ctx) { + public string? GetAccessTokenOrNull(HttpContext ctx) { //qry if (ctx.Request.Query.TryGetValue("access_token", out var token)) { return token; @@ -16,6 +16,13 @@ public class TokenService{ return null; } + public string GetAccessToken(HttpContext ctx) { + return GetAccessTokenOrNull(ctx) ?? throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNKNOWN_TOKEN, + Error = "Missing token" + }; + } + public string? GenerateServerName(HttpContext ctx) { return ctx.Request.Host.ToString(); } diff --git a/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs b/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs index 4f238a0..4ce9f92 100644 --- a/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs +++ b/Tests/LibMatrix.HomeserverEmulator/Services/UserStore.cs @@ -18,11 +18,15 @@ public class UserStore { public UserStore(HSEConfiguration config, RoomStore roomStore) { _roomStore = roomStore; if (config.StoreData) { - var path = Path.Combine(config.DataStoragePath, "users"); - if (!Directory.Exists(path)) Directory.CreateDirectory(path); - foreach (var file in Directory.GetFiles(path)) { - var user = JsonSerializer.Deserialize(File.ReadAllText(file)); - if (user is not null) _users.Add(user); + var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users"); + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); + foreach (var userId in Directory.GetDirectories(dataDir)) { + var tokensDir = Path.Combine(dataDir, userId, "tokens.json"); + var path = Path.Combine(dataDir, userId, $"user.json"); + + var user = JsonSerializer.Deserialize(File.ReadAllText(path)); + user!.AccessTokens = JsonSerializer.Deserialize>(File.ReadAllText(tokensDir))!; + _users.Add(user); } Console.WriteLine($"Loaded {_users.Count} users from disk"); @@ -42,7 +46,7 @@ public class UserStore { return await CreateUser(userId); } - public async Task GetUserByToken(string token, bool createIfNotExists = false, string? serverName = null) { + public async Task GetUserByTokenOrNull(string token, bool createIfNotExists = false, string? serverName = null) { if (_users.Any(x => x.AccessTokens.ContainsKey(token))) return _users.First(x => x.AccessTokens.ContainsKey(token)); @@ -53,6 +57,13 @@ public class UserStore { return await CreateUser(uid); } + public async Task GetUserByToken(string token, bool createIfNotExists = false, string? serverName = null) { + return await GetUserByTokenOrNull(token, createIfNotExists, serverName) ?? throw new MatrixException() { + ErrorCode = MatrixException.ErrorCodes.M_UNKNOWN_TOKEN, + Error = "Invalid token." + }; + } + public async Task CreateUser(string userId, Dictionary? profile = null) { profile ??= new(); if (!profile.ContainsKey("displayname")) profile.Add("displayname", userId.Split(":")[0]); @@ -97,6 +108,7 @@ public class UserStore { Profile = new(); AccountData = new(); RoomKeys = new(); + AuthorizedSessions = new(); } private CancellationTokenSource _debounceCts = new(); @@ -106,6 +118,7 @@ public class UserStore { private ObservableDictionary _profile; private ObservableCollection _accountData; private ObservableDictionary _roomKeys; + private ObservableDictionary _authorizedSessions; public string UserId { get => _userId; @@ -162,15 +175,29 @@ public class UserStore { } } + public ObservableDictionary AuthorizedSessions { + get => _authorizedSessions; + set { + if (value == _authorizedSessions) return; + _authorizedSessions = new(value); + _authorizedSessions.CollectionChanged += async (sender, args) => await SaveDebounced(); + OnPropertyChanged(); + } + } + public async Task SaveDebounced() { if (!HSEConfiguration.Current.StoreData) return; - _debounceCts.Cancel(); + await _debounceCts.CancelAsync(); _debounceCts = new CancellationTokenSource(); try { await Task.Delay(250, _debounceCts.Token); - var path = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users", $"{_userId}.json"); + var dataDir = Path.Combine(HSEConfiguration.Current.DataStoragePath, "users", _userId); + if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir); + var tokensDir = Path.Combine(dataDir, "tokens.json"); + var path = Path.Combine(dataDir, $"user.json"); Console.WriteLine($"Saving user {_userId} to {path}!"); await File.WriteAllTextAsync(path, this.ToJson(ignoreNull: true)); + await File.WriteAllTextAsync(tokensDir, AccessTokens.ToJson(ignoreNull: true)); } catch (TaskCanceledException) { } catch (InvalidOperationException) { } // We don't care about 100% data safety, this usually happens when something is updated while serialising @@ -187,7 +214,19 @@ public class UserStore { public class SyncRoomPosition { public int TimelinePosition { get; set; } + public string LastTimelineEventId { get; set; } public int AccountDataPosition { get; set; } + public bool Joined { get; set; } + } + + public UserSyncState Clone() { + return new() { + FilterId = FilterId, + RoomPositions = RoomPositions.ToDictionary(x => x.Key, x => new SyncRoomPosition() { + TimelinePosition = x.Value.TimelinePosition, + AccountDataPosition = x.Value.AccountDataPosition + }) + }; } } } @@ -202,5 +241,10 @@ public class UserStore { UserId = UserId }; } + + public class AuthorizedSession { + public string Homeserver { get; set; } + public string AccessToken { get; set; } + } } } \ No newline at end of file diff --git a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs index 6f22c03..601e598 100644 --- a/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs +++ b/Utilities/LibMatrix.Utilities.Bot/Services/CommandListenerHostedService.cs @@ -97,21 +97,23 @@ public class CommandListenerHostedService : IHostedService { if (prefix is null && _config.MentionPrefix) { var profile = await _hs.GetProfileAsync(_hs.WhoAmI.UserId); var roomProfile = await _hs.GetRoom(evt.RoomId!).GetStateAsync(RoomMemberEventContent.EventId, _hs.WhoAmI.UserId); - if(message.StartsWith(_hs.WhoAmI.UserId + ": ")) prefix = profile.DisplayName + ": "; // `@bot:server.xyz: ` + if (message.StartsWith(_hs.WhoAmI.UserId + ": ")) prefix = profile.DisplayName + ": "; // `@bot:server.xyz: ` else if (message.StartsWith(_hs.WhoAmI.UserId + " ")) prefix = profile.DisplayName + " "; // `@bot:server.xyz ` - else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + ": ")) prefix = roomProfile.DisplayName + ": "; // `local bot: ` - else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + " ")) prefix = roomProfile.DisplayName + " "; // `local bot ` + else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + ": ")) + prefix = roomProfile.DisplayName + ": "; // `local bot: ` + else if (!string.IsNullOrWhiteSpace(roomProfile?.DisplayName) && message.StartsWith(roomProfile.DisplayName + " ")) + prefix = roomProfile.DisplayName + " "; // `local bot ` else if (!string.IsNullOrWhiteSpace(profile.DisplayName) && message.StartsWith(profile.DisplayName + ": ")) prefix = profile.DisplayName + ": "; // `bot: ` - else if (!string.IsNullOrWhiteSpace(profile.DisplayName) && message.StartsWith(profile.DisplayName + " ")) prefix = profile.DisplayName + " "; // `bot ` + else if (!string.IsNullOrWhiteSpace(profile.DisplayName) && message.StartsWith(profile.DisplayName + " ")) prefix = profile.DisplayName + " "; // `bot ` } return prefix; } - + private async Task InvokeCommand(StateEventResponse evt, string usedPrefix) { var message = evt.TypedContent as RoomMessageEventContent; var room = _hs.GetRoom(evt.RoomId!); - + var commandWithoutPrefix = message.BodyWithoutReplyFallback[usedPrefix.Length..].Trim(); var ctx = new CommandContext { Room = room, @@ -132,7 +134,6 @@ public class CommandListenerHostedService : IHostedService { }; } - if (await command.CanInvoke(ctx)) try { await command.Invoke(ctx); @@ -181,7 +182,7 @@ public class CommandListenerHostedService : IHostedService { CommandResult.CommandResultType.Failure_InvalidCommand => new RoomMessageEventContent("m.notice", $"Command \"{res.Context.CommandName}\" not found!"), _ => throw new ArgumentOutOfRangeException() }; - + await room.SendMessageEventAsync(msg); } } \ No newline at end of file -- cgit 1.4.1