about summary refs log tree commit diff
path: root/MatrixContentFilter/Commands/GetConfigCommand.cs
blob: bac00ca2bd6ee2268751df5bd272162b7b604ce4 (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
using System.Text;
using ArcaneLibs.Attributes;
using ArcaneLibs.Extensions;
using LibMatrix.EventTypes.Spec;
using LibMatrix.Helpers;
using LibMatrix.Utilities.Bot.Commands;
using LibMatrix.Utilities.Bot.Interfaces;
using MatrixContentFilter.EventTypes;
using Microsoft.Extensions.DependencyInjection;

namespace MatrixContentFilter.Commands;

public class GetConfigCommand(IServiceProvider svcs) : ICommand {
    public string Name { get; } = "getconfig";
    public string[]? Aliases { get; } = [];
    public string Description { get; } = "Get the current configuration, optionally takes a room ID";
    public bool Unlisted { get; } = false;

    public async Task Invoke(CommandContext ctx) {
        var room = ctx.Room;
        if (ctx.Args.Length > 0) {
            try {
                room = ctx.Homeserver.GetRoom(ctx.Args[0]);
            }
            catch {
                await ctx.Room.SendMessageEventAsync(new MessageBuilder("m.notice").WithBody("Invalid room ID").Build());
                return;
            }
        }

        var defaults = await ctx.Homeserver.GetAccountDataAsync<FilterConfiguration>(FilterConfiguration.EventId);
        var config = await room.GetRoomAccountDataOrNullAsync<FilterConfiguration>(FilterConfiguration.EventId);
        var msb = new MessageBuilder("m.notice").WithColoredBody("#FFCC00", "Default configuration:")
            .WithTable(tb => {
                foreach (var prop in defaults.GetType().GetProperties()) {
                    var key = prop.GetFriendlyName();
                    var val = prop.GetValue(defaults);

                    tb = tb.WithRow(rb => {
                        rb.WithCell(key);
                        rb.WithCell(val?.ToJson() ?? "null");
                    });
                }
            });

        if (config == null) {
            msb = msb.WithBody("No configuration set for this room, using defaults");
        }
        else {
            msb = msb.WithBody("Room overrides (additive):")
                .WithCodeBlock(config.ToJson(ignoreNull: true), "json");
        }

        await room.SendMessageEventAsync(msb.Build());
    }
}