about summary refs log tree commit diff
path: root/LibMatrix/Helpers/MessageFormatter.cs
blob: 37d7004c30036406c2fc4e8eba39b407a3b3c26b (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
using ArcaneLibs.Extensions;
using LibMatrix.StateEventTypes.Spec;

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}: {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>",
            Format = "org.matrix.custom.html"
        };
    }

    public static RoomMessageEventContent FormatSuccess(string text) {
        return new RoomMessageEventContent(body: text, messageType: "m.text") {
            FormattedBody = $"<font color=\"#00FF00\">{text}</font>",
            Format = "org.matrix.custom.html"
        };
    }

    public static RoomMessageEventContent FormatSuccessJson(string text, object data) {
        return new RoomMessageEventContent(body: text, messageType: "m.text") {
            FormattedBody = $"<font color=\"#00FF00\">{text}: <pre>{data.ToJson(ignoreNull: true)}</pre></font>",
            Format = "org.matrix.custom.html"
        };
    }

    public static string HtmlFormatMention(string id, string? displayName = null) {
        return $"<a href=\"https://matrix.to/#/{id}\">{displayName ?? id}</a>";
    }
}