about summary refs log tree commit diff
path: root/LibMatrix/Helpers/MessageFormatter.cs
blob: 2c5021e1c14e1c603e3946e0acd1dcf9775e4a7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using ArcaneLibs.Extensions;
using LibMatrix.LegacyEvents.EventTypes.Spec;

namespace LibMatrix.Helpers;

public static class MessageFormatter {
    public static RoomMessageLegacyEventContent FormatError(string error) =>
        new(body: error, messageType: "m.text") {
            FormattedBody = $"<font color=\"#EE4444\">{error}</font>",
            Format = "org.matrix.custom.html"
        };

    public static RoomMessageLegacyEventContent 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 RoomMessageLegacyEventContent FormatSuccess(string text) =>
        new(body: text, messageType: "m.text") {
            FormattedBody = $"<font color=\"#00FF00\">{text}</font>",
            Format = "org.matrix.custom.html"
        };

    public static RoomMessageLegacyEventContent 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 = null, string? displayName = null) {
        if (servers is not { Length: > 0 }) servers = new[] { roomId.Split(':', 2)[1] };
        return $"<a href=\"https://matrix.to/#/{roomId}/{eventId}?via={string.Join("&via=", servers)}\">{displayName ?? eventId}</a>";
    }

#region Extension functions

    public static RoomMessageLegacyEventContent ToMatrixMessage(this Exception e, string error) => FormatException(error, e);

#endregion

    public static RoomMessageLegacyEventContent FormatWarning(string warning) =>
        new(body: warning, messageType: "m.text") {
            FormattedBody = $"<font color=\"#FFFF00\">{warning}</font>",
            Format = "org.matrix.custom.html"
        };

    public static RoomMessageLegacyEventContent 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 RoomMessageLegacyEventContent Concat(this RoomMessageLegacyEventContent a, RoomMessageLegacyEventContent b) =>
        new(body: $"{a.Body}{b.Body}", messageType: a.MessageType) {
            FormattedBody = $"{a.FormattedBody}{b.FormattedBody}",
            Format = a.Format
        };

    public static RoomMessageLegacyEventContent ConcatLine(this RoomMessageLegacyEventContent a, RoomMessageLegacyEventContent b) =>
        new(body: $"{a.Body}\n{b.Body}", messageType: "m.text") {
            FormattedBody = $"{a.FormattedBody}<br/>{b.FormattedBody}",
            Format = "org.matrix.custom.html"
        };
}