Remove caching from LimilabsImapEmailService
Simplified the `LimilabsImapEmailService` by removing the dependency on `IMemoryCache` and eliminating all caching logic. The constructor no longer accepts an `IMemoryCache` parameter, and the static `CacheKeyPrefix` field has been removed. Replaced the caching mechanism with direct email fetching using `imap.GetMessageByUIDAsync`. Refactored the logic for processing attachments and visuals into `EmailAttachmentDto` objects, and streamlined the construction of `ReceivedEmailDto` to include metadata directly from the fetched email data. These changes reduce complexity, improve maintainability, and ensure the service always retrieves the latest email data from the IMAP server.
This commit is contained in:
@@ -6,7 +6,6 @@ using DigitalData.MessagingService.Domain.Exceptions;
|
|||||||
using DigitalData.MessagingService.Infrastructure.Services.Extensions;
|
using DigitalData.MessagingService.Infrastructure.Services.Extensions;
|
||||||
using Limilabs.Client.IMAP;
|
using Limilabs.Client.IMAP;
|
||||||
using Limilabs.Mail;
|
using Limilabs.Mail;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@@ -16,10 +15,8 @@ namespace DigitalData.MessagingService.Infrastructure.Services;
|
|||||||
/// IMAP email service using Limilabs Mail.dll.
|
/// IMAP email service using Limilabs Mail.dll.
|
||||||
/// Opens a fresh connection per call — stateless and thread-safe.
|
/// Opens a fresh connection per call — stateless and thread-safe.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger, IMemoryCache Cache) : IImapEmailService
|
public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger) : IImapEmailService
|
||||||
{
|
{
|
||||||
private static readonly string CacheKeyPrefix = Guid.NewGuid().ToString();
|
|
||||||
|
|
||||||
static LimilabsImapEmailService()
|
static LimilabsImapEmailService()
|
||||||
{
|
{
|
||||||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
||||||
@@ -67,58 +64,50 @@ public class LimilabsImapEmailService(ILogger<LimilabsImapEmailService> Logger,
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
#region Read email
|
#region Read email
|
||||||
var email = await Cache.GetOrCreateAsync(
|
var eml = await imap.GetMessageByUIDAsync(uid, cancel);
|
||||||
CacheKeyPrefix + uid,
|
var mail = new MailBuilder().CreateFromEml(eml);
|
||||||
async entry =>
|
var flags = await imap.GetFlagsByUIDAsync(uid, cancel);
|
||||||
|
|
||||||
|
var attachments = new List<EmailAttachmentDto>();
|
||||||
|
|
||||||
|
foreach (var att in mail.Attachments)
|
||||||
|
{
|
||||||
|
attachments.Add(new EmailAttachmentDto
|
||||||
{
|
{
|
||||||
var eml = await imap.GetMessageByUIDAsync(uid, cancel);
|
FileName = att.FileName ?? "attachment",
|
||||||
var mail = new MailBuilder().CreateFromEml(eml);
|
Content = att.Data,
|
||||||
var flags = await imap.GetFlagsByUIDAsync(uid, cancel);
|
ContentType = att.ContentType?.ToString() ?? "application/octet-stream",
|
||||||
|
IsInline = false,
|
||||||
var attachments = new List<EmailAttachmentDto>();
|
ContentId = att.ContentId
|
||||||
|
|
||||||
foreach (var att in mail.Attachments)
|
|
||||||
{
|
|
||||||
attachments.Add(new EmailAttachmentDto
|
|
||||||
{
|
|
||||||
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 EmailAttachmentDto
|
|
||||||
{
|
|
||||||
FileName = vis.FileName ?? "inline",
|
|
||||||
Content = vis.Data,
|
|
||||||
ContentType = vis.ContentType?.ToString() ?? "application/octet-stream",
|
|
||||||
IsInline = true,
|
|
||||||
ContentId = vis.ContentId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ReceivedEmailDto
|
|
||||||
{
|
|
||||||
Uid = uid,
|
|
||||||
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
|
||||||
To = [.. mail.To.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)],
|
|
||||||
Cc = [.. mail.Cc.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)],
|
|
||||||
Subject = mail.Subject ?? string.Empty,
|
|
||||||
TextBody = mail.Text ?? string.Empty,
|
|
||||||
HtmlBody = mail.Html ?? string.Empty,
|
|
||||||
Date = mail.Date ?? DateTime.MinValue,
|
|
||||||
IsSeen = flags.Contains(Flag.Seen),
|
|
||||||
Attachments = attachments,
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
#endregion Read email
|
}
|
||||||
|
|
||||||
if (email is null)
|
foreach (var vis in mail.Visuals)
|
||||||
continue;
|
{
|
||||||
|
attachments.Add(new EmailAttachmentDto
|
||||||
|
{
|
||||||
|
FileName = vis.FileName ?? "inline",
|
||||||
|
Content = vis.Data,
|
||||||
|
ContentType = vis.ContentType?.ToString() ?? "application/octet-stream",
|
||||||
|
IsInline = true,
|
||||||
|
ContentId = vis.ContentId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
var email = new ReceivedEmailDto
|
||||||
|
{
|
||||||
|
Uid = uid,
|
||||||
|
From = mail.From.FirstOrDefault()?.Address ?? string.Empty,
|
||||||
|
To = [.. mail.To.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)],
|
||||||
|
Cc = [.. mail.Cc.SelectMany(m => m.GetMailboxes()).Select(mb => mb.Address)],
|
||||||
|
Subject = mail.Subject ?? string.Empty,
|
||||||
|
TextBody = mail.Text ?? string.Empty,
|
||||||
|
HtmlBody = mail.Html ?? string.Empty,
|
||||||
|
Date = mail.Date ?? DateTime.MinValue,
|
||||||
|
IsSeen = flags.Contains(Flag.Seen),
|
||||||
|
Attachments = attachments,
|
||||||
|
};
|
||||||
|
#endregion Read email
|
||||||
|
|
||||||
if (filter.UnseenOnly && email.IsSeen)
|
if (filter.UnseenOnly && email.IsSeen)
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user