From 91a8daf3a7f8683fa7864a4f597ce7dd7e8aa66a Mon Sep 17 00:00:00 2001 From: Kushida Date: Sat, 25 Jul 2026 20:28:06 +0300 Subject: [PATCH] Fix reverse exports with an after boundary (#1569) --- .../Specs/DateRangeSpecs.cs | 46 +++++++++++++++++++ .../Discord/DiscordClient.cs | 4 ++ 2 files changed, 50 insertions(+) diff --git a/DiscordChatExporter.Cli.Tests/Specs/DateRangeSpecs.cs b/DiscordChatExporter.Cli.Tests/Specs/DateRangeSpecs.cs index 5d5bcac4..59d24b10 100644 --- a/DiscordChatExporter.Cli.Tests/Specs/DateRangeSpecs.cs +++ b/DiscordChatExporter.Cli.Tests/Specs/DateRangeSpecs.cs @@ -60,6 +60,52 @@ public class DateRangeSpecs ); } + [Fact] + public async Task I_can_filter_the_reverse_export_to_only_include_messages_sent_after_the_specified_date() + { + // Arrange + var after = new DateTimeOffset(2021, 07, 24, 0, 0, 0, TimeSpan.Zero); + using var file = TempFile.Create(); + + // Act + await new ExportChannelsCommand + { + Token = Secrets.DiscordToken, + ChannelIds = [ChannelIds.DateRangeTestCases], + ExportFormat = ExportFormat.Json, + OutputPath = file.Path, + After = Snowflake.FromDate(after), + IsReverseMessageOrder = true, + }.ExecuteAsync(new FakeConsole()); + + // Assert + var timestamps = Json.Parse(await File.ReadAllTextAsync(file.Path)) + .GetProperty("messages") + .EnumerateArray() + .Select(j => j.GetProperty("timestamp").GetDateTimeOffset()) + .ToArray(); + + timestamps.All(t => t > after).Should().BeTrue(); + timestamps.Should().BeInDescendingOrder(); + + timestamps + .Should() + .BeEquivalentTo( + [ + new DateTimeOffset(2021, 09, 08, 14, 26, 35, TimeSpan.Zero), + new DateTimeOffset(2021, 07, 24, 14, 52, 40, TimeSpan.Zero), + new DateTimeOffset(2021, 07, 24, 14, 52, 39, TimeSpan.Zero), + new DateTimeOffset(2021, 07, 24, 14, 52, 38, TimeSpan.Zero), + new DateTimeOffset(2021, 07, 24, 13, 49, 13, TimeSpan.Zero), + ], + o => + o.Using(ctx => + ctx.Subject.Should().BeCloseTo(ctx.Expectation, TimeSpan.FromSeconds(1)) + ) + .WhenTypeIs() + ); + } + [Fact] public async Task I_can_filter_the_export_to_only_include_messages_sent_before_the_specified_date() { diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 607f03c3..8d5ef4c3 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -796,6 +796,10 @@ public class DiscordClient( foreach (var message in messages) { + // Ensure that the messages are in range + if (message.Timestamp <= after?.ToDate()) + yield break; + lastMessage ??= message; // Report progress based on timestamps