about summary refs log tree commit diff
path: root/Utilities/LibMatrix.Utilities.Bot/AppServices/AppServiceConfiguration.cs
blob: 6dc76f6cad8c5a9aedc87dae299a4faa3c216a85 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Text.Json.Serialization;

namespace LibMatrix.Utilities.Bot.AppServices;

public class AppServiceConfiguration {
    [JsonPropertyName("id")]
    public string Id { get; set; }

    [JsonPropertyName("url")]
    public string? Url { get; set; }

    [JsonPropertyName("sender_localpart")]
    public string SenderLocalpart { get; set; }

    [JsonPropertyName("as_token")]
    public string AppserviceToken { get; set; }

    [JsonPropertyName("hs_token")]
    public string HomeserverToken { get; set; }

    [JsonPropertyName("protocols")]
    public List<string>? Protocols { get; set; }

    [JsonPropertyName("rate_limited")]
    public bool? RateLimited { get; set; }

    [JsonPropertyName("namespaces")]
    public AppserviceNamespaces Namespaces { get; set; }

    public class AppserviceNamespaces {
        [JsonPropertyName("users")]
        public List<AppserviceNamespace>? Users { get; set; } = null;

        [JsonPropertyName("aliases")]
        public List<AppserviceNamespace>? Aliases { get; set; } = null;

        [JsonPropertyName("rooms")]
        public List<AppserviceNamespace>? Rooms { get; set; } = null;

        public class AppserviceNamespace {
            [JsonPropertyName("exclusive")]
            public bool Exclusive { get; set; }

            [JsonPropertyName("regex")]
            public string Regex { get; set; }
        }
    }

    /// <summary>
    /// Please dont look at code, it's horrifying but works
    /// </summary>
    /// <returns></returns>
    public string ToYaml() {
        var yaml = $"""
                    id: "{Id ?? throw new NullReferenceException("Id is null")}"
                    url: {(Url is null ? "null" : $"\"{Url}\"")}
                    as_token: "{AppserviceToken ?? throw new NullReferenceException("AppserviceToken is null")}"
                    hs_token: "{HomeserverToken ?? throw new NullReferenceException("HomeserverToken is null")}"
                    sender_localpart: "{SenderLocalpart ?? throw new NullReferenceException("SenderLocalpart is null")}"

                    """;

        if (Protocols is not null && Protocols.Count > 0)
            yaml += $"""
                     protocols:
                        - "{Protocols[0] ?? throw new NullReferenceException("Protocols[0] is null")}"
                     """;
        else
            yaml += "protocols: []";
        yaml += "\n";
        if (RateLimited.HasValue)
            yaml += $"rate_limited: {RateLimited.Value.ToString().ToLower()}\n";
        else
            yaml += "rate_limited: false\n";

        yaml += "namespaces: \n";

        if (Namespaces.Users is null || Namespaces.Users.Count == 0)
            yaml += "  users: []";
        else
            Namespaces.Users.ForEach(x =>
                yaml += $"""
                             users:
                                 - exclusive: {x.Exclusive.ToString().ToLower()}
                                   regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}"
                         """);
        yaml += "\n";

        if (Namespaces.Aliases is null || Namespaces.Aliases.Count == 0)
            yaml += "  aliases: []";
        else
            Namespaces.Aliases.ForEach(x =>
                yaml += $"""
                             aliases:
                                 - exclusive: {x.Exclusive.ToString().ToLower()}
                                   regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}"
                         """);
        yaml += "\n";
        if (Namespaces.Rooms is null || Namespaces.Rooms.Count == 0)
            yaml += "  rooms: []";
        else
            Namespaces.Rooms.ForEach(x =>
                yaml += $"""
                             rooms:
                                 - exclusive: {x.Exclusive.ToString().ToLower()}
                                   regex: "{x.Regex ?? throw new NullReferenceException("x.Regex is null")}"
                         """);

        return yaml;
    }
}