Convert some classes to use primary constructors

pull/1160/head
Tyrrrz 1 year ago
parent 743db67f91
commit 73574ade48

@ -8,11 +8,9 @@ using Gress;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
public class ChannelExporter public class ChannelExporter(DiscordClient discord)
{ {
private readonly DiscordClient _discord; private readonly DiscordClient _discord = discord;
public ChannelExporter(DiscordClient discord) => _discord = discord;
public async ValueTask ExportChannelAsync( public async ValueTask ExportChannelAsync(
ExportRequest request, ExportRequest request,

@ -8,15 +8,10 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal partial class CsvMessageWriter : MessageWriter internal partial class CsvMessageWriter(Stream stream, ExportContext context)
: MessageWriter(stream, context)
{ {
private readonly TextWriter _writer; private readonly TextWriter _writer = new StreamWriter(stream);
public CsvMessageWriter(Stream stream, ExportContext context)
: base(stream, context)
{
_writer = new StreamWriter(stream);
}
private async ValueTask<string> FormatMarkdownAsync( private async ValueTask<string> FormatMarkdownAsync(
string markdown, string markdown,

@ -14,7 +14,7 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal partial class ExportAssetDownloader internal partial class ExportAssetDownloader(string workingDirPath, bool reuse)
{ {
private static readonly AsyncKeyedLocker<string> Locker = private static readonly AsyncKeyedLocker<string> Locker =
new(o => new(o =>
@ -23,18 +23,12 @@ internal partial class ExportAssetDownloader
o.PoolInitialFill = 1; o.PoolInitialFill = 1;
}); });
private readonly string _workingDirPath; private readonly string _workingDirPath = workingDirPath;
private readonly bool _reuse; private readonly bool _reuse = reuse;
// File paths of the previously downloaded assets // File paths of the previously downloaded assets
private readonly Dictionary<string, string> _previousPathsByUrl = new(StringComparer.Ordinal); private readonly Dictionary<string, string> _previousPathsByUrl = new(StringComparer.Ordinal);
public ExportAssetDownloader(string workingDirPath, bool reuse)
{
_workingDirPath = workingDirPath;
_reuse = reuse;
}
public async ValueTask<string> DownloadAsync( public async ValueTask<string> DownloadAsync(
string url, string url,
CancellationToken cancellationToken = default CancellationToken cancellationToken = default

@ -12,27 +12,18 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal class ExportContext internal class ExportContext(DiscordClient discord, ExportRequest request)
{ {
private readonly Dictionary<Snowflake, Member?> _membersById = new(); private readonly Dictionary<Snowflake, Member?> _membersById = new();
private readonly Dictionary<Snowflake, Channel> _channelsById = new(); private readonly Dictionary<Snowflake, Channel> _channelsById = new();
private readonly Dictionary<Snowflake, Role> _rolesById = new(); private readonly Dictionary<Snowflake, Role> _rolesById = new();
private readonly ExportAssetDownloader _assetDownloader;
public DiscordClient Discord { get; } private readonly ExportAssetDownloader _assetDownloader =
new(request.AssetsDirPath, request.ShouldReuseAssets);
public ExportRequest Request { get; } public DiscordClient Discord { get; } = discord;
public ExportContext(DiscordClient discord, ExportRequest request) public ExportRequest Request { get; } = request;
{
Discord = discord;
Request = request;
_assetDownloader = new ExportAssetDownloader(
request.AssetsDirPath,
request.ShouldReuseAssets
);
}
public DateTimeOffset NormalizeDate(DateTimeOffset instant) => public DateTimeOffset NormalizeDate(DateTimeOffset instant) =>
Request.IsUtcNormalizationEnabled ? instant.ToUniversalTime() : instant.ToLocalTime(); Request.IsUtcNormalizationEnabled ? instant.ToUniversalTime() : instant.ToLocalTime();

@ -12,18 +12,15 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal partial class HtmlMarkdownVisitor : MarkdownVisitor internal partial class HtmlMarkdownVisitor(
ExportContext context,
StringBuilder buffer,
bool isJumbo
) : MarkdownVisitor
{ {
private readonly ExportContext _context; private readonly ExportContext _context = context;
private readonly StringBuilder _buffer; private readonly StringBuilder _buffer = buffer;
private readonly bool _isJumbo; private readonly bool _isJumbo = isJumbo;
public HtmlMarkdownVisitor(ExportContext context, StringBuilder buffer, bool isJumbo)
{
_context = context;
_buffer = buffer;
_isJumbo = isJumbo;
}
protected override ValueTask VisitTextAsync( protected override ValueTask VisitTextAsync(
TextNode text, TextNode text,

@ -9,21 +9,15 @@ using WebMarkupMin.Core;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal class HtmlMessageWriter : MessageWriter internal class HtmlMessageWriter(Stream stream, ExportContext context, string themeName)
: MessageWriter(stream, context)
{ {
private readonly TextWriter _writer; private readonly TextWriter _writer = new StreamWriter(stream);
private readonly string _themeName; private readonly string _themeName = themeName;
private readonly HtmlMinifier _minifier = new(); private readonly HtmlMinifier _minifier = new();
private readonly List<Message> _messageGroup = new(); private readonly List<Message> _messageGroup = new();
public HtmlMessageWriter(Stream stream, ExportContext context, string themeName)
: base(stream, context)
{
_writer = new StreamWriter(stream);
_themeName = themeName;
}
private bool CanJoinGroup(Message message) private bool CanJoinGroup(Message message)
{ {
// If the group is empty, any message can join it // If the group is empty, any message can join it

@ -12,26 +12,21 @@ using JsonExtensions.Writing;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal class JsonMessageWriter : MessageWriter internal class JsonMessageWriter(Stream stream, ExportContext context)
: MessageWriter(stream, context)
{ {
private readonly Utf8JsonWriter _writer; private readonly Utf8JsonWriter _writer = new Utf8JsonWriter(
stream,
public JsonMessageWriter(Stream stream, ExportContext context) new JsonWriterOptions
: base(stream, context) {
{ // https://github.com/Tyrrrz/DiscordChatExporter/issues/450
_writer = new Utf8JsonWriter( Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
stream, Indented = true,
new JsonWriterOptions // Validation errors may mask actual failures
{ // https://github.com/Tyrrrz/DiscordChatExporter/issues/413
// https://github.com/Tyrrrz/DiscordChatExporter/issues/450 SkipValidation = true
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }
Indented = true, );
// Validation errors may mask actual failures
// https://github.com/Tyrrrz/DiscordChatExporter/issues/413
SkipValidation = true
}
);
}
private async ValueTask<string> FormatMarkdownAsync( private async ValueTask<string> FormatMarkdownAsync(
string markdown, string markdown,

@ -6,20 +6,15 @@ using DiscordChatExporter.Core.Discord.Data;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal partial class MessageExporter : IAsyncDisposable internal partial class MessageExporter(ExportContext context) : IAsyncDisposable
{ {
private readonly ExportContext _context; private readonly ExportContext _context = context;
private int _partitionIndex; private int _partitionIndex;
private MessageWriter? _writer; private MessageWriter? _writer;
public long MessagesExported { get; private set; } public long MessagesExported { get; private set; }
public MessageExporter(ExportContext context)
{
_context = context;
}
private async ValueTask ResetWriterAsync(CancellationToken cancellationToken = default) private async ValueTask ResetWriterAsync(CancellationToken cancellationToken = default)
{ {
if (_writer is not null) if (_writer is not null)

@ -6,22 +6,16 @@ using DiscordChatExporter.Core.Discord.Data;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal abstract class MessageWriter : IAsyncDisposable internal abstract class MessageWriter(Stream stream, ExportContext context) : IAsyncDisposable
{ {
protected Stream Stream { get; } protected Stream Stream { get; } = stream;
protected ExportContext Context { get; } protected ExportContext Context { get; } = context;
public long MessagesWritten { get; private set; } public long MessagesWritten { get; private set; }
public long BytesWritten => Stream.Length; public long BytesWritten => Stream.Length;
protected MessageWriter(Stream stream, ExportContext context)
{
Stream = stream;
Context = context;
}
public virtual ValueTask WritePreambleAsync(CancellationToken cancellationToken = default) => public virtual ValueTask WritePreambleAsync(CancellationToken cancellationToken = default) =>
default; default;

@ -8,16 +8,11 @@ using DiscordChatExporter.Core.Utils.Extensions;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal partial class PlainTextMarkdownVisitor : MarkdownVisitor internal partial class PlainTextMarkdownVisitor(ExportContext context, StringBuilder buffer)
: MarkdownVisitor
{ {
private readonly ExportContext _context; private readonly ExportContext _context = context;
private readonly StringBuilder _buffer; private readonly StringBuilder _buffer = buffer;
public PlainTextMarkdownVisitor(ExportContext context, StringBuilder buffer)
{
_context = context;
_buffer = buffer;
}
protected override ValueTask VisitTextAsync( protected override ValueTask VisitTextAsync(
TextNode text, TextNode text,

@ -8,15 +8,10 @@ using DiscordChatExporter.Core.Discord.Data.Embeds;
namespace DiscordChatExporter.Core.Exporting; namespace DiscordChatExporter.Core.Exporting;
internal class PlainTextMessageWriter : MessageWriter internal class PlainTextMessageWriter(Stream stream, ExportContext context)
: MessageWriter(stream, context)
{ {
private readonly TextWriter _writer; private readonly TextWriter _writer = new StreamWriter(stream);
public PlainTextMessageWriter(Stream stream, ExportContext context)
: base(stream, context)
{
_writer = new StreamWriter(stream);
}
private async ValueTask<string> FormatMarkdownAsync( private async ValueTask<string> FormatMarkdownAsync(
string markdown, string markdown,

Loading…
Cancel
Save