about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
authorEmma [it/its]@Rory& <root@rory.gay>2023-12-19 18:55:45 +0100
committerEmma [it/its]@Rory& <root@rory.gay>2023-12-19 18:55:45 +0100
commit4877079e79b62ae8e8031ab6e3d4e0742abb8530 (patch)
tree5d354d1de45586585c28a01550f9cd1365e58a4a /LibMatrix
parentCleanup (diff)
downloadLibMatrix-4877079e79b62ae8e8031ab6e3d4e0742abb8530.tar.xz
Cleanup
Diffstat (limited to 'LibMatrix')
-rw-r--r--LibMatrix/Helpers/MessageFormatter.cs4
-rw-r--r--LibMatrix/Responses/CreateRoomRequest.cs2
-rw-r--r--LibMatrix/RoomTypes/GenericRoom.cs47
3 files changed, 35 insertions, 18 deletions
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 = $"<font color=\"#FF0000\">{error}</font>",
+            FormattedBody = $"<font color=\"#EE4444\">{error}</font>",
             Format = "org.matrix.custom.html"
         };
     }
 
     public static RoomMessageEventContent FormatException(string error, Exception e) {
         return new RoomMessageEventContent(body: $"{error}: {e.Message}", messageType: "m.text") {
-            FormattedBody = $"<font color=\"#FF0000\">{error}: <pre>{e.Message}</pre></font>",
+            FormattedBody = $"<font color=\"#EE4444\">{error}: <pre>{e.Message}</pre></font>",
             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<MatrixEventAttribute>()?
-                                .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<ChunkedStateEventResponse>(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<EventIdResponse> SendMessageEventAsync(RoomMessageEventContent content) =>
         await SendTimelineEventAsync("m.room.message", content);
@@ -254,9 +255,25 @@ public class GenericRoom {
         await Task.WhenAll(tasks);
     }
 
-    #endregion
+    public async Task<string?> 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<EventIdResponse?> 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<EventIdResponse>())!;
     }
 
-    #endregion
+#endregion
 
-    #region Utilities
+#region Utilities
 
     public async Task<Dictionary<string, List<string>>> 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