Simplify IMAP email fetching API and refactor logic

Removed `markAsSeen` parameter from `FetchEmailsAsync` and
`FetchEmailByUidAsync` methods in `IImapEmailService` to
simplify the API. Updated `MailSearchFilter` to make `MaxCount`
nullable for greater flexibility.

Removed `markAsSeen` from `FetchEmailByUidQuery` and
`FetchEmailsQuery` records and their handlers. Deleted
`FetchEmailByUidQueryValidator` as it is no longer needed.

Refactored `LimilabsImapEmailService`:
- Introduced `FetchEmailUidsAsync` to centralize UID fetching logic.
- Simplified `FetchEmailByUidAsync` using a new helper method.
- Consolidated connection, authentication, and folder selection
  into reusable private methods.
- Removed redundant code for search criteria and flag fetching.

Removed `IsSeen` from `ReceivedEmailContext` and improved
overall code readability and maintainability by reducing
duplication and centralizing logic.
This commit is contained in:
2026-08-12 10:04:48 +02:00
parent f3761e96d9
commit f521683608
6 changed files with 124 additions and 212 deletions

View File

@@ -13,7 +13,6 @@ public interface IImapEmailService
/// </summary>
/// <param name="account">Account whose IMAP settings will be used.</param>
/// <param name="filter">Filter to apply when fetching emails.</param>
/// <param name="markAsSeen">
/// When <see langword="true"/> (default), fetched messages are marked as <c>\Seen</c> on the server.
/// Set to <see langword="false"/> for a non-destructive read (uses <c>BODY.PEEK</c> internally).
/// </param>
@@ -21,7 +20,6 @@ public interface IImapEmailService
Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
EmailAccountDto account,
MailSearchFilter filter,
bool markAsSeen = true,
CancellationToken cancellationToken = default);
/// <summary>
@@ -35,7 +33,6 @@ public interface IImapEmailService
/// <summary>
/// Fetches a single email by its UID.
/// </summary>
/// <param name="markAsSeen">
/// When <see langword="true"/> (default), the message is marked as <c>\Seen</c> on the server.
/// Set to <see langword="false"/> for a non-destructive read.
/// </param>
@@ -44,7 +41,6 @@ public interface IImapEmailService
long uid,
string folder = "INBOX",
bool withAttachments = false,
bool markAsSeen = true,
CancellationToken cancellationToken = default);
/// <summary>

View File

@@ -29,7 +29,7 @@ public record MailSearchFilter
/// Maximum number of messages to retrieve. <c>0</c> means unlimited.
/// Applied after sorting; defaults to <c>50</c>.
/// </summary>
public int MaxCount { get; init; } = 50;
public int? MaxCount { get; init; } = null;
/// <summary>
/// Controls the order of the returned messages. Defaults to <see cref="MailSortOrder.NewestFirst"/>.

View File

@@ -36,11 +36,6 @@ public record FetchEmailByUidQuery : IRequest<ReceivedEmailContext?>
/// When <see langword="true"/>, attachment data is included in the result.
/// </summary>
public bool WithAttachments { get; init; } = false;
/// <summary>
/// When <see langword="true"/> (default), the message is marked as <c>\Seen</c> on the server.
/// Set to <see langword="false"/> for a non-destructive peek.
/// </summary>
public bool MarkAsSeen { get; init; } = true;
}
public class FetchEmailByUidQueryHandler(
@@ -62,7 +57,6 @@ public class FetchEmailByUidQueryHandler(
(long)request.Uid!,
request.Folder,
request.WithAttachments,
request.MarkAsSeen,
cancellationToken);
}
}

View File

@@ -21,12 +21,6 @@ public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
/// Mail query used to filter and limit the emails retrieved.
/// </summary>
public MailSearchFilter Mail { get; init; } = new();
/// <summary>
/// When <see langword="true"/> (default), fetched messages are marked as <c>\Seen</c> on the server.
/// Set to <see langword="false"/> for a non-destructive peek.
/// </summary>
public bool MarkAsSeen { get; init; } = true;
}
public class FetchEmailsQueryHandler(
@@ -46,7 +40,6 @@ public class FetchEmailsQueryHandler(
return await ImapService.FetchEmailsAsync(
account,
request.Mail,
request.MarkAsSeen,
cancellationToken);
}
}

View File

@@ -1,27 +0,0 @@
using DigitalData.MessagingService.Application.EmailReceiving.Queries;
using FluentValidation;
namespace DigitalData.MessagingService.Application.EmailReceiving.Validators;
/// <summary>
/// Validates a <see cref="FetchEmailByUidQuery"/> before it is handled by <see cref="FetchEmailByUidQueryHandler"/>.
/// </summary>
public class FetchEmailByUidQueryValidator : AbstractValidator<FetchEmailByUidQuery>
{
public FetchEmailByUidQueryValidator()
{
RuleFor(x => x.Account)
.NotNull()
.WithMessage("Account query must not be null.");
RuleFor(x => x.Uid)
.NotNull()
.WithMessage("UID must be provided. Use WithUid() to set the UID before dispatching the query.")
.GreaterThan(0)
.WithMessage("UID must be greater than 0.");
RuleFor(x => x.Folder)
.NotEmpty()
.WithMessage("Folder must not be empty.");
}
}