From 74ec00ddd3507e6bc3ea45fab93be46a9c4239aa Mon Sep 17 00:00:00 2001 From: TekH Date: Mon, 10 Aug 2026 12:05:27 +0200 Subject: [PATCH] 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`. --- .../Common/Interfaces/IImapEmailService.cs | 2 -- .../Common/Models/MailSearch/MailSearchFilter.cs | 6 ++++++ .../EmailReceiving/Queries/FetchEmailsQuery.cs | 7 ------- .../Services/LimilabsImapEmailService.cs | 3 +-- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IImapEmailService.cs b/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IImapEmailService.cs index f67a0b4..0fd507f 100644 --- a/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IImapEmailService.cs +++ b/src/core/DigitalData.MessagingService.Application/Common/Interfaces/IImapEmailService.cs @@ -13,12 +13,10 @@ public interface IImapEmailService /// /// Account whose IMAP settings will be used. /// Filter to apply when fetching emails. - /// When , attachment data is included in the results; otherwise attachments are omitted. Defaults to . /// Cancellation token. Task> FetchEmailsAsync( EmailAccountDto account, MailSearchFilter filter, - bool withAttachments = false, CancellationToken cancellationToken = default); /// diff --git a/src/core/DigitalData.MessagingService.Application/Common/Models/MailSearch/MailSearchFilter.cs b/src/core/DigitalData.MessagingService.Application/Common/Models/MailSearch/MailSearchFilter.cs index 9f37b3d..70e31a6 100644 --- a/src/core/DigitalData.MessagingService.Application/Common/Models/MailSearch/MailSearchFilter.cs +++ b/src/core/DigitalData.MessagingService.Application/Common/Models/MailSearch/MailSearchFilter.cs @@ -25,6 +25,12 @@ public record MailSearchFilter /// public bool HasAttachments { get; init; } = false; + /// + /// When , attachment data is included in the results; otherwise attachments are omitted. + /// Defaults to . + /// + public bool WithAttachments { get; init; } = false; + /// /// Maximum number of messages to retrieve. 0 means unlimited. /// Applied after sorting; defaults to 50. diff --git a/src/core/DigitalData.MessagingService.Application/EmailReceiving/Queries/FetchEmailsQuery.cs b/src/core/DigitalData.MessagingService.Application/EmailReceiving/Queries/FetchEmailsQuery.cs index bba1e4d..9b1a6bb 100644 --- a/src/core/DigitalData.MessagingService.Application/EmailReceiving/Queries/FetchEmailsQuery.cs +++ b/src/core/DigitalData.MessagingService.Application/EmailReceiving/Queries/FetchEmailsQuery.cs @@ -21,12 +21,6 @@ public record FetchEmailsQuery : IRequest> /// Mail query used to filter and limit the emails retrieved. /// public MailSearchFilter Mail { get; init; } = new(); - - /// - /// When , attachment data is included in the results; otherwise attachments are omitted. - /// Defaults to . - /// - public bool WithAttachments { get; init; } = false; } public class FetchEmailsQueryHandler( @@ -46,7 +40,6 @@ public class FetchEmailsQueryHandler( return await ImapService.FetchEmailsAsync( account, request.Mail, - request.WithAttachments, cancellationToken); } } diff --git a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs index 318e987..6fec386 100644 --- a/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs +++ b/src/infrastructure/DigitalData.MessagingService.Infrastructure/Services/LimilabsImapEmailService.cs @@ -27,7 +27,6 @@ public class LimilabsImapEmailService( public async Task> FetchEmailsAsync( EmailAccountDto account, MailSearchFilter filter, - bool withAttachments = false, CancellationToken cancellationToken = default) { using var imap = new Imap(); @@ -104,7 +103,7 @@ public class LimilabsImapEmailService( if (filter.HasAttachments && mail.Attachments.Count == 0 && mail.Visuals.Count == 0) continue; - results.Add(MapToContext(uid, mail, flags, withAttachments)); + results.Add(MapToContext(uid, mail, flags, filter.WithAttachments)); } catch (Exception ex) {