From beadc25e61f4d99f8dcc0083cf4a4ea332d29bd7 Mon Sep 17 00:00:00 2001 From: Oleksii Holub Date: Wed, 14 Nov 2018 18:15:07 +0200 Subject: [PATCH] Cleanup GroupMessages --- .../Services/ExportService.TemplateModel.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs b/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs index 0246ccd..10f997c 100644 --- a/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs +++ b/DiscordChatExporter.Core/Services/ExportService.TemplateModel.cs @@ -35,22 +35,23 @@ namespace DiscordChatExporter.Core.Services var buffer = new List(); foreach (var message in messages) { - var groupFirst = buffer.FirstOrDefault(); + // Get first message of the group + var groupFirstMessage = buffer.FirstOrDefault(); // Group break condition var breakCondition = - groupFirst != null && + groupFirstMessage != null && ( - message.Author.Id != groupFirst.Author.Id || - (message.Timestamp - groupFirst.Timestamp).TotalHours > 1 || - message.Timestamp.Hour != groupFirst.Timestamp.Hour || - buffer.Count >= _messageGroupLimit + message.Author.Id != groupFirstMessage.Author.Id || // when author changes + (message.Timestamp - groupFirstMessage.Timestamp).TotalHours > 1 || // when difference in timestamps is an hour or more + message.Timestamp.Hour != groupFirstMessage.Timestamp.Hour || // when the timestamp's hour changes + buffer.Count >= _messageGroupLimit // when group is full ); // If condition is true - flush buffer 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 buffer = new List(); @@ -65,8 +66,8 @@ namespace DiscordChatExporter.Core.Services // Add what's remaining in buffer if (buffer.Any()) { - var groupFirst = buffer.First(); - var group = new MessageGroup(groupFirst.Author, groupFirst.Timestamp, buffer); + var groupFirstMessage = buffer.First(); + var group = new MessageGroup(groupFirstMessage.Author, groupFirstMessage.Timestamp, buffer); yield return group; }