diff --git a/src/MessageQuery.php b/src/MessageQuery.php index d96e005..867c058 100644 --- a/src/MessageQuery.php +++ b/src/MessageQuery.php @@ -10,6 +10,7 @@ use DirectoryTree\ImapEngine\Connection\Responses\UntaggedResponse; use DirectoryTree\ImapEngine\Connection\Tokens\Atom; use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; +use DirectoryTree\ImapEngine\Enums\ImapFlag; use DirectoryTree\ImapEngine\Exceptions\ImapCommandException; use DirectoryTree\ImapEngine\Pagination\LengthAwarePaginator; use DirectoryTree\ImapEngine\Support\ForwardsCalls; @@ -546,6 +547,22 @@ public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentif return $this->process(new MessageCollection([$uid]))->first(); } + /** + * Destroy the given messages. + */ + public function destroy(array|int $uids, bool $expunge = false): void + { + $uids = (array) $uids; + + $this->folder->mailbox() + ->connection() + ->store([ImapFlag::Deleted->value], $uids, mode: '+'); + + if ($expunge) { + $this->folder->expunge(); + } + } + /** * Get the UID for the given identifier. */ diff --git a/tests/Unit/MessageQueryTest.php b/tests/Unit/MessageQueryTest.php index 69ab5b3..5d87941 100644 --- a/tests/Unit/MessageQueryTest.php +++ b/tests/Unit/MessageQueryTest.php @@ -1,14 +1,16 @@ toBe('SUBJECT "hello"'); }); + +test('destroy', function () { + $stream = new FakeStream; + $stream->open(); + + $stream->feed([ + '* OK Welcome to IMAP', + 'TAG1 OK Logged in', + 'TAG2 OK UID STORE completed', + ]); + + $mailbox = Mailbox::make(); + + $mailbox->connect(new ImapConnection($stream)); + + query($mailbox)->destroy(1); + + $stream->assertWritten('TAG2 UID STORE 1 +FLAGS.SILENT (\Deleted)'); +}); + +test('destroy with multiple messages', function () { + $stream = new FakeStream; + $stream->open(); + + $stream->feed([ + '* OK Welcome to IMAP', + 'TAG1 OK Logged in', + 'TAG2 OK UID STORE completed', + ]); + + $mailbox = Mailbox::make(); + + $mailbox->connect(new ImapConnection($stream)); + + query($mailbox)->destroy([1, 2, 3]); + + $stream->assertWritten('TAG2 UID STORE 1,2,3 +FLAGS.SILENT (\Deleted)'); +});