Refactor IMAP email fetching to use SearchFilter
Replaced individual parameters (`folder`, `unseenOnly`, `maxCount`) in `IImapEmailService` with a consolidated `SearchFilter` object to simplify method signatures and improve maintainability. Renamed `MailQuery` to `SearchFilter` in `FetchEmailsQuery` for better clarity. Updated `FetchEmailsQueryHandler` and `LimilabsImapEmailService` to use the new `SearchFilter` object, ensuring consistent handling of folder selection, unread message filtering, and message count limits. Improved logging in `LimilabsImapEmailService` to reflect the updated `SearchFilter` structure.
This commit is contained in:
@@ -25,28 +25,26 @@ public class LimilabsImapEmailService(
|
||||
// Public API
|
||||
public async Task<IEnumerable<ReceivedEmailContext>> FetchEmailsAsync(
|
||||
EmailAccountDto account,
|
||||
string folder = "INBOX",
|
||||
bool unseenOnly = false,
|
||||
int maxCount = 50,
|
||||
FetchEmailsQuery.SearchFilter filter,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var imap = new Imap();
|
||||
try
|
||||
{
|
||||
await ConnectAndAuthenticateAsync(imap, account);
|
||||
await SelectFolderAsync(imap, folder);
|
||||
await SelectFolderAsync(imap, filter.Folder);
|
||||
|
||||
// Get UIDs to fetch
|
||||
List<long> uids = unseenOnly
|
||||
List<long> uids = filter.UnseenOnly
|
||||
? [.. await imap.SearchAsync(Flag.Unseen, cancellationToken)]
|
||||
: [.. await imap.GetAllAsync(cancellationToken)];
|
||||
|
||||
// Most-recent first; honour maxCount
|
||||
uids.Reverse();
|
||||
if (maxCount > 0 && uids.Count > maxCount)
|
||||
uids = [.. uids.Take(maxCount)];
|
||||
if (filter.MaxCount > 0 && uids.Count() > filter.MaxCount)
|
||||
uids = [.. uids.Take(filter.MaxCount)];
|
||||
|
||||
var results = new List<ReceivedEmailContext>(uids.Count);
|
||||
var results = new List<ReceivedEmailContext>(uids.Count());
|
||||
|
||||
foreach (var uid in uids)
|
||||
{
|
||||
@@ -62,7 +60,7 @@ public class LimilabsImapEmailService(
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogWarning(ex, "Failed to fetch IMAP message UID={Uid} from folder {Folder}. Skipping.", uid, folder);
|
||||
logger.LogWarning(ex, "Failed to fetch IMAP message UID={Uid} from folder {Folder}. Skipping.", uid, filter.Folder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user