about summary refs log tree commit diff
path: root/LibMatrix
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2025-07-17 20:49:18 +0200
committerRory& <root@rory.gay>2025-07-17 20:49:18 +0200
commit2d8a5f33bb58c85825e057fcc0e56de9a9fe1c0b (patch)
treea0565445621226b263fe289ddc487a0a8fb19853 /LibMatrix
parentFix joining rooms via ID, without vias. (diff)
downloadLibMatrix-unpacked-2d8a5f33bb58c85825e057fcc0e56de9a9fe1c0b.tar.xz
Throw an exception on MessageBuilder.HtmlFormatMessageLink when ommitting vias with v12+ rooms
Diffstat (limited to 'LibMatrix')
-rw-r--r--LibMatrix/Helpers/MessageFormatter.cs70
1 files changed, 70 insertions, 0 deletions
diff --git a/LibMatrix/Helpers/MessageFormatter.cs b/LibMatrix/Helpers/MessageFormatter.cs

index 1b9b4f3..780ac0e 100644 --- a/LibMatrix/Helpers/MessageFormatter.cs +++ b/LibMatrix/Helpers/MessageFormatter.cs
@@ -0,0 +1,70 @@ +using ArcaneLibs.Extensions; +using LibMatrix.EventTypes.Spec; + +namespace LibMatrix.Helpers; + +public static class MessageFormatter { + public static RoomMessageEventContent FormatError(string error) => + new(body: error, messageType: "m.text") { + FormattedBody = $"<font color=\"#EE4444\">{error}</font>", + Format = "org.matrix.custom.html" + }; + + public static RoomMessageEventContent FormatException(string error, Exception e) => + new(body: $"{error}: {e.Message}", messageType: "m.text") { + FormattedBody = $"<font color=\"#EE4444\">{error}: <pre><code>{e.Message}</code></pre></font>", + Format = "org.matrix.custom.html" + }; + + public static RoomMessageEventContent FormatSuccess(string text) => + new(body: text, messageType: "m.text") { + FormattedBody = $"<font color=\"#00FF00\">{text}</font>", + Format = "org.matrix.custom.html" + }; + + public static RoomMessageEventContent FormatSuccessJson(string text, object data) => + new(body: text, messageType: "m.text") { + FormattedBody = $"<font color=\"#00FF00\">{text}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>", + Format = "org.matrix.custom.html" + }; + + public static string HtmlFormatMention(string id, string? displayName = null) => $"<a href=\"https://matrix.to/#/{id}\">{displayName ?? id}</a>"; + + public static string HtmlFormatMessageLink(string roomId, string eventId, string[] servers, string? displayName = null) { + if (servers is not { Length: > 0 }) + servers = roomId.Contains(':') + ? [roomId.Split(':', 2)[1]] + : throw new ArgumentException("Message links must contain a list of via's for v12+ rooms!", nameof(servers)); + return $"<a href=\"https://matrix.to/#/{roomId}/{eventId}?via={string.Join("&via=", servers)}\">{displayName ?? eventId}</a>"; + } + +#region Extension functions + + public static RoomMessageEventContent ToMatrixMessage(this Exception e, string error) => FormatException(error, e); + +#endregion + + public static RoomMessageEventContent FormatWarning(string warning) => + new(body: warning, messageType: "m.text") { + FormattedBody = $"<font color=\"#FFFF00\">{warning}</font>", + Format = "org.matrix.custom.html" + }; + + public static RoomMessageEventContent FormatWarningJson(string warning, object data) => + new(body: warning, messageType: "m.text") { + FormattedBody = $"<font color=\"#FFFF00\">{warning}: <pre><code>{data.ToJson(ignoreNull: true)}</code></pre></font>", + Format = "org.matrix.custom.html" + }; + + public static RoomMessageEventContent Concat(this RoomMessageEventContent a, RoomMessageEventContent b) => + new(body: $"{a.Body}{b.Body}", messageType: a.MessageType) { + FormattedBody = $"{a.FormattedBody}{b.FormattedBody}", + Format = a.Format + }; + + public static RoomMessageEventContent ConcatLine(this RoomMessageEventContent a, RoomMessageEventContent b) => + new(body: $"{a.Body}\n{b.Body}", messageType: "m.text") { + FormattedBody = $"{a.FormattedBody}<br/>{b.FormattedBody}", + Format = "org.matrix.custom.html" + }; +} \ No newline at end of file