Fix getting last message when there's no date range

Fixes #137
This commit is contained in:
Alexey Golub 2019-02-10 17:26:18 +02:00
parent e89f2735a0
commit 6531462220

View file

@ -41,10 +41,15 @@ namespace DiscordChatExporter.Core.Services
: new AuthenticationHeaderValue(token.Value); : new AuthenticationHeaderValue(token.Value);
// Add parameters // Add parameters
foreach (var parameter in parameters) foreach (var parameter in parameters.ExceptBlank())
{ {
var key = parameter.SubstringUntil("="); var key = parameter.SubstringUntil("=");
var value = parameter.SubstringAfter("="); var value = parameter.SubstringAfter("=");
// Skip empty values
if (value.IsBlank())
continue;
request.RequestUri = request.RequestUri.SetQueryParameter(key, value); request.RequestUri = request.RequestUri.SetQueryParameter(key, value);
} }
@ -123,13 +128,9 @@ namespace DiscordChatExporter.Core.Services
{ {
var result = new List<Message>(); var result = new List<Message>();
// Get the snowflakes for the selected range
var firstId = from != null ? from.Value.ToSnowflake() : "0";
var lastId = to != null ? to.Value.ToSnowflake() : DateTime.MaxValue.ToSnowflake();
// Get the last message // Get the last message
var response = await GetApiResponseAsync(token, "channels", $"{channelId}/messages", var response = await GetApiResponseAsync(token, "channels", $"{channelId}/messages",
"limit=1", $"before={lastId}"); "limit=1", $"before={to?.ToSnowflake()}");
var lastMessage = response.Select(ParseMessage).FirstOrDefault(); var lastMessage = response.Select(ParseMessage).FirstOrDefault();
// If the last message doesn't exist or it's outside of range - return // If the last message doesn't exist or it's outside of range - return
@ -140,7 +141,7 @@ namespace DiscordChatExporter.Core.Services
} }
// Get other messages // Get other messages
var offsetId = firstId; var offsetId = from?.ToSnowflake() ?? "0";
while (true) while (true)
{ {
// Get message batch // Get message batch