Add support for optional email attachments fetching
Introduced a `withAttachments` parameter to the `FetchEmailsAsync` method in `IImapEmailService` and related layers, allowing callers to include or exclude attachment data when fetching emails. Updated `FetchEmailsQuery` and `FetchEmailsQueryHandler` to propagate this parameter. Refactored `LimilabsImapEmailService` to conditionally process attachments and inline visuals based on the `withAttachments` flag, improving performance when attachments are not required. Replaced `Flag.Unseen` with `Expression.HasFlag(Flag.Unseen)` for better criteria handling. Cleaned up attachment-processing logic for improved readability and maintainability.
This commit is contained in:
@@ -27,6 +27,7 @@ public class LimilabsImapEmailService(
|
||||
public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
||||
EmailAccountDto account,
|
||||
MailSearchFilter filter,
|
||||
bool withAttachments = false,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
@@ -38,7 +39,7 @@ public class LimilabsImapEmailService(
|
||||
List<ICriterion> criterions = [];
|
||||
|
||||
if (filter.UnseenOnly)
|
||||
criterions.Add((ICriterion)Flag.Unseen);
|
||||
criterions.Add(Expression.HasFlag(Flag.Unseen));
|
||||
|
||||
if (filter.SubjectContains is not null)
|
||||
criterions.Add(Expression.Subject(filter.SubjectContains));
|
||||
@@ -103,7 +104,7 @@ public class LimilabsImapEmailService(
|
||||
if (filter.HasAttachments && mail.Attachments.Count == 0 && mail.Visuals.Count == 0)
|
||||
continue;
|
||||
|
||||
results.Add(MapToContext(uid, mail, flags));
|
||||
results.Add(MapToContext(uid, mail, flags, withAttachments));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -179,32 +180,35 @@ public class LimilabsImapEmailService(
|
||||
await imap.SelectAsync(folder);
|
||||
}
|
||||
|
||||
private static ReceivedEmailContext MapToContext(long uid, IMail mail, List<Flag> flags)
|
||||
private static ReceivedEmailContext MapToContext(long uid, IMail mail, List<Flag> flags, bool withAttachments = false)
|
||||
{
|
||||
var attachments = new List<EmailAttachmentContext>();
|
||||
|
||||
foreach (var att in mail.Attachments)
|
||||
if (withAttachments)
|
||||
{
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
foreach (var att in mail.Attachments)
|
||||
{
|
||||
FileName = att.FileName ?? "attachment",
|
||||
Content = att.Data,
|
||||
ContentType = att.ContentType?.ToString() ?? "application/octet-stream",
|
||||
IsInline = false,
|
||||
ContentId = att.ContentId
|
||||
});
|
||||
}
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
{
|
||||
FileName = att.FileName ?? "attachment",
|
||||
Content = att.Data,
|
||||
ContentType = att.ContentType?.ToString() ?? "application/octet-stream",
|
||||
IsInline = false,
|
||||
ContentId = att.ContentId
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var vis in mail.Visuals)
|
||||
{
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
foreach (var vis in mail.Visuals)
|
||||
{
|
||||
FileName = vis.FileName ?? "inline",
|
||||
Content = vis.Data,
|
||||
ContentType = vis.ContentType?.ToString() ?? "application/octet-stream",
|
||||
IsInline = true,
|
||||
ContentId = vis.ContentId
|
||||
});
|
||||
attachments.Add(new EmailAttachmentContext
|
||||
{
|
||||
FileName = vis.FileName ?? "inline",
|
||||
Content = vis.Data,
|
||||
ContentType = vis.ContentType?.ToString() ?? "application/octet-stream",
|
||||
IsInline = true,
|
||||
ContentId = vis.ContentId
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return new ReceivedEmailContext
|
||||
|
||||
Reference in New Issue
Block a user