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

index 0000000..4ee53c9 --- /dev/null +++ b/MiniUtils/Commands/DumpTimelineCommand.cs
@@ -0,0 +1,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"); + } +} \ No newline at end of file