File tree Expand file tree Collapse file tree 2 files changed +64
-11
lines changed
Expand file tree Collapse file tree 2 files changed +64
-11
lines changed Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 55use DirectoryTree \ImapEngine \DraftMessage ;
66use DirectoryTree \ImapEngine \Folder ;
77use DirectoryTree \ImapEngine \Message ;
8+ use DirectoryTree \ImapEngine \MessageQuery ;
89use Illuminate \Support \ItemNotFoundException ;
910
1011function 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+ from: 'foo@email.com ' ,
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+ from: 'foo@email.com ' ,
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+
102139test ('append ' , function () {
103140 $ folder = folder ();
104141
You can’t perform that action at this time.
0 commit comments