Improve message date filtering, convert from snowflake to get the 'to' message ID

pull/26/head
Alexey Golub 7 years ago
parent cc565598d3
commit 60a5a69aa4

@ -108,7 +108,7 @@ namespace DiscordChatExporter.Services
// We are going backwards from last message to first // We are going backwards from last message to first
// collecting everything between them in batches // collecting everything between them in batches
string beforeId = null; var beforeId = to != null ? DateTimeToSnowflake(to.Value) : null;
while (true) while (true)
{ {
// Form request url // Form request url
@ -126,6 +126,14 @@ namespace DiscordChatExporter.Services
string currentMessageId = null; string currentMessageId = null;
foreach (var message in messages) foreach (var message in messages)
{ {
// Break when the message is older than from date
if (from != null && message.TimeStamp < from)
{
currentMessageId = null;
break;
}
// Add message
result.Add(message); result.Add(message);
currentMessageId = message.Id; currentMessageId = message.Id;
} }
@ -134,10 +142,6 @@ namespace DiscordChatExporter.Services
if (currentMessageId == null) if (currentMessageId == null)
break; break;
// If last message is older than from date - break
if (from != null && result.Last().TimeStamp < from)
break;
// Otherwise offset the next request // Otherwise offset the next request
beforeId = currentMessageId; beforeId = currentMessageId;
} }
@ -145,12 +149,6 @@ namespace DiscordChatExporter.Services
// Messages appear newest first, we need to reverse // Messages appear newest first, we need to reverse
result.Reverse(); result.Reverse();
// Filter
if (from != null)
result.RemoveAll(m => m.TimeStamp < from);
if (to != null)
result.RemoveAll(m => m.TimeStamp > to);
return result; return result;
} }
@ -171,6 +169,14 @@ namespace DiscordChatExporter.Services
public partial class DataService public partial class DataService
{ {
private static string DateTimeToSnowflake(DateTime dateTime)
{
const long epoch = 62135596800000;
var unixTime = dateTime.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond - epoch;
var value = ((ulong) unixTime - 1420070400000UL) << 22;
return value.ToString();
}
private static Guild ParseGuild(JToken token) private static Guild ParseGuild(JToken token)
{ {
var id = token.Value<string>("id"); var id = token.Value<string>("id");

Loading…
Cancel
Save