Refactor: Replace EmailAttachmentContext with DTO
Replaced `EmailAttachmentContext` with `EmailAttachmentDto` across the codebase to align with the updated DTO naming convention. - Renamed `EmailAttachmentContext` to `EmailAttachmentDto`. - Updated property types, method signatures, and return types to use `EmailAttachmentDto`. - Modified XML documentation references to reflect the new class name. - Updated `ReceivedEmailContext` to `ReceivedEmailDto` and adjusted related methods and properties. - Refactored `FetchEmailsAsync` methods and handlers to use `ReceivedEmailDto`. - Adjusted `BuildAttachmentsAsync` and attachment handling logic in `EmailController` and `LimilabsImapEmailService`. This refactor ensures consistency and improves code clarity while maintaining functionality.
This commit is contained in:
@@ -3,7 +3,7 @@ namespace DigitalData.MessagingService.Abstraction;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a single email attachment.
|
/// Represents a single email attachment.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class EmailAttachmentContext
|
public sealed class EmailAttachmentDto
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Display name of the attachment (e.g. "invoice.pdf").
|
/// Display name of the attachment (e.g. "invoice.pdf").
|
||||||
@@ -48,8 +48,8 @@ public record EmailContext
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Optional list of attachments to include with the email.
|
/// Optional list of attachments to include with the email.
|
||||||
/// Each entry may carry its content as a byte array (<see cref="EmailAttachmentContext.Content"/>)
|
/// Each entry may carry its content as a byte array (<see cref="EmailAttachmentDto.Content"/>)
|
||||||
/// or reference a file on disk via <see cref="EmailAttachmentContext.FilePath"/>.
|
/// or reference a file on disk via <see cref="EmailAttachmentDto.FilePath"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<EmailAttachmentContext> Attachments { get; set; } = [];
|
public IEnumerable<EmailAttachmentDto> Attachments { get; set; } = [];
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@ namespace DigitalData.MessagingService.Abstraction;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents an email message received via IMAP.
|
/// Represents an email message received via IMAP.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed record ReceivedEmailContext
|
public sealed record ReceivedEmailDto
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Unique identifier of the message on the IMAP server (UID).
|
/// Unique identifier of the message on the IMAP server (UID).
|
||||||
@@ -81,9 +81,9 @@ public sealed record ReceivedEmailContext
|
|||||||
/// Attachments included with this message.
|
/// Attachments included with this message.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
#if NET
|
#if NET
|
||||||
public IEnumerable<EmailAttachmentContext> Attachments { get; init; } = [];
|
public IEnumerable<EmailAttachmentDto> Attachments { get; init; } = [];
|
||||||
#else
|
#else
|
||||||
public IEnumerable<EmailAttachmentContext> Attachments { get; set; } = [];
|
public IEnumerable<EmailAttachmentDto> Attachments { get; set; } = [];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -17,7 +17,7 @@ public interface IImapEmailService
|
|||||||
/// Set to <see langword="false"/> for a non-destructive read (uses <c>BODY.PEEK</c> internally).
|
/// Set to <see langword="false"/> for a non-destructive read (uses <c>BODY.PEEK</c> internally).
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <param name="cancellationToken">Cancellation token.</param>
|
/// <param name="cancellationToken">Cancellation token.</param>
|
||||||
Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
Task<IEnumerable<ReceivedEmailDto>> FetchEmailsAsync(
|
||||||
EmailAccountDto account,
|
EmailAccountDto account,
|
||||||
MailSearchFilter filter,
|
MailSearchFilter filter,
|
||||||
CancellationToken cancellationToken = default);
|
CancellationToken cancellationToken = default);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace DigitalData.MessagingService.Application.EmailReceiving.Queries;
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Query to fetch emails from an IMAP mailbox.
|
/// Query to fetch emails from an IMAP mailbox.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
|
public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailDto>>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Identifies the email account to use.
|
/// Identifies the email account to use.
|
||||||
@@ -25,9 +25,9 @@ public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
|
|||||||
|
|
||||||
public class FetchEmailsQueryHandler(
|
public class FetchEmailsQueryHandler(
|
||||||
ISender Sender,
|
ISender Sender,
|
||||||
IImapEmailService ImapService) : IRequestHandler<FetchEmailsQuery, IEnumerable<ReceivedEmailContext>>
|
IImapEmailService ImapService) : IRequestHandler<FetchEmailsQuery, IEnumerable<ReceivedEmailDto>>
|
||||||
{
|
{
|
||||||
public async Task<IEnumerable<ReceivedEmailContext>> Handle(FetchEmailsQuery request, CancellationToken cancellationToken)
|
public async Task<IEnumerable<ReceivedEmailDto>> Handle(FetchEmailsQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var account = await Sender.Send(request.Account, cancellationToken)
|
var account = await Sender.Send(request.Account, cancellationToken)
|
||||||
?? throw new NotFoundException(
|
?? throw new NotFoundException(
|
||||||
|
|||||||
@@ -35,13 +35,13 @@ public record PublishEmailCommand : IRequest<Guid>
|
|||||||
public bool IsHtml { get; init; } = true;
|
public bool IsHtml { get; init; } = true;
|
||||||
|
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
internal IEnumerable<EmailAttachmentContext> Attachments { get; private init; } = [];
|
internal IEnumerable<EmailAttachmentDto> Attachments { get; private init; } = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns a new command instance with the supplied attachments.
|
/// Returns a new command instance with the supplied attachments.
|
||||||
/// Called by the controller after resolving uploaded files.
|
/// Called by the controller after resolving uploaded files.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public PublishEmailCommand WithAttachments(IEnumerable<EmailAttachmentContext> attachments)
|
public PublishEmailCommand WithAttachments(IEnumerable<EmailAttachmentDto> attachments)
|
||||||
=> this with { Attachments = attachments };
|
=> this with { Attachments = attachments };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ public class LimilabsEmailService(
|
|||||||
return message.ToString();
|
return message.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void AddAttachments(MailBuilder builder, IEnumerable<EmailAttachmentContext> attachments)
|
private static void AddAttachments(MailBuilder builder, IEnumerable<EmailAttachmentDto> attachments)
|
||||||
{
|
{
|
||||||
foreach (var attachment in attachments)
|
foreach (var attachment in attachments)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class LimilabsImapEmailService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Public API
|
// Public API
|
||||||
public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
public async Task<IEnumerable<ReceivedEmailDto>> FetchEmailsAsync(
|
||||||
EmailAccountDto account,
|
EmailAccountDto account,
|
||||||
MailSearchFilter filter,
|
MailSearchFilter filter,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
@@ -60,7 +60,7 @@ public class LimilabsImapEmailService(
|
|||||||
if (uids.Count == 0)
|
if (uids.Count == 0)
|
||||||
return [];
|
return [];
|
||||||
|
|
||||||
var results = new List<ReceivedEmailContext>(uids.Count);
|
var results = new List<ReceivedEmailDto>(uids.Count);
|
||||||
|
|
||||||
foreach (var uid in uids)
|
foreach (var uid in uids)
|
||||||
{
|
{
|
||||||
@@ -77,11 +77,11 @@ public class LimilabsImapEmailService(
|
|||||||
var mail = new MailBuilder().CreateFromEml(eml);
|
var mail = new MailBuilder().CreateFromEml(eml);
|
||||||
var flags = await imap.GetFlagsByUIDAsync(uid, cancellationToken);
|
var flags = await imap.GetFlagsByUIDAsync(uid, cancellationToken);
|
||||||
|
|
||||||
var attachments = new List<EmailAttachmentContext>();
|
var attachments = new List<EmailAttachmentDto>();
|
||||||
|
|
||||||
foreach (var att in mail.Attachments)
|
foreach (var att in mail.Attachments)
|
||||||
{
|
{
|
||||||
attachments.Add(new EmailAttachmentContext
|
attachments.Add(new EmailAttachmentDto
|
||||||
{
|
{
|
||||||
FileName = att.FileName ?? "attachment",
|
FileName = att.FileName ?? "attachment",
|
||||||
Content = att.Data,
|
Content = att.Data,
|
||||||
@@ -93,7 +93,7 @@ public class LimilabsImapEmailService(
|
|||||||
|
|
||||||
foreach (var vis in mail.Visuals)
|
foreach (var vis in mail.Visuals)
|
||||||
{
|
{
|
||||||
attachments.Add(new EmailAttachmentContext
|
attachments.Add(new EmailAttachmentDto
|
||||||
{
|
{
|
||||||
FileName = vis.FileName ?? "inline",
|
FileName = vis.FileName ?? "inline",
|
||||||
Content = vis.Data,
|
Content = vis.Data,
|
||||||
@@ -103,7 +103,7 @@ public class LimilabsImapEmailService(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ReceivedEmailContext
|
return new ReceivedEmailDto
|
||||||
{
|
{
|
||||||
Uid = uid,
|
Uid = uid,
|
||||||
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
||||||
|
|||||||
@@ -56,21 +56,21 @@ public class EmailController(IMediator mediator) : ControllerBase
|
|||||||
return Accepted(new { Id = eventId });
|
return Accepted(new { Id = eventId });
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<IEnumerable<EmailAttachmentContext>> BuildAttachmentsAsync(
|
private static async Task<IEnumerable<EmailAttachmentDto>> BuildAttachmentsAsync(
|
||||||
IFormFileCollection? files,
|
IFormFileCollection? files,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (files is null || files.Count == 0)
|
if (files is null || files.Count == 0)
|
||||||
return [];
|
return [];
|
||||||
|
|
||||||
var result = new List<EmailAttachmentContext>(files.Count);
|
var result = new List<EmailAttachmentDto>(files.Count);
|
||||||
|
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
using var ms = new MemoryStream();
|
using var ms = new MemoryStream();
|
||||||
await file.CopyToAsync(ms, cancellationToken);
|
await file.CopyToAsync(ms, cancellationToken);
|
||||||
|
|
||||||
result.Add(new EmailAttachmentContext
|
result.Add(new EmailAttachmentDto
|
||||||
{
|
{
|
||||||
FileName = file.FileName,
|
FileName = file.FileName,
|
||||||
Content = ms.ToArray(),
|
Content = ms.ToArray(),
|
||||||
|
|||||||
Reference in New Issue
Block a user