From de71f95e3de1cfe27f41d8f40ac0b081ef3f5182 Mon Sep 17 00:00:00 2001 From: Sebastian Krupinski Date: Sat, 26 Sep 2026 21:49:16 -0400 Subject: [PATCH] refactor: mailbox resolution Signed-off-by: Sebastian Krupinski --- lib/Providers/Mail/Service.php | 45 +++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/Providers/Mail/Service.php b/lib/Providers/Mail/Service.php index f2362b5..02ef582 100644 --- a/lib/Providers/Mail/Service.php +++ b/lib/Providers/Mail/Service.php @@ -468,21 +468,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC $this->initialize(); $deleteMode = $this->auxiliary['deleteMode'] ?? 'soft'; - $deleteTarget = $this->auxiliary['deleteTarget'] ?? null; if ($deleteMode !== 'soft' && $deleteMode !== 'hard') { throw new \InvalidArgumentException("Invalid delete mode: $deleteMode"); } - $deleteTargetIdentifier = null; - if ($deleteMode === 'soft') { - $deleteTargetIdentifier = $deleteTarget !== null && (string) $deleteTarget !== '' - ? new CollectionIdentifier($this->provider(), (string) $this->identifier(), (string) $deleteTarget) - : $this->resolveCollectionByRole(CollectionRoles::Trash->value); - if ($deleteTargetIdentifier === null) { - throw new \RuntimeException('No Trash collection configured or found for deletion'); - } - } + $deleteTargetIdentifier = $deleteMode === 'soft' ? $this->resolveDeleteDestination() : null; if ($deleteMode === 'soft') { $targetMailbox = $this->mailService->collectionFetch((string) $target->collection()); @@ -737,7 +728,6 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC // determine delete mode and target collection (e.g. Trash) if applicable $deleteMode = $this->auxiliary['deleteMode'] ?? 'soft'; - $deleteTarget = $this->auxiliary['deleteTarget'] ?? null; if ($deleteMode !== 'soft' && $deleteMode !== 'hard') { throw new \InvalidArgumentException("Invalid delete mode: $deleteMode"); @@ -746,15 +736,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC // connect to remote store $this->initialize(); - $deleteTargetIdentifier = null; - if ($deleteMode === 'soft') { - $deleteTargetIdentifier = $deleteTarget !== null && (string) $deleteTarget !== '' - ? new CollectionIdentifier($this->provider(), (string) $this->identifier(), (string) $deleteTarget) - : $this->resolveCollectionByRole(CollectionRoles::Trash->value); - if ($deleteTargetIdentifier === null) { - throw new \RuntimeException('No Trash collection configured or found for deletion'); - } - } + $deleteTargetIdentifier = $deleteMode === 'soft' ? $this->resolveDeleteDestination() : null; $deleteTargetNative = $deleteTargetIdentifier?->collection(); @@ -1036,6 +1018,29 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC return [$native->toJmap(), $uploads]; } + /** + * Resolve the collection soft-deleted messages and collections are moved to + */ + private function resolveDeleteDestination(): CollectionIdentifier + { + $destination = trim((string) ($this->auxiliary['deleteDestination'] ?? '')); + + if ($destination === '') { + return $this->resolveCollectionByRole(CollectionRoles::Trash->value) + ?? throw new \RuntimeException('No Trash collection configured or found for deletion'); + } + + $role = CollectionRoles::tryFrom(strtolower($destination)); + if ($role !== null && $role !== CollectionRoles::None) { + $resolved = $this->resolveCollectionByRole($role->value); + if ($resolved !== null) { + return $resolved; + } + } + + return new CollectionIdentifier($this->provider(), (string) $this->identifier(), $destination); + } + private function resolveCollectionByRole(string $role): ?CollectionIdentifier { $filter = $this->collectionListFilter();