From 5ca0a45606ad2ca7e1ca45a3b27be08e9640dd9d Mon Sep 17 00:00:00 2001 From: Rory& Date: Thu, 25 Apr 2024 06:31:11 +0200 Subject: Fixes --- LibMatrix/RoomTypes/GenericRoom.cs | 59 +++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) (limited to 'LibMatrix/RoomTypes') diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index e4d2b9c..43f3acc 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -10,6 +10,8 @@ using LibMatrix.EventTypes; using LibMatrix.EventTypes.Spec; using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; +using LibMatrix.Filters; +using LibMatrix.Helpers; using LibMatrix.Homeservers; using LibMatrix.Services; using Microsoft.Extensions.Logging.Abstractions; @@ -74,7 +76,11 @@ public class GenericRoom { 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`."); + throw new LibMatrixException() { + Error = "Homeserver returned event type does not match requested type, or server does not support passing `format`.", + ErrorCode = LibMatrixException.ErrorCodes.M_UNSUPPORTED + }; + // throw new InvalidDataException("Returned event type does not match requested type, or server does not support passing `format`."); return resp.Deserialize(); } catch (MatrixException e) { @@ -87,6 +93,45 @@ public class GenericRoom { } } + public async Task GetStateEventIdAsync(string type, string stateKey = "", bool fallbackToSync = true) { + try { + return (await GetStateEventAsync(type, stateKey)).EventId ?? throw new LibMatrixException() { + ErrorCode = LibMatrixException.ErrorCodes.M_UNSUPPORTED, + Error = "Homeserver does not include event ID in state events." + }; + } + catch (LibMatrixException e) { + if (e.ErrorCode == LibMatrixException.ErrorCodes.M_UNSUPPORTED) { + if (!fallbackToSync) throw; + Console.WriteLine("WARNING: Homeserver does not support getting event ID from state events, falling back to sync"); + var sh = new SyncHelper(Homeserver); + var emptyFilter = new SyncFilter.EventFilter(types: [], limit: 1, senders: [], notTypes: ["*"]); + var emptyStateFilter = new SyncFilter.RoomFilter.StateFilter(types: [], limit: 1, senders: [], notTypes: ["*"], rooms:[]); + sh.Filter = new() { + Presence = emptyFilter, + AccountData = emptyFilter, + Room = new SyncFilter.RoomFilter() { + AccountData = emptyStateFilter, + Timeline = emptyStateFilter, + Ephemeral = emptyStateFilter, + State = new SyncFilter.RoomFilter.StateFilter(), + Rooms = [RoomId] + } + }; + var sync = await sh.SyncAsync(); + var state = sync.Rooms.Join[RoomId].State.Events; + var stateEvent = state.FirstOrDefault(x => x.Type == type && x.StateKey == stateKey); + if (stateEvent is null) throw new LibMatrixException() { + ErrorCode = LibMatrixException.ErrorCodes.M_NOT_FOUND, + Error = "State event not found in sync response" + }; + return stateEvent.EventId; + } + + return null; + } + } + public async Task GetStateEventOrNullAsync(string type, string stateKey = "") { try { return await GetStateEventAsync(type, stateKey); @@ -174,7 +219,7 @@ public class GenericRoom { var joinUrl = $"/_matrix/client/v3/join/{HttpUtility.UrlEncode(RoomId)}"; Console.WriteLine($"Calling {joinUrl} with {homeservers?.Length ?? 0} via's..."); - if (homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':')[1] }; + if (homeservers == null || homeservers.Length == 0) homeservers = new[] { RoomId.Split(':', 2)[1] }; var fullJoinUrl = $"{joinUrl}?server_name=" + string.Join("&server_name=", homeservers); var res = await Homeserver.ClientHttpClient.PostAsJsonAsync(fullJoinUrl, new { reason @@ -406,7 +451,8 @@ public class GenericRoom { } } - public Task GetEventAsync(string eventId) => Homeserver.ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); + public Task GetEventAsync(string eventId) => + Homeserver.ClientHttpClient.GetFromJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/event/{eventId}"); public async Task RedactEventAsync(string eventToRedact, string reason) { var data = new { reason }; @@ -457,8 +503,13 @@ public class GenericRoom { } if (stateTypeIgnore.Contains(state.Type)) continue; - await SendStateEventAsync(state.Type, state.StateKey, new object()); + try { + await SendStateEventAsync(state.Type, state.StateKey, new object()); + } + catch { } } + + await LeaveAsync("Disbanded room"); } #endregion -- cgit 1.4.1