Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/MessageQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/MessageQueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int, int>
*/
public function uids(): Collection;

/**
* Append a new message to the folder.
*/
Expand Down
13 changes: 13 additions & 0 deletions src/Testing/FakeMessageQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}
*/
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/MessageQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Testing/FakeMessageQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading