refactor: bunch of improvements
All checks were successful
Build Test / test (pull_request) Successful in 35s
JS Unit Tests / test (pull_request) Successful in 33s
PHP Unit Tests / test (pull_request) Successful in 1m5s

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-04-23 22:04:36 -04:00
parent d0e8406830
commit acc42d09ee
8 changed files with 406 additions and 259 deletions

View File

@@ -11,6 +11,7 @@ namespace KTXM\ProviderJmapc\Providers\Mail;
use Generator;
use KTXF\Mail\Collection\CollectionBaseInterface;
use KTXF\Mail\Collection\CollectionRoles;
use KTXF\Mail\Collection\CollectionMutableInterface;
use KTXF\Mail\Object\Address;
use KTXF\Mail\Object\AddressInterface;
@@ -24,6 +25,8 @@ use KTXF\Resource\Provider\ResourceServiceLocationInterface;
use KTXF\Resource\Delta\Delta;
use KTXF\Resource\Filter\Filter;
use KTXF\Resource\Filter\IFilter;
use KTXF\Resource\Identifier\CollectionIdentifier;
use KTXF\Resource\Identifier\EntityIdentifier;
use KTXF\Resource\Range\IRange;
use KTXF\Resource\Range\Range;
use KTXF\Resource\Range\RangeType;
@@ -95,6 +98,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
self::CAPABILITY_ENTITY_DELTA => true,
self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_FETCH => true,
//self::CAPABILITY_ENTITY_DELETE => true,
//self::CAPABILITY_ENTITY_MOVE => true,
];
private readonly RemoteMailService $mailService;
@@ -535,12 +540,75 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
return $entities;
}
public function entityMove(string|int $target, array $sources): array
public function entityDelete(EntityIdentifier ...$identifiers): array
{
// validate identifiers and construct ID list
$ids = [];
foreach ($identifiers as $identifier) {
if ($identifier->provider() !== $this->provider() || $identifier->service() !== (string)$this->identifier()) {
throw new \InvalidArgumentException('Entity identifier does not belong to this service: ' . (string)$identifier);
}
$ids[] = $identifier->entity();
}
$this->initialize();
$result = $this->mailService->entityMove($target, $sources);
$deleteMode = strtolower(trim((string) ($this->getAuxiliary()['deleteMode'] ?? 'soft')));
if ($deleteMode === 'soft') {
$deleteDestination = $this->getAuxiliary()['deleteDestination'] ?? null;
if (is_string($deleteDestination) || is_int($deleteDestination)) {
$deleteDestination = trim((string) $deleteDestination);
if ($deleteDestination === '') {
$deleteDestination = null;
}
} else {
$deleteDestination = null;
}
return $result;
if ($deleteDestination === null) {
$filter = $this->collectionListFilter();
$filter->condition(self::CAPABILITY_COLLECTION_FILTER_ROLE, CollectionRoles::Trash->value);
foreach ($this->collectionList(null, $filter) as $collection) {
if (!$collection instanceof CollectionBaseInterface) {
continue;
}
if ($collection->getProperties()->getRole() === CollectionRoles::Trash) {
$deleteDestination = (string) $collection->identifier();
break;
}
}
}
if ($deleteDestination === null) {
throw new \RuntimeException('Soft delete is enabled but no trash collection could be resolved.');
}
return $this->mailService->entityMove($deleteDestination, ...$ids);
}
return $this->mailService->entityDelete(...$ids);
}
public function entityMove(CollectionIdentifier $target, EntityIdentifier ...$identifiers): array
{
// validate target belongs to this service
if ($target->provider() !== $this->provider() || $target->service() !== (string)$this->identifier()) {
throw new \InvalidArgumentException('Target collection does not belong to this service');
}
// validate identifiers and construct ID list
$ids = [];
foreach ($identifiers as $identifier) {
if ($identifier->provider() !== $this->provider() || $identifier->service() !== (string)$this->identifier()) {
throw new \InvalidArgumentException('Entity identifier does not belong to this service: ' . (string)$identifier);
}
$ids[] = $identifier->entity();
}
$this->initialize();
return $this->mailService->entityMove($target->collection(), ...$ids);
}
}