about summary refs log tree commit diff
path: root/MiniUtils/Commands/DumpTimelineCommand.cs
blob: 4ee53c9588b0380f0047d886fbc154444d76d682 (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
using System.Text.Json;
using System.Text.Json.Serialization;
using LibMatrix;
using LibMatrix.Services;
using LibMatrix.Utilities.Bot.Interfaces;

namespace MiniUtils.Commands;

public class DumpTimelineCommand(MiniUtilsConfiguration config, HomeserverProviderService hsProvider) : ICommand {
    public string Name => "dump timeline";

    public string[]? Aliases => ["dt"];

    public string Description => "Dump timeline";

    public bool Unlisted => false;

    public async Task Invoke(CommandContext ctx) {
        MessagesResponse res;
        if (ctx.Args.Length < 1) {
            res = await ctx.Room.GetMessagesAsync(limit: 250);
        }
        else {
            var profile = config.ExternalProfiles[ctx.Args[0]];
            var rhs = await hsProvider.GetAuthenticatedWithToken(profile.Homeserver, profile.AccessToken, enableServer: false, useGeneric: true);
            res = await rhs.GetRoom(ctx.Room.RoomId).GetMessagesAsync(limit: 250);
        }

        var ms = new MemoryStream();
        await JsonSerializer.SerializeAsync(ms, res, new JsonSerializerOptions() {
            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
            WriteIndented = true
        });
        ms.Seek(0, SeekOrigin.Begin);
        
        await ctx.Room.SendFileAsync("timeline.json", ms, contentType: "application/json");
    }
}