refactor: bunch of improvements
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -725,24 +725,40 @@ class RemoteMailService {
|
||||
}
|
||||
|
||||
/**
|
||||
* delete entity from remote storage
|
||||
* delete entities from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityDelete(string $id): ?string {
|
||||
public function entityDelete(string ...$identifiers): array {
|
||||
// construct set request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct object
|
||||
$r0->delete($id);
|
||||
foreach ($identifiers as $id) {
|
||||
$r0->delete($id);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command succeeded
|
||||
if (array_search($id, $response->deleted()) !== false) {
|
||||
return $response->stateNew();
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
$results = [];
|
||||
// check for success
|
||||
foreach ($response->deleteSuccesses() as $id) {
|
||||
$results[$id] = true;
|
||||
}
|
||||
// check for failure
|
||||
foreach ($response->deleteFailures() as $id => $data) {
|
||||
$results[$id] = $data['type'] ?? 'unknownError';
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -751,7 +767,7 @@ class RemoteMailService {
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCopy(string $target, array $sources): array {
|
||||
public function entityCopy(string $target, string ...$identifiers): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -759,15 +775,8 @@ class RemoteMailService {
|
||||
* move entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityMove(string $target, array $sources): array {
|
||||
|
||||
// extract identifiers from sources
|
||||
$identifiers = [];
|
||||
foreach ($sources as $source) {
|
||||
$identifiers = array_merge($identifiers, $source);
|
||||
}
|
||||
public function entityMove(string $target, string ...$identifiers): array {
|
||||
// construct request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
foreach ($identifiers as $id) {
|
||||
|
||||
@@ -69,10 +69,10 @@ class RemoteService {
|
||||
}
|
||||
// debugging
|
||||
if ($service->getDebug()) {
|
||||
$logDir = Server::getInstance()?->logDir();
|
||||
$logDir .= '/jmap/' . $service->identifier() . '.json';
|
||||
$client->configureTransportLogState(true);
|
||||
$client->configureTransportLogLocation(
|
||||
sys_get_temp_dir() . '/' . $location->getHost() . '-' . $identity->getIdentity() . '.log'
|
||||
);
|
||||
$client->configureTransportLogLocation($logDir);
|
||||
}
|
||||
// return
|
||||
return $client;
|
||||
|
||||
Reference in New Issue
Block a user