diff options
author | Emma [it/its]@Rory& <root@rory.gay> | 2024-01-24 02:28:54 +0100 |
---|---|---|
committer | Emma [it/its]@Rory& <root@rory.gay> | 2024-01-24 02:28:54 +0100 |
commit | bf2da30c7ae9d4c15a5e22f3ee0b1bae2ca66e46 (patch) | |
tree | c375a53ef850e33d8721abb0f68b34eb10afed08 | |
parent | Abstract FederationClient from RemoteHomeserver (diff) | |
download | LibMatrix-bf2da30c7ae9d4c15a5e22f3ee0b1bae2ca66e46.tar.xz |
MessageBuilder extensions
-rw-r--r-- | ExampleBots/LibMatrix.ExampleBot/Bot/RMUBot.cs (renamed from ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs) | 10 | ||||
-rw-r--r-- | ExampleBots/LibMatrix.ExampleBot/Bot/RMUBotConfiguration.cs (renamed from ExampleBots/LibMatrix.ExampleBot/Bot/MRUBotConfiguration.cs) | 4 | ||||
-rw-r--r-- | ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs | 4 | ||||
-rw-r--r-- | ExampleBots/LibMatrix.ExampleBot/Program.cs | 4 | ||||
-rw-r--r-- | LibMatrix.EventTypes/LibMatrix.EventTypes.csproj | 4 | ||||
-rw-r--r-- | LibMatrix/Helpers/MessageBuilder.cs | 50 | ||||
-rw-r--r-- | LibMatrix/LibMatrix.csproj | 4 | ||||
-rw-r--r-- | LibMatrix/Services/ServiceInstaller.cs | 2 | ||||
-rw-r--r-- | Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs | 1 |
9 files changed, 59 insertions, 24 deletions
diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/RMUBot.cs index 8e6cd6a..de47ec6 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBot.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/RMUBot.cs @@ -12,14 +12,14 @@ using Microsoft.Extensions.Logging; namespace LibMatrix.ExampleBot.Bot; -public class MRUBot : IHostedService { +public class RMUBot : IHostedService { private readonly HomeserverProviderService _homeserverProviderService; - private readonly ILogger<MRUBot> _logger; - private readonly MRUBotConfiguration _configuration; + private readonly ILogger<RMUBot> _logger; + private readonly RMUBotConfiguration _configuration; private readonly IEnumerable<ICommand> _commands; - public MRUBot(HomeserverProviderService homeserverProviderService, ILogger<MRUBot> logger, - MRUBotConfiguration configuration, IServiceProvider services) { + public RMUBot(HomeserverProviderService homeserverProviderService, ILogger<RMUBot> logger, + RMUBotConfiguration configuration, IServiceProvider services) { logger.LogInformation("{} instantiated!", this.GetType().Name); _homeserverProviderService = homeserverProviderService; _logger = logger; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBotConfiguration.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/RMUBotConfiguration.cs index dcdfc4c..9edc4bd 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/MRUBotConfiguration.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/RMUBotConfiguration.cs @@ -2,8 +2,8 @@ using Microsoft.Extensions.Configuration; namespace LibMatrix.ExampleBot.Bot; -public class MRUBotConfiguration { - public MRUBotConfiguration(IConfiguration config) { +public class RMUBotConfiguration { + public RMUBotConfiguration(IConfiguration config) { config.GetRequiredSection("Bot").Bind(this); } public string Homeserver { get; set; } = ""; diff --git a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs index 2f6e0b0..0645668 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Bot/StartupTasks/ServerRoomSizeCalulator.cs @@ -10,11 +10,11 @@ namespace LibMatrix.ExampleBot.Bot.StartupTasks; public class ServerRoomSizeCalulator : IHostedService { private readonly HomeserverProviderService _homeserverProviderService; private readonly ILogger<ServerRoomSizeCalulator> _logger; - private readonly MRUBotConfiguration _configuration; + private readonly RMUBotConfiguration _configuration; private readonly IEnumerable<ICommand> _commands; public ServerRoomSizeCalulator(HomeserverProviderService homeserverProviderService, ILogger<ServerRoomSizeCalulator> logger, - MRUBotConfiguration configuration, IServiceProvider services) { + RMUBotConfiguration configuration, IServiceProvider services) { logger.LogInformation("Server room size calculator hosted service instantiated!"); _homeserverProviderService = homeserverProviderService; _logger = logger; diff --git a/ExampleBots/LibMatrix.ExampleBot/Program.cs b/ExampleBots/LibMatrix.ExampleBot/Program.cs index 25ce07d..86d7598 100644 --- a/ExampleBots/LibMatrix.ExampleBot/Program.cs +++ b/ExampleBots/LibMatrix.ExampleBot/Program.cs @@ -16,7 +16,7 @@ var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { dataStorageProvider: new FileStorageProvider("bot_data/data/") ) ); - services.AddScoped<MRUBotConfiguration>(); + services.AddScoped<RMUBotConfiguration>(); services.AddRoryLibMatrixServices(); foreach (var commandClass in new ClassCollector<ICommand>().ResolveFromAllAccessibleAssemblies()) { Console.WriteLine($"Adding command {commandClass.Name}"); @@ -24,7 +24,7 @@ var host = Host.CreateDefaultBuilder(args).ConfigureServices((_, services) => { } // services.AddHostedService<ServerRoomSizeCalulator>(); - services.AddHostedService<MRUBot>(); + services.AddHostedService<RMUBot>(); }).UseConsoleLifetime().Build(); await host.RunAsync(); diff --git a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj index a242125..1c38825 100644 --- a/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj +++ b/LibMatrix.EventTypes/LibMatrix.EventTypes.csproj @@ -11,8 +11,8 @@ <!-- This is dangerous, but eases development since locking the version will drift out of sync without noticing, which causes build errors due to missing functions. Using the NuGet version in development is annoying due to delays between pushing and being able to consume. - If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixRoomUtils.git - instead, since this will be locked by the MatrixRoomUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. --> + If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixUtils.git + instead, since this will be locked by the MatrixUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. --> <PackageReference Condition="!Exists('..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj')" Include="ArcaneLibs" Version="*-preview*"/> </ItemGroup> diff --git a/LibMatrix/Helpers/MessageBuilder.cs b/LibMatrix/Helpers/MessageBuilder.cs index 7715462..250187a 100644 --- a/LibMatrix/Helpers/MessageBuilder.cs +++ b/LibMatrix/Helpers/MessageBuilder.cs @@ -11,6 +11,37 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr public RoomMessageEventContent Build() => Content; + public MessageBuilder WithBody(string body) { + Content.Body += body; + Content.FormattedBody += body; + return this; + } + + public MessageBuilder WithHtmlTag(string tag, string body, Dictionary<string, string>? attributes = null) { + Content.Body += body; + Content.FormattedBody += $"<{tag}"; + if (attributes != null) { + foreach (var (key, value) in attributes) { + Content.FormattedBody += $" {key}=\"{value}\""; + } + } + Content.FormattedBody += $">{body}</{tag}>"; + return this; + } + + public MessageBuilder WithHtmlTag(string tag, Action<MessageBuilder> bodyBuilder, Dictionary<string, string>? attributes = null) { + Content.FormattedBody += $"<{tag}"; + if (attributes != null) { + foreach (var (key, value) in attributes) { + Content.FormattedBody += $" {key}=\"{value}\""; + } + } + Content.FormattedBody += ">"; + bodyBuilder(this); + Content.FormattedBody += $"</{tag}>"; + return this; + } + public MessageBuilder WithColoredBody(string color, string body) { Content.Body += body; Content.FormattedBody += $"<font color=\"{color}\">{body}</font>"; @@ -25,14 +56,17 @@ public class MessageBuilder(string msgType = "m.text", string format = "org.matr } public MessageBuilder WithRainbowString(string text, byte skip = 1, int offset = 0, double lengthFactor = 255.0, bool useLength = true) { - if (useLength) { - lengthFactor = text.Length; - } - RainbowEnumerator enumerator = new(skip, offset, lengthFactor); - for (int i = 0; i < text.Length; i++) { - var (r, g, b) = enumerator.Next(); - Content.FormattedBody += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>"; - } + // if (useLength) { + // lengthFactor = text.Length; + // } + // HslaColorInterpolator interpolator = new((0, 255, 128, 255), (255, 255, 128, 255)); + // // RainbowEnumerator enumerator = new(skip, offset, lengthFactor); + // for (int i = 0; i < text.Length; i++) { + // // var (r, g, b) = enumerator.Next(); + // // var (r,g,b,a) = interpolator.Interpolate((i+offset) * skip, lengthFactor).ToRgba(); + // // Console.WriteLine($"RBA: {r} {g} {b} {a}"); + // // Content.FormattedBody += $"<font color=\"#{r:X2}{g:X2}{b:X2}\">{text[i]}</font>"; + // } return this; } diff --git a/LibMatrix/LibMatrix.csproj b/LibMatrix/LibMatrix.csproj index a2ee327..16e43f5 100644 --- a/LibMatrix/LibMatrix.csproj +++ b/LibMatrix/LibMatrix.csproj @@ -21,8 +21,8 @@ <!-- This is dangerous, but eases development since locking the version will drift out of sync without noticing, which causes build errors due to missing functions. Using the NuGet version in development is annoying due to delays between pushing and being able to consume. - If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixRoomUtils.git - instead, since this will be locked by the MatrixRoomUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. --> + If you want to use a time-appropriate version of the library, recursively clone https://cgit.rory.gay/matrix/MatrixUtils.git + instead, since this will be locked by the MatrixUtils project, which contains both LibMatrix and ArcaneLibs as a submodule. --> <PackageReference Condition="!Exists('..\ArcaneLibs\ArcaneLibs\ArcaneLibs.csproj')" Include="ArcaneLibs" Version="*-preview*"/> <ProjectReference Include="..\LibMatrix.EventTypes\LibMatrix.EventTypes.csproj" /> </ItemGroup> diff --git a/LibMatrix/Services/ServiceInstaller.cs b/LibMatrix/Services/ServiceInstaller.cs index ad5bedc..358dc2a 100644 --- a/LibMatrix/Services/ServiceInstaller.cs +++ b/LibMatrix/Services/ServiceInstaller.cs @@ -7,7 +7,7 @@ public static class ServiceInstaller { public static IServiceCollection AddRoryLibMatrixServices(this IServiceCollection services, RoryLibMatrixConfiguration? config = null) { //Check required services // if (!services.Any(x => x.ServiceType == typeof(TieredStorageService))) - // throw new Exception("[MRUCore/DI] No TieredStorageService has been registered!"); + // throw new Exception("[RMUCore/DI] No TieredStorageService has been registered!"); //Add config services.AddSingleton(config ?? new RoryLibMatrixConfiguration()); diff --git a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs index b2f597c..678df27 100644 --- a/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs +++ b/Utilities/LibMatrix.DevTestBot/Bot/DevTestBot.cs @@ -44,6 +44,7 @@ public class DevTestBot : IHostedService { throw; } + var msg = new MessageBuilder().WithRainbowString("Meanwhile, I'm sitting here, still struggling with trying to rainbow. ^^'").Build(); var res = await hs.ResolveRoomAliasAsync("#watercooler:maunium.net"); var syncHelper = new SyncHelper(hs); |