Skip to content

Commit fa21e7c

Browse files
authored
Merge pull request #110 from DirectoryTree/BUG-109
Only fetch message items when set on query
2 parents 93333e1 + bf017d7 commit fa21e7c

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

src/MessageQuery.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,21 +261,37 @@ protected function fetch(Collection $messages): array
261261
'desc' => $messages->sortDesc(SORT_NUMERIC),
262262
};
263263

264-
$uids = $messages->forPage($this->page, $this->limit)
265-
->values()
266-
->all();
264+
$uids = $messages->forPage($this->page, $this->limit)->values();
267265

268-
$response = $this->connection()->fetch(array_filter([
269-
$this->fetchFlags ? 'FLAGS' : null,
270-
$this->fetchBody ? $this->fetchAsUnread
266+
$fetch = [];
267+
268+
if ($this->fetchFlags) {
269+
$fetch[] = 'FLAGS';
270+
}
271+
272+
if ($this->fetchBody) {
273+
$fetch[] = $this->fetchAsUnread
271274
? 'BODY.PEEK[TEXT]'
272-
: 'BODY[TEXT]' : null,
273-
$this->fetchHeaders ? $this->fetchAsUnread
275+
: 'BODY[TEXT]';
276+
}
277+
278+
if ($this->fetchHeaders) {
279+
$fetch[] = $this->fetchAsUnread
274280
? 'BODY.PEEK[HEADER]'
275-
: 'BODY[HEADER]' : null,
276-
]), $uids);
281+
: 'BODY[HEADER]';
282+
}
283+
284+
if (empty($fetch)) {
285+
return $uids->mapWithKeys(fn (string|int $uid) => [
286+
$uid => [
287+
'flags' => [],
288+
'headers' => '',
289+
'contents' => '',
290+
],
291+
])->all();
292+
}
277293

278-
return $response->mapWithKeys(function (UntaggedResponse $response) {
294+
return $this->connection()->fetch($fetch, $uids->all())->mapWithKeys(function (UntaggedResponse $response) {
279295
$data = $response->tokenAt(3);
280296

281297
$uid = $data->lookup('UID')->value;

tests/Integration/MessagesTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DirectoryTree\ImapEngine\DraftMessage;
66
use DirectoryTree\ImapEngine\Folder;
77
use DirectoryTree\ImapEngine\Message;
8+
use DirectoryTree\ImapEngine\MessageQuery;
89
use Illuminate\Support\ItemNotFoundException;
910

1011
function folder(): Folder
@@ -99,6 +100,42 @@ function folder(): Folder
99100
})->toThrow(ItemNotFoundException::class);
100101
});
101102

103+
test('get without fetches', function () {
104+
$folder = folder();
105+
106+
$uid = $folder->messages()->append(
107+
new DraftMessage(
108+
109+
text: 'hello world',
110+
),
111+
);
112+
113+
$messages = $folder->messages()->get();
114+
115+
expect($messages->count())->toBe(1);
116+
expect($messages->first()->uid())->toBe($uid);
117+
});
118+
119+
test('get with fetches', function (callable $callback) {
120+
$folder = folder();
121+
122+
$uid = $folder->messages()->append(
123+
new DraftMessage(
124+
125+
text: 'hello world',
126+
),
127+
);
128+
129+
$messages = $callback($folder->messages())->get();
130+
131+
expect($messages->count())->toBe(1);
132+
expect($messages->first()->uid())->toBe($uid);
133+
})->with([
134+
fn (MessageQuery $query) => $query->withBody(),
135+
fn (MessageQuery $query) => $query->withFlags(),
136+
fn (MessageQuery $query) => $query->withHeaders(),
137+
]);
138+
102139
test('append', function () {
103140
$folder = folder();
104141

0 commit comments

Comments
 (0)