Skip to content

Commit

Permalink
Simplified GetMimeMessageEnvelopeBodyAsync to only return one body
Browse files Browse the repository at this point in the history
  • Loading branch information
danzuep committed Sep 4, 2023
1 parent e4d737f commit 4c2cd62
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
34 changes: 15 additions & 19 deletions source/MailKitSimplified.Receiver/Services/MailFolderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ public static MailFolderReader Create(IImapClient imapClient, EmailReceiverOptio

public IMailReader Skip(int skipCount)
{
if (skipCount < 0)
throw new ArgumentOutOfRangeException(nameof(skipCount));
_skip = skipCount;
return this;
}

public IMailReader Take(int takeCount, bool continuous = false)
{
if (takeCount < -1)
throw new ArgumentOutOfRangeException(nameof(takeCount));
if (_take > ushort.MaxValue)
_logger.LogWarning($"Take({_take}) should be split into smaller batches.");
_take = takeCount;
Expand Down Expand Up @@ -154,19 +158,19 @@ public IMailReader Items(MessageSummaryItems messageSummaryItems)
private async Task CloseMailFolderAsync(IMailFolder mailFolder, bool close = true, int count = 1)
{
if (_continueTake && (count < 1 ||
_uniqueIds == null && (_take == 0 || _skip + (uint)_take > int.MaxValue) ||
_uniqueIds == null && (_take <= 0 || _skip + (uint)_take > int.MaxValue) ||
_uniqueIds != null && _uniqueIds.End.Id == uint.MaxValue))
_continueTake = false;
if (_continueTake)
{
if (_uniqueIds != null)
{
var size = _uniqueIds.End.Id - _uniqueIds.Start.Id;
var size = _uniqueIds.End.Id - _uniqueIds.Start.Id + 1;
uint endUid = _uniqueIds.End.Id + size;
if (size > 0 && endUid > _uniqueIds.End.Id)
_uniqueIds = new UniqueIdRange(new UniqueId(_uniqueIds.End.Id + 1), new UniqueId(endUid));
}
else if (_take > 0)
else
{
if (_skip < mailFolder.Count)
_skip += _take;
Expand Down Expand Up @@ -348,23 +352,15 @@ public async Task<MimeMessage> GetMimeMessageEnvelopeBodyAsync(IMessageSummary m
bool peekFolder = !messageSummary.Folder.IsOpen;
if (peekFolder || messageSummary.Folder.Access == FolderAccess.None)
_ = await messageSummary.Folder.OpenAsync(FolderAccess.ReadOnly, cancellationToken).ConfigureAwait(false);
var mimeEntities = new List<MimeEntity>();
if (messageSummary.TextBody is BodyPart textBody)
mimeEntities.Add(await messageSummary.Folder.GetBodyPartAsync(messageSummary.UniqueId, textBody, cancellationToken));

MimeEntity textEntity = null;
if (messageSummary.HtmlBody is BodyPart htmlBody)
mimeEntities.Add(await messageSummary.Folder.GetBodyPartAsync(messageSummary.UniqueId, htmlBody, cancellationToken));
if (mimeEntities.Count == 1)
{
mimeMessage.Body = mimeEntities[0];
}
else
{
var multipart = new Multipart();
foreach (var mimeEntity in mimeEntities)
if (mimeEntity != null)
multipart.Add(mimeEntity);
mimeMessage.Body = multipart;
}
textEntity = await messageSummary.Folder.GetBodyPartAsync(messageSummary.UniqueId, htmlBody, cancellationToken);
else if (messageSummary.TextBody is BodyPart textBody)
textEntity = await messageSummary.Folder.GetBodyPartAsync(messageSummary.UniqueId, textBody, cancellationToken);

if (textEntity is TextPart bodyText)
mimeMessage.Body = bodyText;

if (peekFolder)
await messageSummary.Folder.CloseAsync(expunge: false, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,50 @@ public async Task GetMimeMessagesAsync_WithAnyUniqueIds_ReturnsMimeMessages()
}

[Fact]
public async Task GetMessageAsync_WithNullMessageSummary_ReturnsMimeMessages()
public async Task GetMimeMessagesAsync_SkipTakeItemsQuery_ReturnsMimeMessages()
{
// Arrange
_mailFolderMock.Setup(_ => _.SearchAsync(It.IsAny<SearchQuery>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(Array.Empty<UniqueId>()).Verifiable();
_mailFolderMock.Setup(_ => _.GetMessageAsync(It.IsAny<int>(), It.IsAny<CancellationToken>(), It.IsAny<ITransferProgress>()))
.ReturnsAsync(new MimeMessage()).Verifiable();
// Act
var mimeMessages = await _mailFolderReader.Skip(0).Take(1).Items(MailFolderReader.CoreMessageItems).Query(SearchQuery.NotSeen)
var mimeMessages = await _mailFolderReader.Skip(0).Take(1, continuous: false)
.Items(MailFolderReader.CoreMessageItems).Query(SearchQuery.NotSeen)
.GetMimeMessagesAsync(It.IsAny<CancellationToken>(), It.IsAny<ITransferProgress>());
// Assert
Assert.NotNull(mimeMessages);
}

[Fact]
public async Task GetMimeMessagesRangeAsync_ContinuousRange_ReturnsMimeMessages()
{
// Arrange
_mailFolderMock.Setup(_ => _.GetMessageAsync(It.IsAny<UniqueId>(), It.IsAny<CancellationToken>(), It.IsAny<ITransferProgress>()))
.ReturnsAsync(new MimeMessage()).Verifiable();
var mailReader = _mailFolderReader.Range(UniqueId.MinValue, UniqueId.MinValue, continuous: true);
// Act
var mimeMessages = await mailReader.GetMimeMessagesAsync(It.IsAny<CancellationToken>(), It.IsAny<ITransferProgress>());
// Assert
Assert.Single(mimeMessages);
}

[Fact]
public async Task GetMimeMessagesEnvelopeBodyAsync_Range_ReturnsMimeMessages()
{
// Arrange
var stubMessageSummaries = new List<IMessageSummary>();
_mailFolderMock.Setup(_ => _.FetchAsync(It.IsAny<IList<UniqueId>>(), It.IsAny<IFetchRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(stubMessageSummaries);
_mailFolderMock.Setup(_ => _.GetMessageAsync(It.IsAny<UniqueId>(), It.IsAny<CancellationToken>(), It.IsAny<ITransferProgress>()))
.ReturnsAsync(new MimeMessage()).Verifiable();
// Act
var mimeMessages = await _mailFolderReader.Range(UniqueId.MinValue, UniqueId.MinValue, continuous: false)
.GetMimeMessagesEnvelopeBodyAsync(It.IsAny<CancellationToken>());
// Assert
Assert.NotNull(mimeMessages);
}

[Fact]
public async Task GetMessageSummariesAsync_WithMessageSummaryItemFilter_ReturnsMessageSummaries()
{
Expand Down

0 comments on commit 4c2cd62

Please sign in to comment.