diff --git a/src/MessageQuery.php b/src/MessageQuery.php index 66ee8bd..edcccb8 100644 --- a/src/MessageQuery.php +++ b/src/MessageQuery.php @@ -75,7 +75,17 @@ public function firstOrFail(): MessageInterface */ public function get(): MessageCollection { - return $this->process($this->sortKey ? $this->sort() : $this->search()); + return $this->process($this->matchingUids()); + } + + /** + * {@inheritDoc} + */ + public function uids(): Collection + { + return $this->pageUids($this->matchingUids()) + ->values() + ->map(fn (string|int $uid) => (int) $uid); } /** @@ -369,15 +379,21 @@ public function copy(string $folder): int */ protected function process(Collection $uids): MessageCollection { - $selected = $this->order($uids)->forPage($this->page, $this->limit); - - $messages = $this->load($selected); + $messages = $this->load($this->pageUids($uids)); $messages->total($uids->count()); return $messages; } + /** + * Get the UIDs for the current page in query order. + */ + protected function pageUids(Collection $uids): Collection + { + return $this->order($uids)->forPage($this->page, $this->limit); + } + /** * Yield each page of UIDs in their existing order. * @@ -526,6 +542,14 @@ protected function fetch(Collection $uids): array })->all(); } + /** + * Look up matching UIDs using the configured search or server-side sort. + */ + protected function matchingUids(): Collection + { + return $this->sortKey ? $this->sort() : $this->search(); + } + /** * Execute an IMAP search request. */ diff --git a/src/MessageQueryInterface.php b/src/MessageQueryInterface.php index f2d5573..ad3f93d 100644 --- a/src/MessageQueryInterface.php +++ b/src/MessageQueryInterface.php @@ -10,6 +10,7 @@ use DirectoryTree\ImapEngine\Pagination\Cursor; use DirectoryTree\ImapEngine\Pagination\CursorPaginator; use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator; +use Illuminate\Support\Collection; use Illuminate\Support\ItemNotFoundException; use Illuminate\Support\LazyCollection; use Illuminate\Support\MultipleItemsFoundException; @@ -227,6 +228,13 @@ public function sole(): MessageInterface; */ public function get(): MessageCollection; + /** + * Get the matching message UIDs for the current page in query order. + * + * @return Collection + */ + public function uids(): Collection; + /** * Append a new message to the folder. */ diff --git a/src/Testing/FakeMessageQuery.php b/src/Testing/FakeMessageQuery.php index 19b7950..108a5d8 100644 --- a/src/Testing/FakeMessageQuery.php +++ b/src/Testing/FakeMessageQuery.php @@ -12,6 +12,7 @@ use DirectoryTree\ImapEngine\Pagination\CursorPaginator; use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator; use DirectoryTree\ImapEngine\QueriesMessages; +use Illuminate\Support\Collection; use Illuminate\Support\LazyCollection; use InvalidArgumentException; @@ -37,6 +38,18 @@ public function get(): MessageCollection ); } + /** + * {@inheritDoc} + */ + public function uids(): Collection + { + return $this->get()->toBase() + ->sortBy(fn (MessageInterface $message) => $message->uid(), SORT_NUMERIC, $this->fetchOrder === 'desc') + ->forPage($this->page, $this->limit) + ->values() + ->map(fn (MessageInterface $message) => $message->uid()); + } + /** * {@inheritDoc} */ diff --git a/tests/Unit/MessageQueryTest.php b/tests/Unit/MessageQueryTest.php index 4b6638d..c321217 100644 --- a/tests/Unit/MessageQueryTest.php +++ b/tests/Unit/MessageQueryTest.php @@ -839,6 +839,50 @@ protected function search(): Collection ['desc', [10, 2]], ]); +test('uids returns the requested page in UID order without fetching messages', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feed([ + '* OK Welcome to IMAP', + 'TAG1 OK Logged in', + 'TAG2 OK SELECT completed', + '* SEARCH 50 2 100 1 10', + 'TAG3 OK SEARCH completed', + ]); + + $mailbox = Mailbox::make(); + $mailbox->connect(new ImapConnection($stream)); + + $query = (new Folder($mailbox, 'INBOX'))->messages()->unseen()->withBody()->oldest()->limit(2, 2); + + expect($query->uids()->all())->toBe([10, 50]); + $stream->assertWritten('TAG3 UID SEARCH UNSEEN'); + $stream->assertNotWritten('UID FETCH'); +}); + +test('uids preserves server sort order', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feed([ + '* OK Welcome to IMAP', + 'TAG1 OK Logged in', + 'TAG2 OK SELECT completed', + '* CAPABILITY IMAP4rev1 SORT', + 'TAG3 OK CAPABILITY completed', + '* SORT 50 2 100 1 10', + 'TAG4 OK SORT completed', + ]); + + $mailbox = Mailbox::make(); + $mailbox->connect(new ImapConnection($stream)); + + $query = (new Folder($mailbox, 'INBOX'))->messages()->sortBy('date')->limit(2, 2); + + expect($query->uids()->all())->toBe([100, 1]); + $stream->assertWritten('TAG4 UID SORT (DATE) UTF-8 ALL'); + $stream->assertNotWritten('UID FETCH'); +}); + test('get preserves server sort order before pagination', function () { $stream = new FakeStream; $stream->open(); diff --git a/tests/Unit/Testing/FakeMessageQueryTest.php b/tests/Unit/Testing/FakeMessageQueryTest.php index ec2ea28..68cc991 100644 --- a/tests/Unit/Testing/FakeMessageQueryTest.php +++ b/tests/Unit/Testing/FakeMessageQueryTest.php @@ -32,6 +32,18 @@ expect($collection)->toHaveCount(2); }); +test('it returns UIDs in the requested order and page', function () { + $folder = new FakeFolder('INBOX', messages: [ + new FakeMessage(2), + new FakeMessage(10), + new FakeMessage(1), + ]); + + $query = $folder->messages()->newest()->limit(2, 2); + + expect($query->uids()->all())->toBe([1]); +}); + test('it iterates lazily in the requested UID order', function (string $order, array $uids) { $folder = new FakeFolder('INBOX', messages: [ new FakeMessage(10),