Update message grouping algorithm to match Discord's

Fixes #152
pull/162/head
Alexey Golub 6 years ago
parent 1f6d1f21e6
commit 9b78472e05

@ -34,23 +34,19 @@ namespace DiscordChatExporter.Core.Services
var buffer = new List<Message>(); var buffer = new List<Message>();
foreach (var message in messages) foreach (var message in messages)
{ {
// Get first message of the group
var groupFirstMessage = buffer.FirstOrDefault();
// Group break condition // Group break condition
var breakCondition = var breakCondition =
groupFirstMessage != null && buffer.Any() &&
( (
message.Author.Id != groupFirstMessage.Author.Id || // when author changes message.Author.Id != buffer.First().Author.Id || // when author changes
(message.Timestamp - groupFirstMessage.Timestamp).TotalHours > 1 || // when difference in timestamps is an hour or more (message.Timestamp - buffer.Last().Timestamp).TotalMinutes > 7 || // when more than 7 minutes passed since last message
message.Timestamp.Hour != groupFirstMessage.Timestamp.Hour || // when the timestamp's hour changes
buffer.Count >= _messageGroupLimit // when group is full buffer.Count >= _messageGroupLimit // when group is full
); );
// If condition is true - flush buffer // If condition is true - flush buffer
if (breakCondition) if (breakCondition)
{ {
var group = new MessageGroup(groupFirstMessage.Author, groupFirstMessage.Timestamp, buffer); var group = new MessageGroup(buffer.First().Author, buffer.First().Timestamp, buffer);
// Reset the buffer instead of clearing to avoid mutations on existing references // Reset the buffer instead of clearing to avoid mutations on existing references
buffer = new List<Message>(); buffer = new List<Message>();
@ -65,8 +61,7 @@ namespace DiscordChatExporter.Core.Services
// Add what's remaining in buffer // Add what's remaining in buffer
if (buffer.Any()) if (buffer.Any())
{ {
var groupFirstMessage = buffer.First(); var group = new MessageGroup(buffer.First().Author, buffer.First().Timestamp, buffer);
var group = new MessageGroup(groupFirstMessage.Author, groupFirstMessage.Timestamp, buffer);
yield return group; yield return group;
} }

Loading…
Cancel
Save