Cleanup GroupMessages

pull/123/head
Oleksii Holub 6 years ago
parent 8bc29c4928
commit beadc25e61

@ -35,22 +35,23 @@ namespace DiscordChatExporter.Core.Services
var buffer = new List<Message>(); var buffer = new List<Message>();
foreach (var message in messages) foreach (var message in messages)
{ {
var groupFirst = buffer.FirstOrDefault(); // Get first message of the group
var groupFirstMessage = buffer.FirstOrDefault();
// Group break condition // Group break condition
var breakCondition = var breakCondition =
groupFirst != null && groupFirstMessage != null &&
( (
message.Author.Id != groupFirst.Author.Id || message.Author.Id != groupFirstMessage.Author.Id || // when author changes
(message.Timestamp - groupFirst.Timestamp).TotalHours > 1 || (message.Timestamp - groupFirstMessage.Timestamp).TotalHours > 1 || // when difference in timestamps is an hour or more
message.Timestamp.Hour != groupFirst.Timestamp.Hour || message.Timestamp.Hour != groupFirstMessage.Timestamp.Hour || // when the timestamp's hour changes
buffer.Count >= _messageGroupLimit 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(groupFirst.Author, groupFirst.Timestamp, buffer); var group = new MessageGroup(groupFirstMessage.Author, groupFirstMessage.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 +66,8 @@ namespace DiscordChatExporter.Core.Services
// Add what's remaining in buffer // Add what's remaining in buffer
if (buffer.Any()) if (buffer.Any())
{ {
var groupFirst = buffer.First(); var groupFirstMessage = buffer.First();
var group = new MessageGroup(groupFirst.Author, groupFirst.Timestamp, buffer); var group = new MessageGroup(groupFirstMessage.Author, groupFirstMessage.Timestamp, buffer);
yield return group; yield return group;
} }

Loading…
Cancel
Save