From b7dbc011e0eee55c011623d2747e517436d04106 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Mon, 29 Jan 2024 10:14:42 +0100 Subject: Get full state event --- LibMatrix/RoomTypes/GenericRoom.cs | 48 ++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'LibMatrix/RoomTypes/GenericRoom.cs') diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index bfb7f43..f5cbc51 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -2,6 +2,7 @@ using System.Collections.Frozen; using System.Diagnostics; using System.Net.Http.Json; using System.Text.Json; +using System.Text.Json.Nodes; using System.Text.Json.Serialization; using System.Web; using ArcaneLibs.Extensions; @@ -41,25 +42,12 @@ public class GenericRoom { Homeserver.ClientHttpClient.GetFromJsonAsync>($"/_matrix/client/v3/rooms/{RoomId}/state"); public async Task GetStateAsync(string type, string stateKey = "") { - var url = $"/_matrix/client/v3/rooms/{RoomId}/state"; - if (!string.IsNullOrEmpty(type)) url += $"/{type}"; + if (string.IsNullOrEmpty(type)) throw new ArgumentNullException(nameof(type), "Event type must be specified"); + var url = $"/_matrix/client/v3/rooms/{RoomId}/state/{type}"; if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}"; try { -#if DEBUG && false - var resp = await _httpClient.GetFromJsonAsync(url); - try { - _homeServer._httpClient.PostAsJsonAsync( - "http://localhost:5116/validate/" + typeof(T).AssemblyQualifiedName, resp); - } - catch (Exception e) { - Console.WriteLine("[!!] Checking state response failed: " + e); - } - - return resp.Deserialize(); -#else var resp = await Homeserver.ClientHttpClient.GetFromJsonAsync(url); return resp; -#endif } catch (MatrixException e) { // if (e is not { ErrorCodode: "M_NOT_FOUND" }) { @@ -81,6 +69,36 @@ public class GenericRoom { } } + public async Task GetStateEventAsync(string type, string stateKey = "") { + if (string.IsNullOrEmpty(type)) throw new ArgumentNullException(nameof(type), "Event type must be specified"); + var url = $"/_matrix/client/v3/rooms/{RoomId}/state/{type}"; + if (!string.IsNullOrEmpty(stateKey)) url += $"/{stateKey}"; + url += "?format=event"; + try { + var resp = await Homeserver.ClientHttpClient.GetFromJsonAsync(url); + if(resp["type"]?.GetValue() != type) throw new InvalidDataException("Returned event type does not match requested type, or server does not support passing `format`."); + return resp.Deserialize(); + } + catch (MatrixException e) { + // if (e is not { ErrorCodode: "M_NOT_FOUND" }) { + throw; + // } + + // Console.WriteLine(e); + // return default; + } + } + + public async Task GetStateEventOrNullAsync(string type, string stateKey = "") { + try { + return await GetStateEventAsync(type, stateKey); + } + catch (MatrixException e) { + if (e.ErrorCode == "M_NOT_FOUND") return default; + throw; + } + } + public async Task GetMessagesAsync(string from = "", int? limit = null, string dir = "b", string filter = "") { var url = $"/_matrix/client/v3/rooms/{RoomId}/messages?dir={dir}"; if (!string.IsNullOrWhiteSpace(from)) url += $"&from={from}"; -- cgit 1.4.1