From 1cbcf84174f8fdbd021f8e16466d2784e8fdf38c Mon Sep 17 00:00:00 2001 From: Rory& Date: Fri, 4 Oct 2024 19:46:45 +0200 Subject: Minor cleanups, support for loading access tokens from disk or appservice --- LibMatrix/RoomTypes/GenericRoom.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'LibMatrix/RoomTypes') diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index 2ec8571..8398ab9 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -431,6 +431,16 @@ public class GenericRoom { return await res.Content.ReadFromJsonAsync(); } + + public async Task GetRoomAccountDataOrNullAsync(string key) { + try { + return await GetRoomAccountDataAsync(key); + } + catch (MatrixException e) { + if (e.ErrorCode == "M_NOT_FOUND") return default; + throw; + } + } public async Task SetRoomAccountDataAsync(string key, object data) { var res = await Homeserver.ClientHttpClient.PutAsJsonAsync($"/_matrix/client/v3/user/{Homeserver.UserId}/rooms/{RoomId}/account_data/{key}", data); @@ -443,10 +453,17 @@ public class GenericRoom { public Task GetEventAsync(string eventId) => Homeserver.ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); - public async Task RedactEventAsync(string eventToRedact, string reason) { + public async Task RedactEventAsync(string eventToRedact, string? reason = null) { var data = new { reason }; - return (await (await Homeserver.ClientHttpClient.PutAsJsonAsync( - $"/_matrix/client/v3/rooms/{RoomId}/redact/{eventToRedact}/{Guid.NewGuid()}", data)).Content.ReadFromJsonAsync())!; + var url = $"/_matrix/client/v3/rooms/{RoomId}/redact/{eventToRedact}/{Guid.NewGuid().ToString()}"; + while (true) { + try { + return (await (await Homeserver.ClientHttpClient.PutAsJsonAsync(url, data)).Content.ReadFromJsonAsync())!; + } catch (MatrixException e) { + if (e is { ErrorCode: MatrixException.ErrorCodes.M_FORBIDDEN }) throw; + throw; + } + } } #endregion -- cgit 1.5.1