Skip to content

Commit

Permalink
#73 re-read flag change email before returning it
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Aug 23, 2024
1 parent dd31382 commit b5577df
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions source/MailKitSimplified.Receiver/Services/MailFolderMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using MailKitSimplified.Receiver.Abstractions;
using MailKitSimplified.Receiver.Extensions;
using MailKitSimplified.Receiver.Models;
using System.Linq;

namespace MailKitSimplified.Receiver.Services
{
Expand Down Expand Up @@ -536,7 +537,14 @@ private async Task ProcessFlagChangeQueueAsync(Func<IMessageSummary, Task> messa
try
{
if (_flagChangeQueue.TryDequeue(out messageSummary))
await messageFlagChangedMethod(messageSummary).ConfigureAwait(false);
{
var filter = _folderMonitorOptions.MessageSummaryItems | MessageSummaryItems.UniqueId;
var uniqueIds = new UniqueId[] { messageSummary.UniqueId };
var fetched = await _fetchFolder.FetchAsync(uniqueIds, filter, cancellationToken).ConfigureAwait(false);
messageSummary = fetched.FirstOrDefault();
if (messageSummary != null)
await messageFlagChangedMethod(messageSummary).ConfigureAwait(false);
}
else if (_flagChangeQueue.IsEmpty)
await Task.Delay(_folderMonitorOptions.EmptyQueueMaxDelayMs, cancellationToken).ConfigureAwait(false);
retryCount = 0;
Expand Down Expand Up @@ -602,15 +610,19 @@ private void OnFlagsChanged(object sender, MessageFlagsChangedEventArgs e)
if (index < cachedCount)
{
messageSummary = _messageCache[index];
_flagChangeQueue.Enqueue(messageSummary);
}
}
using (_logger.BeginScope("OnFlagsChanged"))
{
if (messageSummary != null)
{
_flagChangeQueue.Enqueue(messageSummary);
_logger.Log<MailFolderMonitor>($"{_imapReceiver}[{index}] flags have changed ({e.Flags}), item #{messageSummary.UniqueId}.", LogLevel.Trace);
}
else
{
_logger.Log<MailFolderMonitor>($"{_imapReceiver}[{index}] message flag change (count={cachedCount}) was out of range.", LogLevel.Warning);
}
}
}

Expand All @@ -631,15 +643,19 @@ private void OnMessageExpunged(object sender, MessageEventArgs e)
{
messageSummary = _messageCache[index];
_messageCache.RemoveAt(index);
_departureQueue.Enqueue(messageSummary);
}
}
using (_logger.BeginScope("OnMessageExpunged"))
{
if (messageSummary != null)
{
_departureQueue.Enqueue(messageSummary);
_logger.Log<MailFolderMonitor>($"{_imapReceiver}[{index}] (count={cachedCount}) expunged, item #{messageSummary.UniqueId}.", LogLevel.Trace);
}
else
{
_logger.Log<MailFolderMonitor>($"{_imapReceiver}[{index}] (count={cachedCount}) was out of range.", LogLevel.Warning);
}
}
}

Expand Down

0 comments on commit b5577df

Please sign in to comment.