|
|
|
@ -6,6 +6,7 @@ using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DiscordChatExporter.Core.Discord.Data;
|
|
|
|
|
using DiscordChatExporter.Core.Exporting.Writers.Html;
|
|
|
|
|
using WebMarkupMin.Core;
|
|
|
|
|
|
|
|
|
|
namespace DiscordChatExporter.Core.Exporting.Writers;
|
|
|
|
|
|
|
|
|
@ -14,6 +15,7 @@ internal class HtmlMessageWriter : MessageWriter
|
|
|
|
|
private readonly TextWriter _writer;
|
|
|
|
|
private readonly string _themeName;
|
|
|
|
|
|
|
|
|
|
private readonly HtmlMinifier _minifier = new();
|
|
|
|
|
private readonly List<Message> _messageGroup = new();
|
|
|
|
|
|
|
|
|
|
public HtmlMessageWriter(Stream stream, ExportContext context, string themeName)
|
|
|
|
@ -23,22 +25,22 @@ internal class HtmlMessageWriter : MessageWriter
|
|
|
|
|
_themeName = themeName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool CanJoinGroup(Message message)
|
|
|
|
|
{
|
|
|
|
|
var lastMessage = _messageGroup.LastOrDefault();
|
|
|
|
|
if (lastMessage is null)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
// Must be from the same author
|
|
|
|
|
lastMessage.Author.Id == message.Author.Id &&
|
|
|
|
|
// Author's name must not have changed between messages
|
|
|
|
|
string.Equals(lastMessage.Author.FullName, message.Author.FullName, StringComparison.Ordinal) &&
|
|
|
|
|
// Duration between messages must be 7 minutes or less
|
|
|
|
|
(message.Timestamp - lastMessage.Timestamp).Duration().TotalMinutes <= 7 &&
|
|
|
|
|
// Other message must not be a reply
|
|
|
|
|
message.Reference is null;
|
|
|
|
|
}
|
|
|
|
|
private bool CanJoinGroup(Message message) =>
|
|
|
|
|
// First message in the group can always join
|
|
|
|
|
_messageGroup.LastOrDefault() is not { } lastMessage ||
|
|
|
|
|
// Must be from the same author
|
|
|
|
|
lastMessage.Author.Id == message.Author.Id &&
|
|
|
|
|
// Author's name must not have changed between messages
|
|
|
|
|
string.Equals(lastMessage.Author.FullName, message.Author.FullName, StringComparison.Ordinal) &&
|
|
|
|
|
// Duration between messages must be 7 minutes or less
|
|
|
|
|
(message.Timestamp - lastMessage.Timestamp).Duration().TotalMinutes <= 7 &&
|
|
|
|
|
// Other message must not be a reply
|
|
|
|
|
message.Reference is null;
|
|
|
|
|
|
|
|
|
|
// Use <!--wmm:ignore--> to preserve blocks of code inside the templates
|
|
|
|
|
private string Minify(string html) => _minifier
|
|
|
|
|
.Minify(html, false)
|
|
|
|
|
.MinifiedContent;
|
|
|
|
|
|
|
|
|
|
public override async ValueTask WritePreambleAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
@ -47,7 +49,9 @@ internal class HtmlMessageWriter : MessageWriter
|
|
|
|
|
// We are not writing directly to output because Razor
|
|
|
|
|
// does not actually do asynchronous writes to stream.
|
|
|
|
|
await _writer.WriteLineAsync(
|
|
|
|
|
await PreambleTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
Minify(
|
|
|
|
|
await PreambleTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -60,7 +64,9 @@ internal class HtmlMessageWriter : MessageWriter
|
|
|
|
|
// We are not writing directly to output because Razor
|
|
|
|
|
// does not actually do asynchronous writes to stream.
|
|
|
|
|
await _writer.WriteLineAsync(
|
|
|
|
|
await MessageGroupTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
Minify(
|
|
|
|
|
await MessageGroupTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -96,7 +102,9 @@ internal class HtmlMessageWriter : MessageWriter
|
|
|
|
|
// We are not writing directly to output because Razor
|
|
|
|
|
// does not actually do asynchronous writes to stream.
|
|
|
|
|
await _writer.WriteLineAsync(
|
|
|
|
|
await PostambleTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
Minify(
|
|
|
|
|
await PostambleTemplate.RenderAsync(templateContext, cancellationToken)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|