about summary refs log tree commit diff
path: root/MiniUtils/Commands/ExperimentalFeaturesCommand.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MiniUtils/Commands/ExperimentalFeaturesCommand.cs')
-rw-r--r--MiniUtils/Commands/ExperimentalFeaturesCommand.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/MiniUtils/Commands/ExperimentalFeaturesCommand.cs b/MiniUtils/Commands/ExperimentalFeaturesCommand.cs
new file mode 100644

index 0000000..de5d035 --- /dev/null +++ b/MiniUtils/Commands/ExperimentalFeaturesCommand.cs
@@ -0,0 +1,68 @@ +using System.Globalization; +using System.Net.Http.Json; +using System.Text; +using System.Text.Json.Nodes; +using System.Text.RegularExpressions; +using LibMatrix.EventTypes.Spec.State; +using LibMatrix.Extensions; +using LibMatrix.Helpers; +using LibMatrix.Utilities.Bot.Interfaces; +using MiniUtils.Utilities; + +namespace SynapseDataMiner.Commands; + +public class ExperimentalFeaturesCommand(MscInfoProvider mscInfoProvider) : ICommand { + public string Name { get; } = "experimental"; + public string[]? Aliases { get; } = []; + public string Description { get; } = "List experimental synapse features"; + public bool Unlisted { get; } = false; + + private static readonly Regex ExperimentalGetMultilineRegex = new("""experimental\.get\(\s*"(?<key>.+?)"(,\s*(?<defaultValue>.+?))?\s*\)""", + RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); + + private static readonly Regex MscNumberRegex = new(@"msc(?<id>\d+)", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + public async Task Invoke(CommandContext ctx) { + var hc = new MatrixHttpClient(); + var resp = await hc.GetAsync("https://raw.githubusercontent.com/element-hq/synapse/develop/synapse/config/experimental.py"); + var data = await resp.Content.ReadAsStringAsync(); + + var msb = new MessageBuilder("m.notice"); + List<SynapseFeature> features = []; + + foreach (Match match in ExperimentalGetMultilineRegex.Matches(data)) { + var mscMatch = MscNumberRegex.Match(match.Groups["key"].Value); + features.Add(new() { + ConfigKey = match.Groups["key"].Value, + DefaultValue = match.Groups["defaultValue"]?.Value, + MscInfo = mscMatch.Success ? await mscInfoProvider.GetMscInfo(int.Parse(mscMatch.Groups["id"].Value)) : null + }); + } + + msb.WithTable(tb => { + tb.WithTitle("Available features", 2); + tb.WithRow(rb => { + rb.WithCell("Feature flag"); + rb.WithCell("Description"); + }); + + foreach (var feature in features.OrderBy(x => x.ConfigKey)) { + tb.WithRow(rb => { + rb.WithCell($"{feature.ConfigKey}<br/>Default: {feature.DefaultValue}"); + rb.WithCell(feature.MscInfo is not null ? feature.MscInfo.ToHtml() : "No MSC info found"); + }); + } + }); + + await ctx.Room.SendMessageEventAsync(msb.Build()); + Console.WriteLine(msb.Build().FormattedBody); + } + + private class SynapseFeature { + public string ConfigKey { get; set; } + public string? DefaultValue { get; set; } + public MscInfoProvider.MscInfo? MscInfo { get; set; } + } + + +} \ No newline at end of file