about summary refs log tree commit diff
path: root/LibMatrix/Homeservers/RemoteHomeServer.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LibMatrix/Homeservers/RemoteHomeServer.cs80
1 files changed, 70 insertions, 10 deletions
diff --git a/LibMatrix/Homeservers/RemoteHomeServer.cs b/LibMatrix/Homeservers/RemoteHomeServer.cs

index af84be2..54f5937 100644 --- a/LibMatrix/Homeservers/RemoteHomeServer.cs +++ b/LibMatrix/Homeservers/RemoteHomeServer.cs
@@ -23,7 +23,6 @@ public class RemoteHomeserver { // Timeout = TimeSpan.FromSeconds(300) // TODO: Re-implement this }; - if (proxy is not null) ClientHttpClient.DefaultRequestHeaders.Add("MXAE_UPSTREAM", serverName); if (!string.IsNullOrWhiteSpace(wellKnownUris.Server)) FederationClient = new FederationClient(WellKnownUris.Server!, proxy); Auth = new(this); @@ -61,29 +60,54 @@ public class RemoteHomeserver { return data ?? throw new InvalidOperationException($"Could not resolve alias {alias}"); } - public Task<PublicRoomDirectoryResult> GetPublicRoomsAsync(int limit = 100, string? server = null, string? since = null) { - var url = $"/_matrix/client/v3/publicRooms?limit={limit}"; - if (!string.IsNullOrWhiteSpace(server)) { - url += $"&server={server}"; + public async Task<PublicRoomDirectoryResult> GetPublicRoomsAsync(int limit = 100, string? server = null, string? since = null, string? thirdPartyInstanceId = null, + bool? includeAllNetworks = null, RoomDirectoryFilter? filter = null) { + if (thirdPartyInstanceId is null && includeAllNetworks is null && filter is null) { + var url = $"/_matrix/client/v3/publicRooms?limit={limit}"; + if (!string.IsNullOrWhiteSpace(server)) { + url += $"&server={server}"; + } + + if (!string.IsNullOrWhiteSpace(since)) { + url += $"&since={since}"; + } + + return await ClientHttpClient.GetFromJsonAsync<PublicRoomDirectoryResult>(url); } - if (!string.IsNullOrWhiteSpace(since)) { - url += $"&since={since}"; + // this technically requires authentication... TODO: move to AuthenticatedHomeserver? + var postUrl = "/_matrix/client/v3/publicRooms"; + if (!string.IsNullOrWhiteSpace(server)) { + postUrl += $"?server={HttpUtility.UrlEncode(server)}"; } - return ClientHttpClient.GetFromJsonAsync<PublicRoomDirectoryResult>(url); + var postData = new RoomDirectoryFilteredRequest { + Limit = limit, + Since = since, + ThirdPartyInstanceId = thirdPartyInstanceId, + IncludeAllNetworks = includeAllNetworks, + Filter = filter + }; + + return await (await ClientHttpClient.PostAsJsonAsync(postUrl, postData)).EnsureSuccessStatusCode() + .Content.ReadFromJsonAsync<PublicRoomDirectoryResult>() ?? throw new InvalidOperationException(); } - public async IAsyncEnumerable<PublicRoomDirectoryResult> EnumeratePublicRoomsAsync(int limit = int.MaxValue, string? server = null, string? since = null, int chunkSize = 100) { + public async IAsyncEnumerable<PublicRoomDirectoryResult> EnumeratePublicRoomsAsync(int limit = int.MaxValue, string? server = null, string? since = null, + string? thirdPartyInstanceId = null, bool? includeAllNetworks = null, RoomDirectoryFilter? filter = null, int chunkSize = 100) { PublicRoomDirectoryResult res; do { - res = await GetPublicRoomsAsync(chunkSize, server, since); + res = await GetPublicRoomsAsync(chunkSize, server, since, thirdPartyInstanceId, includeAllNetworks, filter); yield return res; if (res.NextBatch is null || res.NextBatch == since || res.Chunk.Count == 0) break; since = res.NextBatch; } while (limit > 0 && limit-- > 0); } + public async Task<RoomDirectoryVisibilityResponse> GetRoomDirectoryVisibilityAsync(string roomId) + => await (await ClientHttpClient.GetAsync($"/_matrix/client/v3/directory/list/room/{HttpUtility.UrlEncode(roomId)}")).Content + .ReadFromJsonAsync<RoomDirectoryVisibilityResponse>() ?? throw new InvalidOperationException(); + #region Authentication public async Task<LoginResponse> LoginAsync(string username, string password, string? deviceName = null) { @@ -121,6 +145,42 @@ public class RemoteHomeserver { public UserInteractiveAuthClient Auth; } +public class RoomDirectoryFilteredRequest { + [JsonPropertyName("filter")] + public RoomDirectoryFilter? Filter { get; set; } + + [JsonPropertyName("include_all_networks")] + public bool? IncludeAllNetworks { get; set; } + + [JsonPropertyName("limit")] + public int Limit { get; set; } + + [JsonPropertyName("since")] + public string? Since { get; set; } + + [JsonPropertyName("third_party_instance_id")] + public string? ThirdPartyInstanceId { get; set; } +} + +public class RoomDirectoryFilter { + [JsonPropertyName("generic_search_term")] + public string? GenericSearchTerm { get; set; } + + [JsonPropertyName("room_types")] + public List<string?>? RoomTypes { get; set; } +} + +public class RoomDirectoryVisibilityResponse { + [JsonPropertyName("visibility")] + public VisibilityValue Visibility { get; set; } + + [JsonConverter(typeof(JsonStringEnumConverter))] + public enum VisibilityValue { + [JsonStringEnumMemberName("public")] Public, + [JsonStringEnumMemberName("private")] Private + } +} + public class PublicRoomDirectoryResult { [JsonPropertyName("chunk")] public List<PublicRoomListItem> Chunk { get; set; }