From f6b66d9f5bffc0c22ca49a0881c0576a9da9fe01 Mon Sep 17 00:00:00 2001 From: Rory& Date: Wed, 31 Jan 2024 12:16:09 +0100 Subject: Capabilities --- .../Homeservers/AuthenticatedHomeserverGeneric.cs | 31 +++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) (limited to 'LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs') 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($"/_matrix/client/v3/user/{WhoAmI.UserId}/account_data/{key}"); } + public virtual async Task GetAccountDataOrNullAsync(string key) { + try { + return await GetAccountDataAsync(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(); - - 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? _namedFilterCache; private Dictionary _filterCache = new(); + + public async Task 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(); + } } \ No newline at end of file -- cgit 1.4.1