about summary refs log tree commit diff
path: root/MiniUtils/Commands/ExperimentalFeaturesCommand.cs
blob: 8ae40d3fcf317a2a0e3ed82ccaf0b86f0af4e0e2 (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
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*"(?<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; }
    }


}