From cd214bcd538fc1e89d5734a1c5262509e347d359 Mon Sep 17 00:00:00 2001 From: Rory& Date: Sun, 5 Jul 2026 22:25:52 +0200 Subject: Helpers for managing room account data --- .../Homeservers/AuthenticatedHomeserverGeneric.cs | 35 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs') diff --git a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs index b453d87..b1e655d 100644 --- a/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs +++ b/LibMatrix/Homeservers/AuthenticatedHomeserverGeneric.cs @@ -186,6 +186,34 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver { } } + public virtual async Task GetRoomAccountDataAsync(string roomId, string key) => + // var res = await _httpClient.GetAsync($"/_matrix/client/v3/user/{UserId}/account_data/{key}"); + // if (!res.IsSuccessStatusCode) { + // Console.WriteLine($"Failed to get account data: {await res.Content.ReadAsStringAsync()}"); + // throw new InvalidDataException($"Failed to get account data: {await res.Content.ReadAsStringAsync()}"); + // } + // + // return await res.Content.ReadFromJsonAsync(); + await ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/user/{WhoAmI.UserId}/rooms/{HttpUtility.UrlEncode(roomId)}/account_data/{key}"); + + public virtual async Task GetRoomAccountDataOrNullAsync(string roomId, string key) { + try { + return await GetRoomAccountDataAsync(roomId, key); + } + catch (MatrixException e) { + if (e is { ErrorCode: MatrixException.ErrorCodes.M_NOT_FOUND }) return default; + throw; + } + } + + public virtual async Task SetRoomAccountDataAsync(string roomId, string key, object data) { + var res = await ClientHttpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{WhoAmI.UserId}/rooms/{HttpUtility.UrlEncode(roomId)}/account_data/{key}", data); + if (!res.IsSuccessStatusCode) { + Console.WriteLine($"Failed to set account data: {await res.Content.ReadAsStringAsync()}"); + throw new InvalidDataException($"Failed to set account data: {await res.Content.ReadAsStringAsync()}"); + } + } + #endregion #region MSC 4133 @@ -378,11 +406,14 @@ public class AuthenticatedHomeserverGeneric : RemoteHomeserver { /// Warning: This uses /sync! /// /// Include non-room account data + /// Scope sync request to given room ID(s) /// Dictionary of room IDs and their account data. /// - public async Task> EnumerateAccountDataPerRoom(bool includeGlobal = false) { + public async Task> EnumerateAccountDataPerRoom(bool includeGlobal = false, List? rooms = null) { var syncHelper = new SyncHelper(this); - syncHelper.FilterId = await NamedCaches.FilterCache.GetOrSetValueAsync(CommonSyncFilters.GetAccountDataWithRooms); + syncHelper.FilterId = rooms == null + ? await NamedCaches.FilterCache.GetOrSetValueAsync(CommonSyncFilters.GetAccountDataWithRooms) + : (await UploadFilterAsync(CommonSyncFilters.GetAccountDataForRoomsFilter(rooms))).FilterId; var resp = await syncHelper.SyncAsync(); if (resp is null) throw new Exception("Sync failed"); var perRoomAccountData = new Dictionary(); -- cgit 1.5.1