diff --git a/MailKit/Net/Imap/ImapFolder.cs b/MailKit/Net/Imap/ImapFolder.cs index b1ccaa1baf..0acca90c75 100644 --- a/MailKit/Net/Imap/ImapFolder.cs +++ b/MailKit/Net/Imap/ImapFolder.cs @@ -4508,7 +4508,7 @@ async Task> GetIndexesAsync (IList uids, bool doAsync, Canc { var command = string.Format ("SEARCH UID {0}\r\n", UniqueIdSet.ToString (uids)); var ic = new ImapCommand (Engine, cancellationToken, this, command); - var results = new SearchResults (); + var results = new SearchResults (SortOrder.Ascending); if ((Engine.Capabilities & ImapCapabilities.ESearch) != 0) ic.RegisterUntaggedHandler ("ESEARCH", ESearchMatchesAsync); diff --git a/MailKit/Net/Imap/ImapFolderSearch.cs b/MailKit/Net/Imap/ImapFolderSearch.cs index 2de02cfe8e..f011550a0b 100644 --- a/MailKit/Net/Imap/ImapFolderSearch.cs +++ b/MailKit/Net/Imap/ImapFolderSearch.cs @@ -337,8 +337,8 @@ static string BuildSortOrder (IList orderBy) static async Task SearchMatchesAsync (ImapEngine engine, ImapCommand ic, int index, bool doAsync) { - var uids = new UniqueIdSet (SortOrder.Ascending); var results = (SearchResults) ic.UserData; + var uids = results.UniqueIds; ImapToken token; uint uid; @@ -387,7 +387,6 @@ static async Task ESearchMatchesAsync (ImapEngine engine, ImapCommand ic, int in { var token = await engine.ReadTokenAsync (doAsync, ic.CancellationToken).ConfigureAwait (false); var results = (SearchResults) ic.UserData; - UniqueIdSet uids = null; int parenDepth = 0; //bool uid = false; string atom; @@ -500,9 +499,10 @@ static async Task ESearchMatchesAsync (ImapEngine engine, ImapCommand ic, int in case "ALL": ImapEngine.AssertToken (token, ImapTokenType.Atom, ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token); - uids = ImapEngine.ParseUidSet (token, ic.Folder.UidValidity, ImapEngine.GenericItemSyntaxErrorFormat, atom, token); + var uids = ImapEngine.ParseUidSet (token, ic.Folder.UidValidity, ImapEngine.GenericItemSyntaxErrorFormat, atom, token); results.Count = uids.Count; + results.UniqueIds = uids; break; default: throw ImapEngine.UnexpectedToken (ImapEngine.GenericUntaggedResponseSyntaxErrorFormat, "ESEARCH", token); @@ -510,8 +510,6 @@ static async Task ESearchMatchesAsync (ImapEngine engine, ImapCommand ic, int in token = await engine.ReadTokenAsync (doAsync, ic.CancellationToken).ConfigureAwait (false); } while (true); - - results.UniqueIds = uids ?? new UniqueIdSet (); } async Task SearchAsync (string query, bool doAsync, CancellationToken cancellationToken) @@ -531,7 +529,7 @@ async Task SearchAsync (string query, bool doAsync, CancellationT if ((Engine.Capabilities & ImapCapabilities.ESearch) != 0) ic.RegisterUntaggedHandler ("ESEARCH", ESearchMatchesAsync); ic.RegisterUntaggedHandler ("SEARCH", SearchMatchesAsync); - ic.UserData = new SearchResults (); + ic.UserData = new SearchResults (SortOrder.Ascending); Engine.QueueCommand (ic); @@ -669,7 +667,7 @@ async Task> SearchAsync (SearchQuery query, bool doAsync, Cancel // respond with "* SEARCH ..." instead of "* ESEARCH ..." even when using the extended // search syntax. ic.RegisterUntaggedHandler ("SEARCH", SearchMatchesAsync); - ic.UserData = new SearchResults (); + ic.UserData = new SearchResults (SortOrder.Ascending); Engine.QueueCommand (ic); diff --git a/MailKit/Search/SearchResults.cs b/MailKit/Search/SearchResults.cs index 58976f0410..ec1bb39b7c 100644 --- a/MailKit/Search/SearchResults.cs +++ b/MailKit/Search/SearchResults.cs @@ -41,9 +41,10 @@ public class SearchResults /// /// Creates a new . /// - public SearchResults () + /// The expected sort-order of the results. + public SearchResults (SortOrder order = SortOrder.None) { - UniqueIds = new UniqueId[0]; + UniqueIds = new UniqueIdSet (order); } /// diff --git a/UnitTests/Net/Imap/ImapClientTests.cs b/UnitTests/Net/Imap/ImapClientTests.cs index 0ee52b5c0c..b93e7f26e5 100644 --- a/UnitTests/Net/Imap/ImapClientTests.cs +++ b/UnitTests/Net/Imap/ImapClientTests.cs @@ -3008,8 +3008,9 @@ public void TestDovecot () Assert.IsFalse (matches.Min.HasValue, "MIN should not be set"); Assert.AreEqual (0, matches.Count, "COUNT should not be set"); Assert.AreEqual (14, matches.UniqueIds.Count); + var expectedSortByReverseArrivalResults = new uint[] { 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8 }; for (int i = 0; i < matches.UniqueIds.Count; i++) - Assert.AreEqual (i + 1, matches.UniqueIds[i].Id); + Assert.AreEqual (expectedSortByReverseArrivalResults[i], matches.UniqueIds[i].Id); destination.GetStreams (UniqueIdRange.All, GetStreamsCallback); destination.GetStreams (new int[] { 0, 1, 2 }, GetStreamsCallback); @@ -3632,8 +3633,9 @@ public async void TestDovecotAsync () Assert.IsFalse (matches.Min.HasValue, "MIN should not be set"); Assert.AreEqual (0, matches.Count, "COUNT should not be set"); Assert.AreEqual (14, matches.UniqueIds.Count); + var expectedSortByReverseArrivalResults = new uint[] { 7, 14, 6, 13, 5, 12, 4, 11, 3, 10, 2, 9, 1, 8 }; for (int i = 0; i < matches.UniqueIds.Count; i++) - Assert.AreEqual (i + 1, matches.UniqueIds[i].Id); + Assert.AreEqual (expectedSortByReverseArrivalResults[i], matches.UniqueIds[i].Id); await destination.GetStreamsAsync (UniqueIdRange.All, GetStreamsCallback); await destination.GetStreamsAsync (new int[] { 0, 1, 2 }, GetStreamsCallback);