diff options
author | Rory& <root@rory.gay> | 2024-01-31 12:16:09 +0100 |
---|---|---|
committer | Rory& <root@rory.gay> | 2024-01-31 12:18:29 +0100 |
commit | f6b66d9f5bffc0c22ca49a0881c0576a9da9fe01 (patch) | |
tree | 0c687db39e1628cb27603e0a9a5520bda6108175 | |
parent | Better sync filter support, named filters, error handling (diff) | |
download | LibMatrix-bak-f6b66d9f5bffc0c22ca49a0881c0576a9da9fe01.tar.xz |
Capabilities
-rw-r--r-- | LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs | 31 | ||||
-rw-r--r-- | LibMatrix/Responses/CreateRoomRequest.cs | 3 |
2 files changed, 28 insertions, 6 deletions
diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index ef6fa68..134ec11 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -161,6 +161,15 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke return await ClientHttpClient.GetFromJsonAsync<T>($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}"); } + public virtual async Task<T?> GetAccountDataOrNullAsync<T>(string key) { + try { + return await GetAccountDataAsync<T>(key); + } + catch (Exception e) { + return default; + } + } + public virtual async Task SetAccountDataAsync(string key, object data) { var res = await ClientHttpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}", data); if (!res.IsSuccessStatusCode) { @@ -344,7 +353,7 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke var filterList = await GetNamedFilterListOrNullAsync() ?? new(); filterList[filterName] = idResp.FilterId; await SetAccountDataAsync("gay.rory.libmatrix.named_filters", filterList); - + _namedFilterCache = filterList; return idResp; @@ -381,10 +390,10 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke var syncHelper = new SyncHelper(this); syncHelper.FilterId = await GetOrUploadNamedFilterIdAsync(CommonSyncFilters.GetAccountDataWithRooms); var resp = await syncHelper.SyncAsync(); - if(resp is null) throw new Exception("Sync failed"); + if (resp is null) throw new Exception("Sync failed"); var perRoomAccountData = new Dictionary<string, EventList?>(); - - if(includeGlobal) + + if (includeGlobal) perRoomAccountData[""] = resp.AccountData; foreach (var (roomId, room) in resp.Rooms?.Join ?? []) { perRoomAccountData[roomId] = room.AccountData; @@ -397,10 +406,20 @@ public class AuthenticatedHomeserverGeneric(string serverName, string accessToke var syncHelper = new SyncHelper(this); syncHelper.FilterId = await GetOrUploadNamedFilterIdAsync(CommonSyncFilters.GetAccountData); var resp = await syncHelper.SyncAsync(); - if(resp is null) throw new Exception("Sync failed"); + if (resp is null) throw new Exception("Sync failed"); return resp.AccountData; } - + private Dictionary<string, string>? _namedFilterCache; private Dictionary<string, SyncFilter> _filterCache = new(); + + public async Task<JsonObject?> GetCapabilitiesAsync() { + var res = await ClientHttpClient.GetAsync("/_matrix/client/v3/capabilities"); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to get capabilities: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to get capabilities: {await res.Content.ReadAsStringAsync()}"); + } + + return await res.Content.ReadFromJsonAsync<JsonObject>(); + } } \ No newline at end of file diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs index 3b93cf7..9a797b5 100644 --- a/LibMatrix/Responses/CreateRoomRequest.cs +++ b/LibMatrix/Responses/CreateRoomRequest.cs @@ -17,6 +17,9 @@ public class CreateRoomRequest { [JsonPropertyName("name")] public string? Name { get; set; } + /// <summary> + /// Room alias local name. Must be unique on the homeserver. + /// </summary> [JsonPropertyName("room_alias_name")] public string? RoomAliasName { get; set; } |