From 6f90c367b9571c5f65dedb4fb5665529f282d590 Mon Sep 17 00:00:00 2001 From: Tyrrrz Date: Tue, 23 Mar 2021 20:54:06 +0200 Subject: [PATCH] Clamp progress in DiscordClient.GetMessagesAsync(...) --- .../Discord/DiscordClient.cs | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index 41b9aa0..8bd8345 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -11,6 +11,7 @@ using DiscordChatExporter.Core.Utils; using DiscordChatExporter.Core.Utils.Extensions; using JsonExtensions.Http; using JsonExtensions.Reading; +using Tyrrrz.Extensions; namespace DiscordChatExporter.Core.Discord { @@ -259,11 +260,23 @@ namespace DiscordChatExporter.Core.Discord if (message.Timestamp > lastMessage.Timestamp) yield break; - // Report progress based on the duration of parsed messages divided by total - progress?.Report( - (message.Timestamp - firstMessage.Timestamp) / - (lastMessage.Timestamp - firstMessage.Timestamp) - ); + // Report progress based on the duration of exported messages divided by total + if (progress is not null) + { + var exportedDuration = (message.Timestamp - firstMessage.Timestamp).Duration(); + var totalDuration = (lastMessage.Timestamp - firstMessage.Timestamp).Duration(); + + if (totalDuration > TimeSpan.Zero) + { + progress.Report(exportedDuration / totalDuration); + } + // Avoid division by zero if all messages have the exact same timestamp + // (which can happen easily if there's only one message in the channel) + else + { + progress.Report(1); + } + } yield return message; currentAfter = message.Id;