Skip to content

Commit 74ba2d3

Browse files
committed
Add ability to destroy messages without fetching them
1 parent f950cb9 commit 74ba2d3

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/MessageQuery.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse;
1111
use DirectoryTree\ImapEngine\Connection\Tokens\Atom;
1212
use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier;
13+
use DirectoryTree\ImapEngine\Enums\ImapFlag;
1314
use DirectoryTree\ImapEngine\Exceptions\ImapCommandException;
1415
use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator;
1516
use DirectoryTree\ImapEngine\Support\ForwardsCalls;
@@ -546,6 +547,22 @@ public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentif
546547
return $this->process(new MessageCollection([$uid]))->first();
547548
}
548549

550+
/**
551+
* Destroy the given messages.
552+
*/
553+
public function destroy(array|int $uids, bool $expunge = false): void
554+
{
555+
$uids = (array) $uids;
556+
557+
$this->folder->mailbox()
558+
->connection()
559+
->store([ImapFlag::Deleted->value], $uids, mode: '+');
560+
561+
if ($expunge) {
562+
$this->folder->expunge();
563+
}
564+
}
565+
549566
/**
550567
* Get the UID for the given identifier.
551568
*/

tests/Unit/MessageQueryTest.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

3+
use DirectoryTree\ImapEngine\Connection\ImapConnection;
34
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
5+
use DirectoryTree\ImapEngine\Connection\Streams\FakeStream;
46
use DirectoryTree\ImapEngine\Folder;
57
use DirectoryTree\ImapEngine\Mailbox;
68
use DirectoryTree\ImapEngine\MessageQuery;
79

8-
function query(): MessageQuery
10+
function query(?Mailbox $mailbox = null): MessageQuery
911
{
1012
return new MessageQuery(
11-
new Folder(new Mailbox, 'test'),
13+
new Folder($mailbox ?? new Mailbox, 'test'),
1214
new ImapQueryBuilder
1315
);
1416
}
@@ -27,3 +29,41 @@ function query(): MessageQuery
2729

2830
expect($query)->toBe('SUBJECT "hello"');
2931
});
32+
33+
test('destroy', function () {
34+
$stream = new FakeStream;
35+
$stream->open();
36+
37+
$stream->feed([
38+
'* OK Welcome to IMAP',
39+
'TAG1 OK Logged in',
40+
'TAG2 OK UID STORE completed',
41+
]);
42+
43+
$mailbox = Mailbox::make();
44+
45+
$mailbox->connect(new ImapConnection($stream));
46+
47+
query($mailbox)->destroy(1);
48+
49+
$stream->assertWritten('TAG2 UID STORE 1 +FLAGS.SILENT (\Deleted)');
50+
});
51+
52+
test('destroy with multiple messages', function () {
53+
$stream = new FakeStream;
54+
$stream->open();
55+
56+
$stream->feed([
57+
'* OK Welcome to IMAP',
58+
'TAG1 OK Logged in',
59+
'TAG2 OK UID STORE completed',
60+
]);
61+
62+
$mailbox = Mailbox::make();
63+
64+
$mailbox->connect(new ImapConnection($stream));
65+
66+
query($mailbox)->destroy([1, 2, 3]);
67+
68+
$stream->assertWritten('TAG2 UID STORE 1,2,3 +FLAGS.SILENT (\Deleted)');
69+
});

0 commit comments

Comments
 (0)