From 4877079e79b62ae8e8031ab6e3d4e0742abb8530 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Tue, 19 Dec 2023 18:55:45 +0100 Subject: Cleanup --- LibMatrix/Helpers/MessageFormatter.cs | 4 +-- LibMatrix/Responses/CreateRoomRequest.cs | 2 +- LibMatrix/RoomTypes/GenericRoom.cs | 47 ++++++++++++++++++++++---------- 3 files changed, 35 insertions(+), 18 deletions(-) (limited to 'LibMatrix') diff --git a/LibMatrix/Helpers/MessageFormatter.cs b/LibMatrix/Helpers/MessageFormatter.cs index f275b57..b2dda61 100644 --- a/LibMatrix/Helpers/MessageFormatter.cs +++ b/LibMatrix/Helpers/MessageFormatter.cs @@ -6,14 +6,14 @@ namespace LibMatrix.Helpers; public static class MessageFormatter { public static RoomMessageEventContent FormatError(string error) { return new RoomMessageEventContent(body: error, messageType: "m.text") { - FormattedBody = $"{error}", + FormattedBody = $"{error}", Format = "org.matrix.custom.html" }; } public static RoomMessageEventContent FormatException(string error, Exception e) { return new RoomMessageEventContent(body: $"{error}: {e.Message}", messageType: "m.text") { - FormattedBody = $"{error}:
{e.Message}
", + FormattedBody = $"{error}:
{e.Message}
", Format = "org.matrix.custom.html" }; } diff --git a/LibMatrix/Responses/CreateRoomRequest.cs b/LibMatrix/Responses/CreateRoomRequest.cs index f8d1d05..3b93cf7 100644 --- a/LibMatrix/Responses/CreateRoomRequest.cs +++ b/LibMatrix/Responses/CreateRoomRequest.cs @@ -56,7 +56,7 @@ public class CreateRoomRequest { TypedContent = (EventContent)Activator.CreateInstance( StateEvent.KnownStateEventTypes.FirstOrDefault(x => x.GetCustomAttributes()? - .Any(y => y.EventName == eventType) ?? false) ?? typeof(object) + .Any(y => y.EventName == eventType) ?? false) ?? typeof(UnknownEventContent) )! }); } diff --git a/LibMatrix/RoomTypes/GenericRoom.cs b/LibMatrix/RoomTypes/GenericRoom.cs index d067f9f..0cedf29 100644 --- a/LibMatrix/RoomTypes/GenericRoom.cs +++ b/LibMatrix/RoomTypes/GenericRoom.cs @@ -9,6 +9,7 @@ using LibMatrix.EventTypes.Spec; using LibMatrix.EventTypes.Spec.State; using LibMatrix.EventTypes.Spec.State.RoomInfo; using LibMatrix.Homeservers; +using LibMatrix.Services; namespace LibMatrix.RoomTypes; @@ -136,6 +137,7 @@ public class GenericRoom { Console.WriteLine("End is null"); yield break; } + from = resp.End; } } @@ -174,7 +176,6 @@ public class GenericRoom { Console.WriteLine($"Members call response read in {sw.GetElapsedAndRestart()}"); var result = await JsonSerializer.DeserializeAsync(await res.Content.ReadAsStreamAsync(), new JsonSerializerOptions() { TypeInfoResolver = ChunkedStateEventResponseSerializerContext.Default, - }); Console.WriteLine($"Members call deserialised in {sw.GetElapsedAndRestart()}"); foreach (var resp in result.Chunk) { @@ -186,7 +187,7 @@ public class GenericRoom { Console.WriteLine($"Members call iterated in {sw.GetElapsedAndRestart()}"); } - #region Utility shortcuts +#region Utility shortcuts public async Task SendMessageEventAsync(RoomMessageEventContent content) => await SendTimelineEventAsync("m.room.message", content); @@ -254,9 +255,25 @@ public class GenericRoom { await Task.WhenAll(tasks); } - #endregion + public async Task GetResolvedRoomAvatarUrlAsync(bool useOriginHomeserver = false) { + var avatar = await GetAvatarUrlAsync(); + if (avatar?.Url is null) return null; + if (!avatar.Url.StartsWith("mxc://")) return avatar.Url; + if (useOriginHomeserver) + try { + var hs = avatar.Url.Split('/', 3)[1]; + return await new HomeserverResolverService().ResolveMediaUri(hs, avatar.Url); + } + catch (Exception e) { + Console.WriteLine(e); + } + + return Homeserver.ResolveMediaUri(avatar.Url); + } + +#endregion - #region Simple calls +#region Simple calls public async Task ForgetAsync() => await Homeserver.ClientHttpClient.PostAsync($"/_matrix/client/v3/rooms/{RoomId}/forget", null); @@ -284,9 +301,9 @@ public class GenericRoom { await Homeserver.ClientHttpClient.PostAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/invite", new UserIdAndReason(userId, reason)); } - #endregion +#endregion - #region Events +#region Events public async Task SendStateEventAsync(string eventType, object content) => await (await Homeserver.ClientHttpClient.PutAsJsonAsync($"/_matrix/client/v3/rooms/{RoomId}/state/{eventType}", content)) @@ -347,9 +364,9 @@ public class GenericRoom { $"/_matrix/client/v3/rooms/{RoomId}/redact/{eventToRedact}/{Guid.NewGuid()}", data)).Content.ReadFromJsonAsync())!; } - #endregion +#endregion - #region Utilities +#region Utilities public async Task>> GetMembersByHomeserverAsync(bool joinedOnly = true) { if (Homeserver is AuthenticatedHomeserverMxApiExtended mxaeHomeserver) @@ -367,11 +384,7 @@ public class GenericRoom { return roomHomeservers; } - #endregion - - public readonly SpaceRoom AsSpace; - - #region Disband room +#region Disband room public async Task DisbandRoomAsync() { var states = GetFullStateAsync(); @@ -398,10 +411,14 @@ public class GenericRoom { } } - #endregion +#endregion + +#endregion + + public readonly SpaceRoom AsSpace; } public class RoomIdResponse { [JsonPropertyName("room_id")] public string RoomId { get; set; } = null!; -} +} \ No newline at end of file -- cgit 1.4.1