From 6bd02248ccfbcb46960a6f39eaad23888d190eb5 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 15 Sep 2023 09:50:45 +0200 Subject: Some refactoring --- .../AppServiceConfiguration.cs | 87 ++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs (limited to 'Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs') diff --git a/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs new file mode 100644 index 0000000..4de139e --- /dev/null +++ b/Utilities/LibMatrix.Utilities.Bot/AppServiceConfiguration.cs @@ -0,0 +1,87 @@ +namespace PluralContactBotPoC; + +public class AppServiceConfiguration { + public string Id { get; set; } = null!; + public string? Url { get; set; } = null!; + public string SenderLocalpart { get; set; } = null!; + public string AppserviceToken { get; set; } = null!; + public string HomeserverToken { get; set; } = null!; + public List? Protocols { get; set; } = null!; + public bool? RateLimited { get; set; } = null!; + + public AppserviceNamespaces Namespaces { get; set; } = null!; + + public class AppserviceNamespaces { + public List? Users { get; set; } = null; + public List? Aliases { get; set; } = null; + public List? Rooms { get; set; } = null; + + public class AppserviceNamespace { + public bool Exclusive { get; set; } + public string Regex { get; set; } = null!; + } + } + + /// + /// Please dont look at code, it's horrifying but works + /// + /// + 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 is not null) + yaml += $"rate_limited: {RateLimited!.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; + } +} -- cgit 1.4.1