Refactor attachment handling in email fetching

Consolidate `WithAttachments` behavior into `MailSearchFilter` to simplify the API and reduce redundancy.

- Removed `withAttachments` parameter from `FetchEmailsAsync` in `IImapEmailService`.
- Added `WithAttachments` property to `MailSearchFilter` to control attachment inclusion.
- Removed `WithAttachments` property from `FetchEmailsQuery` as it is now encapsulated in `MailSearchFilter`.
- Updated `FetchEmailsQueryHandler` to use `MailSearchFilter` for attachment handling.
- Refactored `LimilabsImapEmailService` to use `MailSearchFilter.WithAttachments` for mapping email data.

These changes improve maintainability and clarity by centralizing attachment-related options in `MailSearchFilter`.
This commit is contained in:
2026-08-10 12:05:27 +02:00
parent 763ba67d34
commit 74ec00ddd3
4 changed files with 7 additions and 11 deletions

View File

@@ -13,12 +13,10 @@ public interface IImapEmailService
/// </summary> /// </summary>
/// <param name="account">Account whose IMAP settings will be used.</param> /// <param name="account">Account whose IMAP settings will be used.</param>
/// <param name="filter">Filter to apply when fetching emails.</param> /// <param name="filter">Filter to apply when fetching emails.</param>
/// <param name="withAttachments">When <see langword="true"/>, attachment data is included in the results; otherwise attachments are omitted. Defaults to <see langword="false"/>.</param>
/// <param name="cancellationToken">Cancellation token.</param> /// <param name="cancellationToken">Cancellation token.</param>
Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync( Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
EmailAccountDto account, EmailAccountDto account,
MailSearchFilter filter, MailSearchFilter filter,
bool withAttachments = false,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);
/// <summary> /// <summary>

View File

@@ -25,6 +25,12 @@ public record MailSearchFilter
/// </summary> /// </summary>
public bool HasAttachments { get; init; } = false; public bool HasAttachments { get; init; } = false;
/// <summary>
/// When <see langword="true"/>, attachment data is included in the results; otherwise attachments are omitted.
/// Defaults to <see langword="false"/>.
/// </summary>
public bool WithAttachments { get; init; } = false;
/// <summary> /// <summary>
/// Maximum number of messages to retrieve. <c>0</c> means unlimited. /// Maximum number of messages to retrieve. <c>0</c> means unlimited.
/// Applied after sorting; defaults to <c>50</c>. /// Applied after sorting; defaults to <c>50</c>.

View File

@@ -21,12 +21,6 @@ public record FetchEmailsQuery : IRequest<IEnumerable<ReceivedEmailContext>>
/// Mail query used to filter and limit the emails retrieved. /// Mail query used to filter and limit the emails retrieved.
/// </summary> /// </summary>
public MailSearchFilter Mail { get; init; } = new(); public MailSearchFilter Mail { get; init; } = new();
/// <summary>
/// When <see langword="true"/>, attachment data is included in the results; otherwise attachments are omitted.
/// Defaults to <see langword="false"/>.
/// </summary>
public bool WithAttachments { get; init; } = false;
} }
public class FetchEmailsQueryHandler( public class FetchEmailsQueryHandler(
@@ -46,7 +40,6 @@ public class FetchEmailsQueryHandler(
return await ImapService.FetchEmailsAsync( return await ImapService.FetchEmailsAsync(
account, account,
request.Mail, request.Mail,
request.WithAttachments,
cancellationToken); cancellationToken);
} }
} }

View File

@@ -27,7 +27,6 @@ public class LimilabsImapEmailService(
public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync( public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
EmailAccountDto account, EmailAccountDto account,
MailSearchFilter filter, MailSearchFilter filter,
bool withAttachments = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
using var imap = new Imap(); using var imap = new Imap();
@@ -104,7 +103,7 @@ public class LimilabsImapEmailService(
if (filter.HasAttachments && mail.Attachments.Count == 0 && mail.Visuals.Count == 0) if (filter.HasAttachments && mail.Attachments.Count == 0 && mail.Visuals.Count == 0)
continue; continue;
results.Add(MapToContext(uid, mail, flags, withAttachments)); results.Add(MapToContext(uid, mail, flags, filter.WithAttachments));
} }
catch (Exception ex) catch (Exception ex)
{ {