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
17 changes: 17 additions & 0 deletions src/MessageQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
44 changes: 42 additions & 2 deletions tests/Unit/MessageQueryTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

use DirectoryTree\ImapEngine\Connection\ImapConnection;
use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder;
use DirectoryTree\ImapEngine\Connection\Streams\FakeStream;
use DirectoryTree\ImapEngine\Folder;
use DirectoryTree\ImapEngine\Mailbox;
use DirectoryTree\ImapEngine\MessageQuery;

function query(): MessageQuery
function query(?Mailbox $mailbox = null): MessageQuery
{
return new MessageQuery(
new Folder(new Mailbox, 'test'),
new Folder($mailbox ?? new Mailbox, 'test'),
new ImapQueryBuilder
);
}
Expand All @@ -27,3 +29,41 @@ function query(): MessageQuery

expect($query)->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)');
});