using System.Text.RegularExpressions; 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*"(?.+?)"(,\s*(?.+?))?\s*\)""", RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture); private static readonly Regex MscNumberRegex = new(@"msc(?\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 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}
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; } } }